{{- if .Values.openmetadata.config.reindexConfig.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: cron-reindex
  labels: 
    {{- include "OpenMetadata.labels" . | nindent 4 }}
  {{- with .Values.deploymentAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  suspend: true
  failedJobsHistoryLimit: 1  
  successfulJobsHistoryLimit: 1 
  jobTemplate:
    metadata:                    
      name: cron-reindex
    spec:
      template:
        metadata:
          {{- with .Values.podAnnotations }}
          annotations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          labels:
            {{- include "OpenMetadata.labels" . | indent 12 }}
        spec:
          {{- with .Values.imagePullSecrets  }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          serviceAccountName: {{ include "OpenMetadata.serviceAccountName" . }}
          {{- if not (.Values.automountServiceAccountToken) }}
          automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
          {{- end }}
          {{- with .Values.podSecurityContext }}
          securityContext:  
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumes:
          {{- include "tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 12 }}
          containers:
          - name: cron-reindex
            {{- with .Values.securityContext }}
            securityContext:
              {{- toYaml . | nindent 14 }}
            {{- end }}
            image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
            imagePullPolicy: {{ .Values.image.pullPolicy }}
            volumeMounts:
            {{- with .Values.extraVolumeMounts }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
            command: 
            {{ include "OpenMetadata.buildReindexCommand" . | nindent 12 }}
            env:
            {{- include "OpenMetadata.configs" . | nindent 12 }}
            {{- with .Values.extraEnvs }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
            envFrom:
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-config-secret
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-omd-secret
              {{- if .Values.openmetadata.config.database.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-db-secret
              {{- end }}
              {{- if .Values.openmetadata.config.elasticsearch.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-search-secret
              {{- end }}
              {{- if .Values.openmetadata.config.authorizer.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-authorizer-secret
              {{- end }}
              {{- if .Values.openmetadata.config.secretsManager.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-secretsmanager-secret
              {{- end }}
              {{- if .Values.openmetadata.config.web.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-web-secret
              {{- end }}
              {{- if .Values.openmetadata.config.authentication.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-authentication-secret
              {{- end }}
              {{- if .Values.openmetadata.config.eventMonitor.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-eventmonitor-secret
              {{- end }}
              {{- if .Values.openmetadata.config.pipelineServiceClientConfig.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-pipeline-secret
              {{- end }}
              {{- if .Values.openmetadata.config.jwtTokenConfiguration.enabled }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" . }}-jwt-secret
              {{- end }}
              {{- with .Values.openmetadata.config.fernetkey }}
              {{- if not .secretRef }}
              - secretRef:
                  name: {{ include "OpenMetadata.fullname" $ }}-fernetkey-secret
              {{- end }}
              {{- end }}
              {{- with .Values.envFrom }}
                {{- toYaml . | nindent 14 }}
              {{- end }}
            {{- with .Values.resources }}
            resources:
              {{- toYaml . | nindent 14 }}
            {{- end }}
          {{- if .Values.sidecars }}
            {{- include "tplvalues.render" (dict "value" .Values.sidecars "context" $) | nindent 10 }}
          {{- end }}
          {{- with .Values.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}  
          {{- with .Values.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}                     
          restartPolicy: OnFailure
  schedule: "0/5 * * * *"
  {{- if ne .Values.startingDeadlineSeconds nil }}
  startingDeadlineSeconds: {{ .Values.startingDeadlineSeconds }}
  {{- end }}
{{- end }}
