{{- range .Values.benji.crontab }}

---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ tuple $ .name | include "benji.fullname" }}
  annotations:
    checksum/config: {{ $.Values.benji.configuration | toYaml | sha256sum }}
  labels:
    app.kubernetes.io/name: {{ include "benji.name" $ }}
    helm.sh/chart: {{ include "benji.chart" $ }}
    app.kubernetes.io/instance: {{ $.Release.Name }}
    app.kubernetes.io/component: cronjob-{{ .name }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
spec:
  schedule: {{ .schedule | quote }}
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  jobTemplate:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "benji.name" $ }}
        app.kubernetes.io/instance: {{ $.Release.Name }}
        app.kubernetes.io/component: cronjob-{{ .name }}
    spec:
      backoffLimit: 0
      template:
        metadata:
          labels:
            app.kubernetes.io/name: {{ include "benji.name" $ }}
            app.kubernetes.io/instance: {{ $.Release.Name }}
            app.kubernetes.io/component: cronjob-{{ .name }}
        spec:
          serviceAccountName: {{ tuple $ "" | include "benji.fullname" }}
          dnsPolicy: ClusterFirstWithHostNet
          restartPolicy: Never
          containers:
            - name: benji
              image: "{{ $.Values.benji.image.registry }}/{{ $.Values.benji.image.repository }}:{{ $.Values.benji.image.tag }}"
              imagePullPolicy: {{ $.Values.benji.image.pullPolicy }}
              securityContext:
                privileged: true
              command:
                {{ toYaml .command | nindent 16 }}
              env:
                - name: POD_NAME
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.name
                - name: POD_NAMESPACE
                  valueFrom:
                    fieldRef:
                      fieldPath: metadata.namespace
                - name: PROM_PUSH_GATEWAY
                  value: "{{- printf "%s-%s" $.Release.Name $.Values.pushgateway.nameOverride | trunc 63 | trimSuffix "-" -}}:9091"
              volumeMounts:
                - name: benji-config
                  mountPath: /benji/etc
                - name: tz-config
                  mountPath: /etc/localtime
                {{ toYaml $.Values.benji.volumeMounts | nindent 16 }}
              resources:
                {{ toYaml $.Values.benji.resources | nindent 16 }}
          hostNetwork: false
          volumes:
            - name: benji-config
              configMap:
                name: {{ tuple $ "" | include "benji.fullname" }}
            - name: tz-config
              hostPath:
                path: /usr/share/zoneinfo/{{ $.Values.timeZone }}
            {{ toYaml $.Values.benji.volumes | nindent 12 }}
    {{- with $.Values.benji.nodeSelector }}
          nodeSelector: {{ toYaml . | nindent 12 }}
    {{- end -}}
    {{- with $.Values.benji.affinity }}
          affinity: {{ toYaml . | nindent 12 }}
    {{- end -}}
    {{- with $.Values.benji.tolerations }}
          tolerations: {{ toYaml . | nindent 12 }}
    {{- end -}}
{{- end -}}
