{{ $cronjob_values := (. | mustDeepCopy) }}
{{ $_ := set $cronjob_values "common" (dict "nameSuffix" "cronjob") }}

{{ $hostName := .Values.nextcloud.host }}
{{ if .Values.useServiceNameForHost }}
{{ $hostName = (include "common.names.fullname" .) }}
{{ end }}

{{if .Values.cronjob.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ template "common.names.fullname" $cronjob_values }}
  labels:
    app: {{ template "common.names.name" $cronjob_values }}
    chart: {{ template "common.names.chart" $cronjob_values }}
    release: {{ .Release.Name }}
  annotations:
    rollme: {{ randAlphaNum 5 | quote }}
spec:
  schedule: "{{ .Values.cronjob.schedule }}"
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 2
  successfulJobsHistoryLimit: 1
  jobTemplate:
    metadata:
      labels:
        app: {{ template "common.names.name" $cronjob_values }}
        release: {{ .Release.Name }}
        {{- include "common.labels.selectorLabels" $cronjob_values | nindent 8 }}
    spec:
      template:
        metadata:
          labels:
            app: {{ template "common.names.name" $cronjob_values }}
            release: {{ .Release.Name }}
            {{- include "common.labels.selectorLabels" $cronjob_values | nindent 12 }}
        spec:
          restartPolicy: Never
          containers:
            - name: {{ .Chart.Name }}-cronjob
              image: "{{.Values.image.repository }}:{{.Values.image.tag }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              {{- if .Values.certificate }}
              command: [ "curl" ,"-k" , "https://{{ $hostName }}:{{.Values.service.nodePort}}/cron.php"]
              {{- else }}
              command: [ "curl" , "http://{{ $hostName }}:{{.Values.service.nodePort}}/cron.php"]
              {{- end }}
{{ end }}
