{{- if .Values.curator.enabled }}
apiVersion: {{ template "common.capabilities.cronjob.apiVersion" . }}
kind: CronJob
metadata:
  name: {{ template "elasticsearch.curator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: curator
    ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
    app: curator
    {{- if .Values.curator.cronjob.cronjobLabels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.curator.cronjob.cronjobLabels "context" $) | nindent 4 }}
    {{- end }}
  {{- if .Values.curator.cronjob.cronjobAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.curator.cronjob.cronjobAnnotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  schedule: {{ .Values.curator.cronjob.schedule | quote }}
  {{- if .Values.curator.cronjob.concurrencyPolicy }}
  concurrencyPolicy: {{ .Values.curator.cronjob.concurrencyPolicy }}
  {{- end }}
  {{- if .Values.curator.cronjob.failedJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.curator.cronjob.failedJobsHistoryLimit }}
  {{- end }}
  {{- if .Values.curator.cronjob.successfulJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ .Values.curator.cronjob.successfulJobsHistoryLimit }}
  {{- end }}
  jobTemplate:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: curator
        ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
        app: curator
        {{- if .Values.curator.cronjob.jobLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.curator.cronjob.jobLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.curator.cronjob.jobAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.curator.jobAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      template:
        metadata:
          labels: {{- include "common.labels.standard" . | nindent 12 }}
            app.kubernetes.io/component: curator
            ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
            app: curator
            {{- if .Values.curator.podLabels }}
            {{- include "common.tplvalues.render" (dict "value" .Values.curator.podLabels "context" $) | nindent 8 }}
            {{- end }}
          {{- if .Values.curator.podAnnotations }}
          annotations: {{- include "common.tplvalues.render" (dict "value" .Values.curator.podAnnotations "context" $) | nindent 12 }}
          {{- end }}
        spec:
          {{- include "elasticsearch.imagePullSecrets" . | indent 10 }}
          restartPolicy: {{ .Values.curator.cronjob.jobRestartPolicy }}
          {{- if .Values.curator.priorityClassName }}
          priorityClassName: {{ .Values.curator.priorityClassName | quote }}
          {{- end }}
          {{- if .Values.curator.schedulerName }}
          schedulerName: {{ .Values.curator.schedulerName }}
          {{- end }}
          {{- if .Values.curator.rbac.enabled }}
          serviceAccountName: {{ include "elasticsearch.curator.serviceAccountName" . }}
          {{- end }}
          {{- if .Values.curator.affinity }}
          affinity: {{- include "common.tplvalues.render" (dict "value" .Values.curator.affinity "context" $) | nindent 12 }}
          {{- else }}
          affinity:
            podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.curator.podAffinityPreset "component" "curator" "context" $) | nindent 14 }}
            podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.curator.podAntiAffinityPreset "component" "curator" "context" $) | nindent 14 }}
            nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.curator.nodeAffinityPreset.type "key" .Values.curator.nodeAffinityPreset.key "values" .Values.curator.nodeAffinityPreset.values) | nindent 14 }}
          {{- end }}
          {{- if .Values.curator.nodeSelector }}
          nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.curator.nodeSelector "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.curator.tolerations }}
          tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.curator.tolerations "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.curator.topologySpreadConstraints }}
          topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.curator.topologySpreadConstraints "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.curator.podSecurityContext.enabled }}
          securityContext: {{- omit .Values.curator.podSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.curator.initContainers }}
          initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.curator.initContainers "context" $) | nindent 12 }}
          {{- end }}
          containers:
            - name: {{ template "elasticsearch.curator.fullname" . }}
              image: {{ template "elasticsearch.curator.image" . }}
              imagePullPolicy: {{ .Values.curator.image.pullPolicy | quote }}
              {{- if .Values.curator.containerSecurityContext.enabled }}
              securityContext: {{- omit .Values.curator.containerSecurityContext "enabled" | toYaml | nindent 16 }}
              {{- end }}
              {{- if .Values.curator.command }}
              command: {{- include "common.tplvalues.render" (dict "value" .Values.curator.command "context" $) | nindent 16 }}
              {{- end }}
              {{- if .Values.curator.args }}
              args: {{- include "common.tplvalues.render" (dict "value" .Values.curator.args "context" $) | nindent 16 }}
              {{- else }}
              args:
                {{- if .Values.curator.dryRun }}
                - "--dry-run"
                {{- end }}
                - "--config"
                - "/etc/es-curator/config.yml"
                - "/etc/es-curator/action_file.yml"
              {{- end }}
              env:
                {{- if .Values.curator.extraEnvVars }}
                {{- include "common.tplvalues.render" (dict "value" .Values.curator.extraEnvVars "context" $) | nindent 16 }}
                {{- end }}
              envFrom:
                {{- if .Values.curator.extraEnvVarsCM }}
                - configMapRef:
                    name: {{ include "common.tplvalues.render" (dict "value" .Values.curator.extraEnvVarsCM "context" $) }}
                {{- end }}
                {{- if .Values.curator.extraEnvVarsSecret }}
                - secretRef:
                    name: {{ include "common.tplvalues.render" (dict "value" .Values.curator.extraEnvVarsSecret "context" $) }}
                {{- end }}
              {{- if .Values.curator.resources }}
              resources: {{- toYaml .Values.curator.resources | nindent 16 }}
              {{- end }}
              volumeMounts:
                - name: config-volume
                  mountPath: /etc/es-curator
                {{- if .Values.curator.extraVolumeMounts }}
                {{- include "common.tplvalues.render" (dict "value" .Values.curator.extraVolumeMounts "context" $) | nindent 16 }}
                {{- end }}
            {{- if .Values.curator.sidecars }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.curator.sidecars "context" $) | nindent 12 }}
            {{- end }}
          volumes:
            - name: config-volume
              configMap:
                name: {{ template "elasticsearch.curator.fullname" . }}
            {{- if .Values.curator.extraVolumes }}
            {{- include "common.tplvalues.render" (dict "value" .Values.curator.extraVolumes "context" $) | nindent 12 }}
            {{- end }}
{{- end }}
