apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: postgresql
  name: postgresql
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      volumes:
      - name: postgresql-claim0
        persistentVolumeClaim:
          claimName: postgresql-claim0
      #### ONLY NEEDED ON VERY FIRST RUN
      initContainers:
        - name: delete-lostfound-dir
          image: busybox
          command: ["rm", "-rf", "/var/lib/postgresql/data/lost+found"]
          volumeMounts:
          - name: postgresql-claim0
            mountPath: /var/lib/postgresql/data
        - name: fix-pgdata-permissions
          image: busybox
          command: ["/bin/chown", "-R", "70:70", "/var/lib/postgresql/data"]
          volumeMounts:
          - name: postgresql-claim0
            mountPath: /var/lib/postgresql/data        
      restartPolicy: Always
      containers:
        - name: postgresql
          image: postgres:13-alpine
          imagePullPolicy: Always
          env:
            - name: TZ
              valueFrom:
                configMapKeyRef:
                  name: postgresql-cm
                  key: timezone
            - name: PGTZ
              valueFrom:
                configMapKeyRef:
                  name: postgresql-cm
                  key: timezone
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: postgresql-cm
                  key: initdb
            - name: POSTGRES_USER
              valueFrom:
                configMapKeyRef:
                  name: postgresql-cm
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: postgresql-cm
                  key: password
          ports:
            - containerPort: 5432
          startupProbe:
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 10
            tcpSocket: 
              port: 5432
          readinessProbe:
            periodSeconds: 60
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 3
            tcpSocket: 
              port: 5432
          livenessProbe:
            periodSeconds: 60
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 3
            tcpSocket: 
              port: 5432
          volumeMounts:
            - name: postgresql-claim0
              mountPath: /var/lib/postgresql/data
