{{- if $.Values.curator.enabled }}
{{- range $kind, $enabled := $.Values.curator.hooks }}
{{- if $enabled }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ printf "%s-curator-on-%s" (include "elasticsearch.curator.fullname" .) $kind }}
  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.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.curator.jobLabels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.curator.jobLabels "context" $) | nindent 8 }}
    {{- end }}
  annotations:
    "helm.sh/hook": post-{{ $kind }}
    "helm.sh/hook-weight": "1"
    {{- if .Values.curator.jobAnnotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.curator.jobAnnotations "context" $) | nindent 12 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  template:
    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.podAnnotations }}
      annotations: {{- toYaml $.Values.curator.podAnnotations | nindent 8 }}
      {{- end }}
    spec:
      {{- include "elasticsearch.imagePullSecrets" . | indent 6 }}
      restartPolicy: Never
      {{- 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 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.curator.podAffinityPreset "component" "curator" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.curator.podAntiAffinityPreset "component" "curator" "context" $) | nindent 10 }}
        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 8 }}
      {{- end }}
      {{- if $.Values.curator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" $.Values.curator.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.curator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" $.Values.curator.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.curator.podSecurityContext.enabled }}
      securityContext: {{- omit $.Values.curator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if $.Values.curator.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" $.Values.curator.initContainers "context" $) | nindent 8 }}
      {{- 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 12 }}
          {{- end }}
          {{- if $.Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" $.Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if $.Values.curator.command }}
          command: {{ toYaml $.Values.curator.command | nindent 12 }}
          {{- end }}
          {{- if $.Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" $.Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if $.Values.curator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" $.Values.curator.args "context" $) | nindent 12 }}
          {{- 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 12 }}
            {{- 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 }}
          resources: {{- toYaml $.Values.curator.resources | nindent 12 }}
          volumeMounts:
            - name: config-volume
              mountPath: /etc/es-curator
           {{- if $.Values.curator.extraVolumeMounts }}
           {{- toYaml $.Values.curator.extraVolumeMounts | nindent 12 }}
           {{- 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 }}
        {{- toYaml $.Values.curator.extraVolumes | nindent 8 }}
        {{- end }}
{{- end }}
{{- end }}
{{- end }}
