apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgresql
  namespace: white-rabbit
  labels:
    app: postgresql
    group: white-rabbit
    version: 11.3.0-debian-9-r38
spec:
  serviceName: postgresql
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
      group: white-rabbit
      version: 11.3.0-debian-9-r38
      # role: master
  template:
    metadata:
      name: postgresql
      labels:
        app: postgresql
        group: white-rabbit
        version: 11.3.0-debian-9-r38
        # role: master
    spec:
      securityContext:
        fsGroup: 1001
      initContainers:
        - name: init-chmod-data
          image: docker.io/bitnami/minideb:latest
          imagePullPolicy: "Always"
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
          command:
            - sh
            - -c
            - |
              mkdir -p /bitnami/postgresql/data
              chmod 700 /bitnami/postgresql/data
              find /bitnami/postgresql -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | \
                xargs chown -R 1001:1001
          securityContext:
            runAsUser: 0
          volumeMounts:
            - name: data-dir
              mountPath: /bitnami/postgresql
      containers:
        - name: postgresql
          image: docker.io/bitnami/postgresql:11.3.0-debian-9-r38
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 250m
              memory: 256Mi
            limits:
              cpu: 500m
              memory: 512Mi
          securityContext:
            runAsUser: 1001
          env:
            - name: BITNAMI_DEBUG
              value: "false"
            - name: POSTGRESQL_PORT_NUMBER
              value: "5432"
            - name: PGDATA
              value: "/bitnami/postgresql/data"
            - name: POSTGRES_USER
              value: "user"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgresql
                  key: user-password
            - name: POSTGRES_DB
              value: "server-app"
          ports:
            - name: postgresql
              containerPort: 5432
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - exec pg_isready -U "user" -d "server-app" -h 127.0.0.1 -p 5432
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - |
                  pg_isready -U "user" -d "server-app" -h 127.0.0.1 -p 5432
                  [ -f /opt/bitnami/postgresql/tmp/.initialized ]
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          volumeMounts:
            - name: data-dir
              mountPath: /bitnami/postgresql
  volumeClaimTemplates:
    - metadata:
        name: data-dir
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 3G
