apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "saberes.fullname" . }}-moodlecron
  labels:
    {{- include "saberes.labels" . | nindent 4 }}
spec:
  schedule: {{ .Values.moodle.cron.schedule | quote }} 
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            {{- include "saberes.labels" . | nindent 12 }}
        spec:
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.podSecurityContext | nindent 12 }}
          containers:
            - name: {{ .Release.Name }}-moodlecron
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              command:
                - "/bin/bash"
                - "-c"
              args:
                - "/usr/bin/php /var/www/html/admin/cli/cron.php"
              env:
                - name: MOODLE_DB_HOST
                  value: {{ .Values.moodle.dbHost | quote }}
                - name: MOODLE_DB_NAME
                  value: {{ .Values.moodle.dbName | quote }}
                - name: MOODLE_DB_PASS
                  value: {{ .Values.moodle.dbPass | quote }}
                - name: MOODLE_DB_USER
                  value: {{ .Values.moodle.dbUser | quote }}
                - name: MOODLE_REVERSEPROXY
                  value: {{ .Values.moodle.reverseProxy | quote }}
                - name: MOODLE_SSLPROXY
                  value: {{ .Values.moodle.sslProxy | quote }}
                - name: MOODLE_URL
                {{- if .Values.ingress.tls.enabled }}
                  value: https://{{ .Values.ingress.hostname }}
                {{- else -}}
                  value: http://{{ .Values.ingress.hostname }}
                {{- end }}
                - name: OSTICKET_API_KEY
                  value: {{ .Values.osticket.apiKey | quote }}
                - name: OSTICKET_API_URL
                  value: {{ .Values.osticket.url | quote }}
              volumeMounts:
                - mountPath: /var/moodledata
                  name: moodledata
              resources:
                {{- toYaml .Values.moodle.cron.resources | nindent 16 }}
          restartPolicy: OnFailure
          volumes:
            - name: moodledata
            {{- if .Values.persistence.enabled }}
              persistentVolumeClaim:
                claimName: {{ include "saberes.fullname" . }}-moodledata
            {{- else }}
              emptyDir: {}
            {{- end }}
          {{- with .Values.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}