---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
  namespace: spack
  labels:
    app: gitlab
    svc: db
spec:
  selector:
    matchLabels:
      app: gitlab
      svc: db
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: gitlab
        svc: db
    spec:
      initContainers:
      # - name: nuke-it-all
      #   image: busybox
      #   imagePullPolicy: IfNotPresent
      #   command: [
      #     "find", "/data",
      #       "-mindepth", "1",
      #       "-maxdepth", "1",
      #       "-exec", "rm", "-rf", "{}", "+"]
      #   volumeMounts:
      #   - name: data
      #     mountPath: /data

      - name: clear-lost-and-found
        image: busybox
        imagePullPolicy: Always
        command: ["rm", "-rf", "/pgdata/lost+found/"]
        volumeMounts:
        - name: data
          mountPath: /pgdata
      containers:
      - name: postgresql
        image: "postgres:9.6.2"
        imagePullPolicy: IfNotPresent
        args: ["-c", "shared_buffers=256MB", "-c", "max_connections=1000"]
        env:
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-user
        - name: PGUSER
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-user
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-name
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: gitlab-secrets
              key: gitlab-db-password
        - name: POD_IP
          valueFrom: { fieldRef: { fieldPath: status.podIP } }
        ports:
        - name: postgresql
          containerPort: 5432
        livenessProbe:
          exec:
            command: ["sh", "-c", "exec pg_isready --host $POD_IP"]
          initialDelaySeconds: 60
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          exec:
            command: ["sh", "-c", "exec pg_isready --host $POD_IP"]
          initialDelaySeconds: 5
          timeoutSeconds: 3
          periodSeconds: 5
        resources:
          requests:
            memory: 256Mi
            cpu: 100m
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data/pgdata
      nodeSelector:
        "beta.kubernetes.io/instance-type": "t2.medium"
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: postgresql-data
