{{- if .Values.global.enterpriseEditionLicense }}
{{- $context := . }}
{{- range $key, $job := .Values.apiCrons }}
{{- with $context }}
---
{{- $kubeTargetVersion := .Capabilities.KubeVersion.GitVersion }}
{{- if semverCompare ">=1.22.0-0" $kubeTargetVersion }}
apiVersion: batch/v1
{{- else }}
apiVersion: batch/v1beta1
{{- end }}
kind: CronJob
metadata:
  name: {{$key | kebabcase}}-cron
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "utilities.labels" . | nindent 4 }}
spec:
  schedule: "{{ $job.cron }}"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 1
  jobTemplate:
    spec:
      backoffLimit: 0 # Don't restart the failed jobs
      template:
        metadata:
          {{- with .Values.podAnnotations }}
          annotations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        spec:
          restartPolicy: Never
          {{- with .Values.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          containers:
          - name: report-cron
            image: "{{ tpl $job.image.repository . }}:{{ $job.image.tag | default .Chart.AppVersion }}-ee"
            imagePullPolicy: "{{ $job.image.pullPolicy}}"
            env:
              {{- range $key, $val := .Values.global.env }}
              - name: {{ $key }}
                value: '{{ $val }}'
              {{- end }}
              - name: LICENSE_KEY
                value: '{{ .Values.global.enterpriseEditionLicense }}'
              - name: version_number
                value: '{{ .Chart.AppVersion }}'
              - name: pg_host
                value: '{{ .Values.global.postgresql.postgresqlHost }}'
              - name: pg_port
                value: "5432"
              - name: pg_dbname
                value: "{{ .Values.global.postgresql.postgresqlDatabase }}"
              - name: pg_user
                value: '{{ .Values.global.postgresql.postgresqlUser }}'
              - name: pg_password
                {{- if .Values.global.postgresql.existingSecret }}
                valueFrom:
                  secretKeyRef:
                    name: {{ .Values.global.postgresql.existingSecret }}
                    key: postgresql-postgres-password
                {{- else }}
                value: '{{ .Values.global.postgresql.postgresqlPassword }}'
                {{- end}}
              - name: SITE_URL
                value: '{{- include "openreplay.domainURL" . }}'
              - name: S3_HOST
                value: '{{- include "openreplay.s3Endpoint" . }}'
              - name: S3_KEY
                {{- if .Values.global.s3.existingSecret }}
                valueFrom:
                  secretKeyRef:
                    name: {{ .Values.global.s3.existingSecret }}
                    key: access-key
                {{- else }}
                value: {{ .Values.global.s3.accessKey }}
                {{- end }}
              - name: S3_SECRET
                {{- if .Values.global.s3.existingSecret }}
                valueFrom:
                  secretKeyRef:
                    name: {{ .Values.global.s3.existingSecret }}
                    key: secret-key
                {{- else }}
                value: {{ .Values.global.s3.secretKey }}
                {{- end }}
              - name: AWS_DEFAULT_REGION
                value: '{{ .Values.global.s3.region }}'
              - name: sessions_region
                value: '{{ .Values.global.s3.region }}' 
              - name: sessions_bucket
                value: {{ .Values.global.s3.recordingsBucket }}
              - name: sourcemaps_bucket
                value: {{ .Values.global.s3.sourcemapsBucket }} 
              - name: js_cache_bucket
                value: {{ .Values.global.s3.assetsBucket }} 
              - name: EMAIL_HOST
                value: '{{ .Values.global.email.emailHost }}'
              - name: EMAIL_PORT
                value: '{{ .Values.global.email.emailPort }}'
              - name: EMAIL_USER
                value: '{{ .Values.global.email.emailUser }}'
              - name: EMAIL_PASSWORD
                value: '{{ .Values.global.email.emailPassword }}'
              - name: EMAIL_USE_TLS
                value: '{{ .Values.global.email.emailUseTls }}'
              - name: EMAIL_USE_SSL
                value: '{{ .Values.global.email.emailUseSsl }}'
              - name: EMAIL_SSL_KEY
                value: '{{ .Values.global.email.emailSslKey }}'
              - name: EMAIL_SSL_CERT
                value: '{{ .Values.global.email.emailSslCert }}'
              - name: EMAIL_FROM
                value: '{{ .Values.global.email.emailFrom }}'
              {{- range $key, $val := $job.env }}
              - name: {{ $key }}
                value: '{{ $val }}'
              {{- end}}
{{- end}}
{{- end}}
{{- end}}
