{{/*
Copyright 2017 The Openstack-Helm Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}

{{- if .Values.manifests.cron_job_mariadb_backup }}
{{- $envAll := . }}

{{- $serviceAccountName := "mariadb-backup" }}
{{ tuple $envAll "mariadb_backup" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: mariadb-backup
  annotations:
    {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }}
spec:
  schedule: {{ .Values.jobs.backup_mariadb.cron | quote }}
  successfulJobsHistoryLimit: {{ .Values.jobs.backup_mariadb.history.success }}
  failedJobsHistoryLimit: {{ .Values.jobs.backup_mariadb.history.failed }}
  concurrencyPolicy: Forbid
  jobTemplate:
    metadata:
      labels:
{{ tuple $envAll "mariadb-backup" "backup" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
    spec:
      template:
        metadata:
          labels:
{{ tuple $envAll "mariadb-backup" "backup" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 12 }}
        spec:
          serviceAccountName: {{ $serviceAccountName }}
          restartPolicy: OnFailure
          nodeSelector:
            {{ .Values.labels.job.node_selector_key }}: {{ .Values.labels.job.node_selector_value }}
          initContainers:
{{ tuple $envAll "mariadb_backup" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 10 }}
          containers:
            - name: mariadb-backup
              command:
                - /tmp/backup_mariadb.sh
              env:
                - name: MARIADB_BACKUP_BASE_DIR
                  value: {{ .Values.conf.backup.base_path | quote }}
                - name: MYSQL_BACKUP_MYSQLDUMP_OPTIONS
                  value: {{ .Values.conf.backup.mysqldump_options | quote }}
                - name: MARIADB_BACKUP_DAYS_TO_KEEP
                  value: {{ .Values.conf.backup.days_of_backup_to_keep | quote }}
                - name: MARIADB_POD_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
{{ tuple $envAll "mariadb_backup" | include "helm-toolkit.snippets.image" | indent 14 }}
{{ tuple $envAll $envAll.Values.pod.resources.jobs.mariadb_backup | include "helm-toolkit.snippets.kubernetes_resources" | indent 14 }}
              volumeMounts:
                - name: pod-tmp
                  mountPath: /tmp
                - mountPath: /tmp/backup_mariadb.sh
                  name: mariadb-bin
                  readOnly: true
                  subPath: backup_mariadb.sh
                - mountPath: {{ .Values.conf.backup.base_path }}
                  name: mariadb-backup-dir
                - name: mariadb-secrets
                  mountPath: /etc/mysql/admin_user.cnf
                  subPath: admin_user.cnf
                  readOnly: true
          restartPolicy: OnFailure
          serviceAccount: {{ $serviceAccountName }}
          serviceAccountName: {{ $serviceAccountName }}
          volumes:
            - name: pod-tmp
              emptyDir: {}
            - name: mariadb-secrets
              secret:
                secretName: mariadb-secrets
                defaultMode: 384
            - configMap:
                defaultMode: 365
                name: mariadb-bin
              name: mariadb-bin
            {{- if and .Values.volume.backup.enabled  .Values.manifests.pvc_backup }}
            - name: mariadb-backup-dir
              persistentVolumeClaim:
                claimName: mariadb-backup-data
            {{- else }}
            - hostPath:
                path: {{ .Values.conf.backup.base_path }}
                type: DirectoryOrCreate
              name: mariadb-backup-dir
            {{- end }}
{{- end }}