apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-name-1
  namespace: default
  labels:
    app: postgres-name
    index: '2'
    replicationRole: replica

spec:
  serviceName: "postgres-replica-name"
  replicas: 1
  selector:
    matchLabels:
      app: postgres-name
      index: '2'

  volumeClaimTemplates:
    - metadata:
        name: postgres-db
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: standard
        resources:
          requests:
            storage: 1Gi

  template:
    metadata:
      labels:
        app: postgres-name
        index: '2'
        replicationRole: replica

    spec:
      terminationGracePeriodSeconds: 10
      volumes:
        - name: base-config
          configMap:
            name: base-kubegres-config
            defaultMode: 0777

        - name: custom-config
          configMap:
            name: toBeReplaced
            defaultMode: 0777

      initContainers:
        - name: setup-replica-data-directory
          image: postgres:latest
          imagePullPolicy: IfNotPresent
          env:
            - name: PRIMARY_HOST_NAME
            - name: PGPASSWORD
            - name: PGDATA

          command:
            - sh
            - -c
            - /tmp/copy_primary_data_to_replica.sh

          volumeMounts:
            - name: postgres-db
              mountPath: toBeReplaced

            - name: base-config
              mountPath: /tmp/copy_primary_data_to_replica.sh
              subPath: copy_primary_data_to_replica.sh

      containers:
        - name: postgres-name-1
          image: postgres:latest
          imagePullPolicy: IfNotPresent
          args: ["-c", "config_file=/etc/postgres.conf", "-c", "hba_file=/etc/pg_hba.conf", "-c", "promote_trigger_file=$(PGDATA)/promote_replica_to_primary.log", "-c", "wal_level=replica"]

          ports:
            - containerPort: 5432
              protocol: TCP

          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP

          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready -U postgres -h $POD_IP
            failureThreshold: 6
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 5

          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready -U postgres -h $POD_IP
            failureThreshold: 3
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 3

          volumeMounts:
            - name: postgres-db
              mountPath: toBeReplaced

            - name: base-config
              mountPath: /etc/postgres.conf
              subPath: postgres.conf

            - name: base-config
              mountPath: /etc/pg_hba.conf
              subPath: pg_hba.conf
