{{- if .Values.esIndexCleaner.enabled -}}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: {{ include "jaeger.fullname" . }}-es-index-cleaner
  namespace: {{ .Release.Namespace  }}
  labels:
    {{- include "jaeger.labels" . | nindent 4 }}
    app.kubernetes.io/component: es-index-cleaner
{{- if .Values.esIndexCleaner.annotations }}
  annotations:
    {{- toYaml .Values.esIndexCleaner.annotations | nindent 4 }}
{{- end }}
spec:
  concurrencyPolicy: "Forbid"
  schedule: {{ .Values.esIndexCleaner.schedule | quote }}
  successfulJobsHistoryLimit: {{ .Values.esIndexCleaner.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.esIndexCleaner.failedJobsHistoryLimit }}
  suspend: false
  jobTemplate:
    spec:
      template:
        metadata:
          {{- if .Values.esIndexCleaner.podAnnotations }}
          annotations:
            {{- toYaml .Values.esIndexCleaner.podAnnotations | nindent 12 }}
          {{- end }}
          labels:
            {{- include "jaeger.selectorLabels" . | nindent 12 }}
            app.kubernetes.io/component: es-index-cleaner
            {{- if .Values.esIndexCleaner.podLabels }}
            {{- toYaml .Values.esIndexCleaner.podLabels | nindent 12 }}
            {{- end }}
        spec:
          serviceAccountName: {{ template "jaeger.esIndexCleaner.serviceAccountName" . }}
          {{- with .Values.esIndexCleaner.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.esIndexCleaner.podSecurityContext | nindent 12 }}
          containers:
          - name: {{ include "jaeger.fullname" . }}-es-index-cleaner
            securityContext:
              {{- toYaml .Values.esIndexCleaner.securityContext | nindent 14 }}
            image: "{{ .Values.esIndexCleaner.image }}:{{ .Values.esIndexCleaner.tag }}"
            imagePullPolicy: {{ .Values.esIndexCleaner.pullPolicy }}
            args:
              - {{ .Values.esIndexCleaner.numberOfDays | quote }}
              - {{ include "elasticsearch.client.url" . }}
            env:
            {{- if .Values.esIndexCleaner.extraEnv }}
              {{- toYaml .Values.esIndexCleaner.extraEnv | nindent 14 }}
            {{- end }}
              {{ include "elasticsearch.env" . | nindent 14 }}
            resources:
              {{- toYaml .Values.esIndexCleaner.resources | nindent 14 }}
            volumeMounts:
            {{- range .Values.esIndexCleaner.extraConfigmapMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- range .Values.esIndexCleaner.extraSecretMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
          restartPolicy: OnFailure
        {{- with .Values.esIndexCleaner.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- with .Values.esIndexCleaner.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
        {{- end }}
        {{- with .Values.esIndexCleaner.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
        {{- end }}
{{- end -}}
