{{- if .Capabilities.APIVersions.Has "batch/v1" }}
apiVersion: batch/v1
{{- else }}
apiVersion: batch/v1beta1
{{- end }}
kind: CronJob
metadata:
  name: {{ .Values.kubernetesConnector.cronJobName }}
  {{- if .Values.namespace }}
  namespace: {{ toYaml .Values.namespace }}
  {{- end }}
  annotations:
  {{- include "custom.annotations" $ | indent 4 }}
  labels:
  {{- include "configseeder.labels" $ | indent 4 }}
  {{- include "custom.labels" $ | indent 4 }}
spec:
  schedule: {{ toYaml .Values.jobConfig.schedule }}
  # Don't execute Job more than once at the same time
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: {{ .Values.jobConfig.failedJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ .Values.jobConfig.successfulJobsHistoryLimit }}
  jobTemplate:
    spec:
      # Don't do any retries
      backoffLimit: 0
      template:
        metadata:
          annotations:
          {{- include "custom.annotations" $ | indent 12 }}
          labels:
            {{- include "configseeder.labels" $ | indent 12 }}
            {{- include "custom.labels" $ | indent 12 }}
        spec:
          # Don't restart if an error occures
          restartPolicy: Never
          containers:
            - name: kubernetes-connector
              image: {{ .Values.image.registry }}/kubernetes-connector:{{ .Values.image.tag }}
              imagePullPolicy: Always
              env:
                - name: CONNECTOR_APIKEY_DISABLED
                  value: {{ toYaml .Values.kubernetesConnector.apikey.disabled }}
                - name: CONNECTOR_APIKEY_LIFETIMEINDAYS
                  value: {{ toYaml .Values.kubernetesConnector.apikey.lifetimeInDays }}
                - name: CONNECTOR_APIKEY_RECREATEDAYSBEFOREEXPIRATION
                  value: {{ toYaml .Values.kubernetesConnector.apikey.recreateDaysBeforeExpiration }}
                - name: CONNECTOR_CONFIGMAP_DISABLED
                  value: {{ toYaml .Values.kubernetesConnector.configmap.disabled }}
                - name: CONNECTOR_CONFIGSEEDER_SERVERURL
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.serverUrl }}
                - name: CONNECTOR_CONFIGSEEDER_TENANTKEY
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.tenantKey }}
                - name: CONNECTOR_CONFIGSEEDER_ENVIRONMENTKEY
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.environmentKey }}
                - name: CONNECTOR_CONFIGSEEDER_CONFIGURATIONGROUPKEYS
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.configurationGroupKeys }}
                - name: CONNECTOR_CONFIGSEEDER_CONNECTIONTIMEOUT
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.connectionTimeout }}
                - name: CONNECTOR_CONFIGSEEDER_READTIMEOUT
                  value: {{ toYaml .Values.kubernetesConnector.configseeder.readTimeout }}
                - name: CONNECTOR_LOG_LEVEL
                  value: {{ toYaml .Values.kubernetesConnector.logLevel }}
                - name: CONNECTOR_SECRET_DISABLED
                  value: {{ toYaml .Values.kubernetesConnector.secret.disabled }}
                - name: CONNECTOR_STARTUPRETRYWAITTIME
                  value: {{ toYaml .Values.kubernetesConnector.startupRetryWaitTime }}
                - name: CONNECTOR_STATEMANAGER_NAMESPACE
                  value: {{ .Values.kubernetesConnector.state.namespace | default .Release.Namespace | quote }}
                - name: CONNECTOR_STATEMANAGER_NAME
                  value: {{ toYaml .Values.kubernetesConnector.state.name }}
                - name: CONNECTOR_CONFIGSEEDER_APIKEY
                  valueFrom:
                    secretKeyRef:
                      name: {{ .Values.kubernetesConnector.apiKeySecretName }}
                      key: apiKey
                {{- if .Values.injectCertificates.required }}
                - name: CONNECTOR_CONFIGSEEDER_CERTIFICATEFILENAME
                  value: /certificates.pem
                {{- end }}
              {{- if .Values.injectCertificates.required }}
              volumeMounts:
              - mountPath: /certificates.pem
                name: certificates
                subPath: certificates.pem
              {{- end }}
              resources:
                limits:
                  cpu: {{ .Values.resource.cpuLimit }}
                  memory: {{ .Values.resource.memory }}
                requests:
                  cpu: {{ .Values.resource.cpuRequest }}
                  memory: {{ .Values.resource.memory }}

          {{- if .Values.image.pullSecret }}
          imagePullSecrets:
            - name: {{ toYaml .Values.image.pullSecret }}
          {{- end }}
          serviceAccountName: {{ .Values.serviceAccount.name }}
          {{- if .Values.securityContext }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          {{- else }}
          securityContext: {}
          {{- end }}
          {{- if .Values.injectCertificates.required }}
          volumes:
          - name: certificates
            configMap:
              defaultMode: 420
              name: {{ .Values.injectCertificates.name | quote }}
              optional: true
          {{- end }}