{{- if .Values.esRollover.enabled -}}
apiVersion: {{ include "common.capabilities.cronjob.apiVersion" $ }}
kind: CronJob
metadata:
  name: {{ include "jaeger.fullname" . }}-es-rollover
  labels:
    {{- include "jaeger.labels" . | nindent 4 }}
    app.kubernetes.io/component: es-rollover
{{- if .Values.esRollover.annotations }}
  annotations:
    {{- toYaml .Values.esRollover.annotations | nindent 4 }}
{{- end }}
spec:
  concurrencyPolicy: {{ .Values.esRollover.concurrencyPolicy }}
  schedule: {{ .Values.esRollover.schedule | quote }}
  successfulJobsHistoryLimit: {{ .Values.esRollover.successfulJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ .Values.esRollover.failedJobsHistoryLimit }}
  suspend: false
  jobTemplate:
    spec:
      {{- if .Values.esRollover.activeDeadlineSeconds }}
      activeDeadlineSeconds: {{ .Values.esRollover.activeDeadlineSeconds }}
      {{- end }}
      {{- if .Values.esRollover.ttlSecondsAfterFinished }}
      ttlSecondsAfterFinished: {{ .Values.esRollover.ttlSecondsAfterFinished }}
      {{- end }}
      template:
        metadata:
          {{- if .Values.esRollover.podAnnotations }}
          annotations:
            {{- toYaml .Values.esRollover.podAnnotations | nindent 12 }}
          {{- end }}
          labels:
            {{- include "jaeger.selectorLabels" . | nindent 12 }}
            app.kubernetes.io/component: es-rollover
            {{- if .Values.esRollover.podLabels }}
            {{- toYaml .Values.esRollover.podLabels | nindent 12 }}
            {{- end }}
        spec:
          serviceAccountName: {{ template "jaeger.esRollover.serviceAccountName" . }}
          {{- with .Values.esRollover.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.esRollover.podSecurityContext | nindent 12 }}
          restartPolicy: OnFailure
          {{- with .Values.esRollover.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.esRollover.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.esRollover.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}

          containers:
          - name: {{ include "jaeger.fullname" . }}-es-rollover
            securityContext:
              {{- toYaml .Values.esRollover.securityContext | nindent 14 }}
            image: "{{ .Values.esRollover.image }}:{{- include "jaeger.image.tag" . }}"
            imagePullPolicy: {{ .Values.esRollover.pullPolicy }}
            args:
              - rollover
              - {{ include "elasticsearch.client.url" . }}
              {{ include "extra.cmdArgs" ( dict "cmdlineParams" .Values.esRollover.cmdlineParams ) | nindent 14  }}
            env:
              {{ include "elasticsearch.env" . | nindent 14 }}
              {{- if .Values.esRollover.extraEnv }}
                {{- toYaml .Values.esRollover.extraEnv | nindent 14 }}
              {{- end }}
            resources:
              {{- toYaml .Values.esRollover.resources | nindent 14 }}
            volumeMounts:
            {{- range .Values.esRollover.extraConfigmapMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- range .Values.esRollover.extraSecretMounts }}
              - name: {{ .name }}
                mountPath: {{ .mountPath }}
                subPath: {{ .subPath }}
                readOnly: {{ .readOnly }}
            {{- end }}
            {{- if .Values.storage.elasticsearch.tls.enabled }}
              - name: {{ .Values.storage.elasticsearch.tls.secretName }}
                mountPath: "/es-tls/ca-cert.pem"
                subPath: "ca-cert.pem"
                readOnly: true
            {{- end }}
          volumes:
          {{- range .Values.esRollover.extraConfigmapMounts }}
            - name: {{ .name }}
              configMap:
                name: {{ .configMap }}
          {{- end }}
          {{- range .Values.esRollover.extraSecretMounts }}
            - name: {{ .name }}
              secret:
                secretName: {{ .secretName }}
        {{- end }}
        {{- if .Values.storage.elasticsearch.tls.enabled }}
            - name: {{ .Values.storage.elasticsearch.tls.secretName }}
              secret:
                secretName: {{ .Values.storage.elasticsearch.tls.secretName }}
        {{- end }}
{{- end -}}
