---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: ${K8S_PROJECT}-backup-${BRANCH_HASH}
  labels:
    app: ${K8S_PROJECT}-backup-${BRANCH_HASH}
    git/branch: ${BRANCH_NAME}
    git/commit: ${COMMIT}
    gitlab/job: "${JOB_ID}"
spec:
  schedule: "0 7 * * *"
  jobTemplate:
    spec:
      # NOTE(douglasduteil): Ensure to keep the backup jobs at least a day.
      # So we can consult its logs
      # 86400 = 60s * 60m * 24h => 1 jour
      ttlSecondsAfterFinished: 86400
      template:
        spec:
          containers:
            #
            #
            #

            - name: backup
              image: postgres:10-alpine
              command:
                - sh
              args:
                - -c
                - |
                  sh -x /mnt/script/backup.sh ;
                  sh -x /mnt/script/status.sh backup ;
              resources:
                requests:
                  cpu: 10m
                  memory: 32Mi
                limits:
                  cpu: 100m
                  memory: 64Mi
              env:
                - name: CRONJOB_STATUS_DIR
                  value: /var/lib/cronjob/status
                - name: DUMP_DIR
                  value: /mnt/data
                - name: PGDATABASE
                  value: emjpm
                - name: PGHOST
                  value: ${K8S_PROJECT}-postgres-${BRANCH_HASH}
                - name: PGUSER
                  value: postgres
                - name: PGPASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: ${K8S_PROJECT}-secret
                      key: POSTGRES_PASSWORD
              volumeMounts:
                - mountPath: "/mnt/data"
                  name: data
                - mountPath: "/mnt/script"
                  name: script
                - mountPath: "/var/lib/cronjob/status"
                  name: cronjob-config

            #
            #
            #

            - name: postgres
              image: ${CI_REGISTRY_IMAGE}/postgres:${IMAGE_TAG}
              command:
                - sh
              args:
                - -c
                - |
                  (
                    sh -x /mnt/script/wait_for_status.sh anonymize &&
                    gosu postgres pg_ctl stop
                  ) &
                  gosu postgres docker-entrypoint.sh postgres ;
              resources:
                requests:
                  cpu: 10m
                  memory: 32Mi
                limits:
                  cpu: 100m
                  memory: 128Mi
              ports:
                - containerPort: 5432
              env:
                - name: CRONJOB_STATUS_DIR
                  value: /var/lib/cronjob/status
                - name: PGUSER
                  value: postgres
              volumeMounts:
                - mountPath: "/mnt/script"
                  name: script
                - mountPath: "/var/lib/cronjob/status"
                  name: cronjob-config

            #
            #
            #

            - name: anonymize
              image: postgres:10-alpine
              command:
                - sh
              args:
                - -c
                - |
                  sh -x /mnt/script/wait_for_status.sh backup ;
                  sh -x /mnt/script/anonymous.sh ;
                  sh -x /mnt/script/status.sh anonymize ;
              resources:
                requests:
                  cpu: 10m
                  memory: 32Mi
                limits:
                  cpu: 100m
                  memory: 64Mi
              env:
                - name: CRONJOB_STATUS_DIR
                  value: /var/lib/cronjob/status
                - name: PGDATABASE
                  value: emjpm
                - name: PGHOST
                  value: localhost
                - name: PGUSER
                  value: postgres
                - name: SAFE_DUMP_DIR
                  value: /mnt/anonymous
                - name: UNSAFE_DUMP_DIR
                  value: /mnt/data
              volumeMounts:
                - mountPath: "/mnt/data"
                  name: data
                - mountPath: "/mnt/anonymous"
                  name: anonymous
                - mountPath: "/mnt/script"
                  name: script
                - mountPath: "/var/lib/cronjob/status"
                  name: cronjob-config

            #
            #
            #

          restartPolicy: Never
          volumes:
            - name: script
              configMap:
                name: ${K8S_PROJECT}-backup-configmap-${BRANCH_HASH}
            - name: cronjob-config
              emptyDir: {}
            - name: data
              azureFile:
                secretName: ${K8S_PROJECT}-azure-storage-${BRANCH_HASH}
                shareName: ${K8S_PROJECT}-backup-restore
                readOnly: false
            - name: anonymous
              azureFile:
                secretName: ${K8S_PROJECT}-azure-storage-dev
                shareName: ${K8S_PROJECT}-backup-restore
                readOnly: false
