{{- if eq .Values.kind "CronJob" }}
apiVersion: {{ .Values.cronJobApiVersion | default "batch/v1" }}
kind: CronJob
metadata:
  name: {{ template "descheduler.fullname" . }}
  namespace: {{ include "descheduler.namespace" . }}
  {{- if .Values.cronJobAnnotations }}
  annotations:
    {{- .Values.cronJobAnnotations | toYaml | nindent 4 }}
  {{- end }}
  labels:
    {{- include "descheduler.labels" . | nindent 4 }}
    {{- if .Values.cronJobLabels }}
    {{- .Values.cronJobLabels | toYaml | nindent 4 }}
    {{- end }}
spec:
  schedule: {{ .Values.schedule | quote }}
  {{- if .Values.suspend }}
  suspend: {{ .Values.suspend }}
  {{- end }}
  concurrencyPolicy: "Forbid"
  {{- if .Values.startingDeadlineSeconds }}
  startingDeadlineSeconds: {{ .Values.startingDeadlineSeconds }}
  {{- end }}
  {{- if ne .Values.successfulJobsHistoryLimit nil }}
  successfulJobsHistoryLimit: {{ .Values.successfulJobsHistoryLimit }}
  {{- end }}
  {{- if ne .Values.failedJobsHistoryLimit nil }}
  failedJobsHistoryLimit: {{ .Values.failedJobsHistoryLimit }}
  {{- end }}
  {{- if .Values.timeZone }}
  timeZone: {{ .Values.timeZone }}
  {{- end }}
  jobTemplate:
    {{- if or .Values.jobAnnotations .Values.jobLabels }}
    metadata:
      {{- if .Values.jobAnnotations }}
      annotations:
        {{- .Values.jobAnnotations | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.jobLabels }}
      labels:
        {{- .Values.jobLabels | toYaml | nindent 8 }}
      {{- end }}
    {{- end }}
    spec:
      {{- if .Values.ttlSecondsAfterFinished }}
      ttlSecondsAfterFinished: {{ .Values.ttlSecondsAfterFinished }}
      {{- end }}
      {{- if .Values.activeDeadlineSeconds }}
      activeDeadlineSeconds: {{ .Values.activeDeadlineSeconds }}
      {{- end }}
      template:
        metadata:
          name: {{ template "descheduler.fullname" . }}
          annotations:
            checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
            {{- if .Values.podAnnotations }}
            {{- .Values.podAnnotations | toYaml | nindent 12 }}
            {{- end }}
          labels:
            {{- include "descheduler.selectorLabels" . | nindent 12 }}
            {{- if .Values.podLabels }}
            {{- .Values.podLabels | toYaml | nindent 12 }}
            {{- end }}
        spec:
          {{- with .Values.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.topologySpreadConstraints }}
          topologySpreadConstraints:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.dnsConfig }}
          dnsConfig:
            {{- .Values.dnsConfig | toYaml | nindent 12 }}
          {{- end }}
          {{- with .Values.tolerations }}
          tolerations:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.priorityClassName }}
          priorityClassName: {{ .Values.priorityClassName }}
          {{- end }}
          serviceAccountName: {{ template "descheduler.serviceAccountName" . }}
          {{- if kindIs "bool" .Values.automountServiceAccountToken }}
          automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
          {{- end }}
          restartPolicy: "Never"
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
          {{- toYaml . | nindent 10 }}
          {{- end }}
          {{- with .Values.initContainers }}
          initContainers:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          containers:
            - name: {{ .Chart.Name }}
              image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default (printf "v%s" .Chart.AppVersion) }}"
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              command:
                {{- toYaml .Values.command | nindent 16 }}
              args:
                - --policy-config-file=/policy-dir/policy.yaml
                {{- range $key, $value := .Values.cmdOptions }}
                {{- if ne $value nil }}
                - {{ printf "--%s=%s" $key (toString $value) }}
                {{- else }}
                - {{ printf "--%s" $key }}
                {{- end }}
                {{- end }}
              livenessProbe:
                {{- toYaml .Values.livenessProbe | nindent 16 }}
              ports:
                {{- toYaml .Values.ports | nindent 16 }}
              resources:
                {{- toYaml .Values.resources | nindent 16 }}
              {{- if .Values.securityContext }}
              securityContext:
                {{- toYaml .Values.securityContext | nindent 16 }}
              {{- end }}
              volumeMounts:
                - mountPath: /policy-dir
                  name: policy-volume
                {{- if and .Values.extraServiceAccountVolumeMounts (not .Values.automountServiceAccountToken) }}
                  {{ toYaml .Values.extraServiceAccountVolumeMounts | nindent 16 }}
                {{- end }}
          {{- if .Values.podSecurityContext }}
          securityContext:
            {{- toYaml .Values.podSecurityContext | nindent 12 }}
          {{- end }}
          volumes:
          - name: policy-volume
            configMap:
              name: {{ template "descheduler.fullname" . }}
          {{- if and .Values.extraServiceAccountVolumes (not .Values.automountServiceAccountToken) }}
            {{ toYaml .Values.extraServiceAccountVolumes | nindent 10 }}
          {{- end }}
{{- end }}
