{{- if .Values.cronjob.enabled }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ template "nextcloud.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "nextcloud.name" . }}
    helm.sh/chart: {{ include "nextcloud.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  annotations:
{{ toYaml .Values.cronjob.annotations | indent 4 }}
spec:
  schedule: "{{ .Values.cronjob.schedule }}"
  concurrencyPolicy: Forbid
  {{- with .Values.cronjob.failedJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ . }}
  {{- end }}
  {{- with .Values.cronjob.successfulJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ . }}
  {{- end }}
  jobTemplate:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "nextcloud.name" . }}
        app.kubernetes.io/managed-by: {{ .Release.Service }}
    spec:
      template:
        metadata:
          labels:
            app.kubernetes.io/name: {{ include "nextcloud.name" . }}
            app.kubernetes.io/managed-by: {{ .Release.Service }}
        spec:
          restartPolicy: Never
          {{- if (default .Values.image.pullSecrets .Values.cronjob.image.pullSecrets) }}
          imagePullSecrets:
          {{- range (default .Values.image.pullSecrets .Values.cronjob.image.pullSecrets) }}
            - name: {{ . }}
          {{- end }}
          {{- end }}
          containers:
            - name: {{ .Chart.Name }}
              image: "{{ default .Values.image.repository .Values.cronjob.image.repository }}:{{ default .Values.image.tag .Values.cronjob.image.tag }}"
              imagePullPolicy: {{ default .Values.image.pullPolicy .Values.cronjob.image.pullPolicy }}
              command: [ "curl" ]
              args:
              {{- if .Values.cronjob.curlInsecure }}
                - "-k"
              {{- end }}
                - "--fail"
                - "-L"
              {{- if .Values.ingress.tls }}
                - "https://{{ .Values.nextcloud.host }}/cron.php"
              {{- else }}
                - "http://{{ .Values.nextcloud.host }}/cron.php"
              {{- end }}
              resources:
{{ toYaml (default .Values.resources .Values.cronjob.resources) | indent 16 }}
    {{- with (default .Values.nodeSelector .Values.cronjob.nodeSelector) }}
          nodeSelector:
{{ toYaml . | indent 12 }}
    {{- end }}
    {{- with (default .Values.affinity .Values.cronjob.affinity) }}
          affinity:
{{ toYaml . | indent 12 }}
    {{- end }}
    {{- with (default .Values.tolerations .Values.cronjob.tolerations) }}
          tolerations:
{{ toYaml . | indent 12 }}:
    {{- end }}
{{- end }}
