apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: timescaledb-data
  labels:
    app: timescaledb
    timescaleNodeType: data
spec:
  serviceName: timescaledb-data
  replicas: 3
  selector:
    matchLabels:
      app: timescaledb
      timescaleNodeType: data
  template:
    metadata:
      name: timescaledb-data
      labels:
        app: timescaledb
        timescaleNodeType: data
    spec:
      serviceAccountName: timescaledb
      securityContext:
        # The postgres user inside the TimescaleDB image has uid=1000.
        # This configuration ensures the permissions of the mounts are suitable
        fsGroup: 1000
      initContainers:
        - name: initdb
          image: "timescaledev/timescaledb:latest-pg11"
          imagePullPolicy: IfNotPresent
          env:
            - name: POSTGRESQL_CUSTOM_PARAMETERS
              value: |
                log_checkpoints = 'on'
                log_connections = 'on'
                log_line_prefix = '%t [%p]: [%c-%l] %u@%d,app=%a [%e] '
                log_lock_waits = 'on'
                log_min_duration_statement = '1s'
                log_statement = 'ddl'
                max_connections = '100'
                max_prepared_transactions = '150'
                max_wal_size = '512MB'
                min_wal_size = '256MB'
                shared_buffers = '300MB'
                temp_file_limit = '1GB'
                timescaledb.passfile = '../.pgpass'
                work_mem = '16MB'
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: timescaledb-data
                  key: password-superuser
            - name: LC_ALL
              value: C.UTF-8
            - name: LANG
              value: C.UTF-8
            - name: PGDATA
              value: /var/lib/postgresql/pgdata
          command:
            - sh
            - '-c'
            # By calling the original entrypoint with the first argument being postgres
            # we ensure we do everything that is required to init a PostgreSQL instance.
            # By supplying --version however, we ensure the instance isn't started, but merely
            # a version number is displayed, allowing us to do some more initialization
            - |
              set -e
              install -o postgres -g postgres -m 0700 -d "${PGDATA}" "${PGDATA}/../conf.d"
              /docker-entrypoint.sh postgres --version
              grep -qxF "include 'postgresql_helm_customizations.conf'" "${PGDATA}/postgresql.conf" \
                  || echo "include 'postgresql_helm_customizations.conf'" >> "${PGDATA}/postgresql.conf"
              echo "Writing custom PostgreSQL Parameters to ${PGDATA}/postgresql_helm_customizations.conf"
              echo "cluster_name = '$(hostname)'" > "${PGDATA}/postgresql_helm_customizations.conf"
              echo "${POSTGRESQL_CUSTOM_PARAMETERS}" | sort >> "${PGDATA}/postgresql_helm_customizations.conf"
              # The TimescaleDB extension should not be available by default, as this interferes with the bootstrapping
              # done by the access nodes. Therefore we drop the extensions from template1
              echo "DROP EXTENSION timescaledb" | /docker-entrypoint.sh postgres --single -D "${PGDATA}" template1
          volumeMounts:
            - name: storage-volume
              mountPath: "/var/lib/postgresql"
              # subPath: ""
      containers:
        - name: timescaledb
          image: "timescaledev/timescaledb:latest-pg11"
          imagePullPolicy: IfNotPresent
          # We start postgres with a fully cleared environment
          command:
            - sh
            - '-c'
            - exec env -i PGDATA="${PGDATA}" PATH="${PATH}" /docker-entrypoint.sh postgres
          env:
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: timescaledb-data
                  key: password-superuser
            - name: LC_ALL
              value: C.UTF-8
            - name: LANG
              value: C.UTF-8
            - name: PGDATA
              value: /var/lib/postgresql/pgdata
          ports:
            - name: postgresql
              containerPort: 5432
          resources:
            requests:
              cpu: 1000m
              memory: 1Gi
            limits:
              cpu: 1500m
              memory: 2Gi
          volumeMounts:
            - name: storage-volume
              mountPath: "/var/lib/postgresql"
              # subPath: ""
  volumeClaimTemplates:
    - metadata:
        name: storage-volume
        labels:
          app: timescaledb
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 5G
