# helm_charts_jenkins-backup-cronjob.yaml
{{- if .Values.backup.enabled }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ template "jenkins.fullname" . }}-backup
  namespace: {{ template "jenkins.namespace" . }}
  labels:
    "app.kubernetes.io/name": '{{ template "jenkins.name" .}}'
    "helm.sh/chart": "{{ .Chart.Name }}-{{ .Chart.Version }}"
    "app.kubernetes.io/managed-by": "{{ .Release.Service }}"
    "app.kubernetes.io/instance": "{{ .Release.Name }}"
    "app.kubernetes.io/component": "{{ .Values.backup.componentName }}"
spec:
  schedule: {{ .Values.backup.schedule | quote }}
  concurrencyPolicy: Forbid
  startingDeadlineSeconds: 120
  jobTemplate:
    spec:
      template:
        metadata:
          {{- if .Values.backup.labels }}
          labels:
{{ toYaml .Values.backup.labels | trim | indent 12 }}
          {{- end }}
          {{- if .Values.backup.annotations }}
          annotations:
{{ toYaml .Values.backup.annotations | trim | indent 12 }}
          {{- end }}
        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
            - {{ template "jenkins.namespace" . }}
            - -l
            - app.kubernetes.io/instance={{ .Release.Name }}
            - --container
            - jenkins
            - --path
            - {{ .Values.master.jenkinsHome }}
            - --dst
            - {{ .Values.backup.destination }}
            {{- with .Values.backup.extraArgs }}
{{ toYaml . | indent 12 }}
            {{- end }}
            env:
            {{- with .Values.backup.env }}
{{ toYaml . | trim | indent 12 }}
            {{- end }}
            {{- if .Values.backup.existingSecret }}
            {{- range $key,$value := .Values.backup.existingSecret }}
            {{- if $value.awsaccesskey }}
            - name: AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: {{ $key }}
                  key: {{ $value.awsaccesskey | quote }}
            {{- end }}
            {{- if $value.awssecretkey }}
            - name: AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ $key }}
                  key: {{ $value.awssecretkey | quote}}
            {{- end }}
            {{- if $value.gcpcredentials }}
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: "/var/run/secrets/{{ $key }}/{{ $value.gcpcredentials }}"
            {{- end }}
            {{- end }}
            {{- end }}
          {{- with .Values.backup.resources }}
            resources:
{{ toYaml . | trim | indent 14 }}
          {{- end }}
            volumeMounts:
          {{- if .Values.backup.existingSecret }}
          {{- range $key,$value := .Values.backup.existingSecret }}
          {{- if $value.gcpcredentials }}
            - mountPath: /var/run/secrets/{{ $key }}
              name: {{ $key }}
          {{- end }}
          {{- end }}
          {{- end }}
          volumes:
          {{- if .Values.backup.existingSecret }}
          {{- range $key,$value := .Values.backup.existingSecret }}
          {{- if $value.gcpcredentials }}
          - name:  {{ $key }}
            secret:
              secretName:  {{ $key }}
          {{- end }}
          {{- end }}
          {{- end }}
          affinity:
            podAffinity:
              preferredDuringSchedulingIgnoredDuringExecution:
              - weight: 100
                podAffinityTerm:
                  topologyKey: "kubernetes.io/hostname"
                  labelSelector:
                    matchExpressions:
                    - key: app
                      operator: In
                      values:
                      - {{ template "jenkins.fullname" . }}
                    - key: release
                      operator: In
                      values:
                      - {{ .Release.Name }}
      {{- with .Values.tolerations }}
          tolerations:
{{ toYaml . | indent 10 }}
      {{- end }}
{{- end }}