{{- if .Values.cronjob.enabled -}}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "freeleaps-data-backup.fullname" . }}
  namespace: {{ .Values.backup.namespace }}
  labels:
    app: {{ include "freeleaps-data-backup.name" . }}
    component: backup
    {{- include "freeleaps-data-backup.labels" . | nindent 4 }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  schedule: {{ .Values.cronjob.schedule | quote }}
  concurrencyPolicy: {{ .Values.cronjob.concurrencyPolicy }}
  successfulJobsHistoryLimit: {{ .Values.cronjob.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.cronjob.failedJobsHistoryLimit }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: {{ include "freeleaps-data-backup.name" . }}
            component: backup
            {{- include "freeleaps-data-backup.labels" . | nindent 12 }}
        spec:
          {{- if .Values.serviceAccount.enabled }}
          serviceAccountName: {{ .Values.serviceAccount.name }}
          {{- end }}
          restartPolicy: {{ .Values.cronjob.restartPolicy }}
          {{- with .Values.global.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          containers:
          - name: backup
            image: "{{ .Values.global.imageRegistry }}/{{ .Values.image.repository }}:{{ .Values.image.tag }}"
            imagePullPolicy: {{ .Values.image.pullPolicy }}
            env:
            - name: TZ
              value: "UTC"
            - name: BACKUP_NAMESPACE
              value: {{ .Values.backup.namespace | quote }}
            - name: SNAPSHOT_CLASS
              value: {{ .Values.backup.snapshotClass | quote }}
            - name: TIMEOUT
              value: {{ .Values.backup.timeout | quote }}
            resources:
              {{- toYaml .Values.resources | nindent 14 }}
            securityContext:
              {{- toYaml .Values.securityContext | nindent 14 }}
{{- end }} 