{{- if and .Values.persistence.enabled .Values.cron.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  schedule: {{ .Values.cron.schedule | quote }}
  startingDeadlineSeconds: 100
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: delete-mail
              image: "{{ .Values.cron.image.repository }}:{{ .Values.cron.image.tag }}"
              imagePullPolicy: {{ .Values.cron.image.pullPolicy }}
              args:
                - "find"
                - "{{ .Values.persistence.mountPath }}"
                - "-type"
                - "f"
                - "-mtime"
                - "+{{ .Values.cron.daysToKeep }}"
                - "-exec"
                - "rm"
                - "-f"
                - "{}"
                - ";"
              volumeMounts:
                - name: data
                  mountPath: {{ .Values.persistence.mountPath }}
              securityContext:
                runAsUser: 1000
                runAsGroup: 1000
            - name: reload-mail
              image: "{{ .Values.cron.image.repository }}:{{ .Values.cron.image.tag }}"
              imagePullPolicy: {{ .Values.cron.image.pullPolicy }}
              args: ["wget","{{ include "common.names.fullname" . }}:1080/reloadMailsFromDirectory","-q","-O","/dev/null"]
              securityContext:
                runAsUser: 1000
                runAsGroup: 1000
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: {{ include "common.names.fullname" . }}
          restartPolicy: OnFailure
  {{- end }}
