---
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 ;
              resources:
                requests:
                  cpu: 10m
                  memory: 32Mi
                limits:
                  cpu: 100m
                  memory: 64Mi
              env:
                - name: DUMP_DIR
                  value: /mnt/data
                - name: DUMP_DEV_DIR
                  value: /mnt/dev-data
                - name: PGDATABASE
                  value: emjpm
                - name: PGUSER
                  value: ${ADMIN_PG_USER}
                - name: PGHOST
                  value: ${PG_HOST}
                - name: PGPASSWORD
                  value: "${ADMIN_PG_PASSWORD}"
              volumeMounts:
                - mountPath: "/mnt/data"
                  name: data
                - mountPath: "/mnt/dev-data"
                  name: dev-data
                - mountPath: "/mnt/script"
                  name: script

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