apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "restic-helm.fullname" . }}
  labels:
    {{- include "restic-helm.labels" . | nindent 4 }}
spec:
  concurrencyPolicy: Forbid
  schedule: "{{ .Values.schedule }}"
  jobTemplate:
    metadata:
      {{- with .Values.jobAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "restic-helm.labels" . | nindent 8 }}
    spec:
      template:
        metadata:
          {{- with .Values.podAnnotations }}
          annotations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          labels:
            {{- include "restic-helm.labels" . | nindent 12 }}
        spec:
          restartPolicy: OnFailure
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.podSecurityContext | nindent 12 }}
          volumes:
            - name: data
              {{- .Values.volume | nindent 14 }}
          initContainers:
            - name: forget
              securityContext:
                {{- toYaml .Values.securityContext | nindent 16 }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              args:
                - "forget"
                - "--prune"
                - "--verbose"
                {{- toYaml .Values.keepOptions | nindent 16 }}
              envFrom:
                - secretRef:
                    name: {{ include "restic-helm.fullname" . }}
              resources:
                {{- toYaml .Values.resources | nindent 16 }}
          containers:
            - name: backup
              securityContext:
                {{- toYaml .Values.securityContext | nindent 16 }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              workingDir: "/sync"
              args:
                - "backup"
                - "."
                - "--verbose"
              envFrom:
                - secretRef:
                    name: {{ include "restic-helm.fullname" . }}
              resources:
                {{- toYaml .Values.resources | nindent 16 }}
              volumeMounts:
                - mountPath: "/sync"
                  name: data
          {{- with .Values.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
