{{- if .Values.backup.enabled }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ template "jenkins.fullname" . }}-backup
  labels:
    app: {{ template "jenkins.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  schedule: {{ .Values.backup.schedule | quote }}
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 120
  jobTemplate:
    spec:
      template:
        metadata:
          annotations:
            {{ toYaml .Values.backup.annotations }}
        spec:
          restartPolicy: OnFailure
          serviceAccountName: {{ template "jenkins.fullname" . }}-backup
          containers:
          - name: jenkins-backup
            image: "{{ .Values.backup.image.repository }}:{{ .Values.backup.image.tag }}"
            command: ["kube-tasks"]
            args:
            - simple-backup
            - -n
            - {{ .Release.Namespace }}
            - -l
            - release={{ .Release.Name }}
            - --container
            - {{ template "jenkins.fullname" . }}
            - --path
            - /var/jenkins_home
            - --dst
            - {{ .Values.backup.destination }}
            {{- with .Values.backup.extraArgs }}
{{ toYaml . | indent 12 }}
            {{- end }}
            {{- with .Values.backup.env }}
            env:
{{ toYaml . | indent 12 }}
            {{- end }}
          {{- with .Values.backup.resources }}
            resources:
{{ toYaml . | indent 14 }}
          {{- end }}
        affinity:
          podAffinity:
            preferredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - {{ template "jenkins.fullname" . }}
                - key: release
                  operator: In
                  values:
                  - {{ .Release.Name }}
              topologyKey: "kubernetes.io/hostname"
      {{- with .Values.tolerations }}
        tolerations:
{{ toYaml . | indent 10 }}
      {{- end }}
{{- end }}
