{{ if .Values.cronjobs.enabled }}
{{- range $job := .Values.cronjobs.jobs }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ $job.name }}
spec:
  schedule: {{ default "0 0 1 1 *" $job.schedule }}
  suspend: {{ default false $job.suspend }}
  timeZone: {{ $.Values.timeZone }}
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      ttlSecondsAfterFinished: 604800 # 7 days
      backoffLimit: 0
      completions: 1
      parallelism: 1
      template:
        spec:
          restartPolicy: Never
          terminationGracePeriodSeconds: 1
          containers:
          - name: {{ $job.name }}
            image: {{ $job.image }}
            imagePullPolicy: Always
            volumeMounts:
          {{- range $mount_name, $mount := $job.mounts }}
              - mountPath: {{ $mount.mountPath }}
                name: {{ $mount_name }}
          {{- end }}
            args:
          {{- range $arg := $job.args }}
              - {{ $arg }}
          {{- end }}
            env:
            - name: ADMIN_LOGIN
              valueFrom:
                secretKeyRef:
                  name: secret-admin
                  key: username
                  optional: true
            - name: ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-admin
                  key: password
                  optional: true
            - name: SERVICE_LOGIN
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: username
                  optional: true
            - name: SERVICE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-service
                  key: password
                  optional: true
          {{- range $job.env }}
            - name: {{ .name }}
            {{- if .valueFrom }}
              valueFrom:
                secretKeyRef:
                  name: {{ .valueFrom.secretKeyRef.name }}
                  key: {{ .valueFrom.secretKeyRef.key }}
            {{- end }}
            {{- if .value }}
              value: {{ .value }}
            {{- end }}
          {{- end }}
    {{ if $.Values.local_user_id }}
            - name: LOCAL_USER_ID
              value: "{{$.Values.local_user_id}}"
    {{ end }}
    {{ if $.Values.local_user_name }}
            - name: LOCAL_USER_NAME
              value: "{{$.Values.local_user_name}}"
    {{ end }}

          imagePullSecrets:
          - name: regcred

    {{ if .mounts }}
          volumes:
        {{- range $mount_name, $mount := $job.mounts }}
            - name: {{ $mount_name }}
              hostPath:
                path: {{ tpl $mount.hostPath $ }}
                type: {{ default "Directory" $mount.type }}
        {{- end }}
    {{ end }}
          {{ if $.Values.azure }}
          affinity:
            nodeAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                nodeSelectorTerms:
                - matchExpressions:
                  - key: agentpool
                    operator: NotIn
                    values:
                    - agentpool
          {{ end }}
---
{{- end }}
{{ end }}
