{{/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
*/}}

################################
## Airflow Cleanup Pods CronJob
#################################
{{- if and .Values.cleanup.enabled (contains "KubernetesExecutor" .Values.executor) }}
{{- $nodeSelector := or .Values.cleanup.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.cleanup.affinity .Values.affinity }}
{{- $tolerations := or .Values.cleanup.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.cleanup.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $securityContext := include "airflowPodSecurityContext" (list .Values.cleanup .Values) }}
{{- $containerSecurityContext := include "containerSecurityContext" (list .Values.cleanup .Values) }}
{{- $containerLifecycleHooks := or .Values.cleanup.containerLifecycleHooks .Values.containerLifecycleHooks }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "airflow.fullname" . }}-cleanup
  labels:
    tier: airflow
    component: airflow-cleanup-pods
    release: {{ .Release.Name }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    {{- with .Values.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.cleanup.jobAnnotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  schedule: "{{ tpl .Values.cleanup.schedule . }}"
  # The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn't finished yet, the cron job skips the new job run
  concurrencyPolicy: Forbid
  {{- if not (eq .Values.cleanup.failedJobsHistoryLimit nil) }}
  failedJobsHistoryLimit: {{ .Values.cleanup.failedJobsHistoryLimit }}
  {{- end }}
  {{- if not (eq .Values.cleanup.successfulJobsHistoryLimit nil) }}
  successfulJobsHistoryLimit: {{ .Values.cleanup.successfulJobsHistoryLimit }}
  {{- end }}
  jobTemplate:
    spec:
      backoffLimit: 1
      template:
        metadata:
          labels:
            tier: airflow
            component: airflow-cleanup-pods
            release: {{ .Release.Name }}
            {{- if or .Values.labels .Values.cleanup.labels }}
              {{- mustMerge .Values.cleanup.labels .Values.labels | toYaml | nindent 12 }}
            {{- end }}
          annotations:
            {{- if .Values.airflowPodAnnotations }}
              {{- tpl (toYaml .Values.airflowPodAnnotations) . | nindent 12 }}
            {{- end }}
            {{- if .Values.cleanup.podAnnotations }}
              {{- tpl (toYaml .Values.cleanup.podAnnotations) . | nindent 12 }}
            {{- end }}
        spec:
          restartPolicy: Never
          {{- if .Values.cleanup.priorityClassName }}
          priorityClassName: {{ .Values.cleanup.priorityClassName }}
          {{- end }}
          nodeSelector: {{- toYaml $nodeSelector | nindent 12 }}
          affinity: {{- toYaml $affinity | nindent 12 }}
          {{- if .Values.schedulerName }}
          schedulerName: {{ .Values.schedulerName }}
          {{- end }}
          tolerations: {{- toYaml $tolerations | nindent 12 }}
          topologySpreadConstraints: {{- toYaml $topologySpreadConstraints | nindent 12 }}
          serviceAccountName: {{ include "cleanup.serviceAccountName" . }}
          {{- include "serviceLinks" . | nindent 10 }}
          imagePullSecrets: {{- include "image_pull_secrets" . | nindent 12 }}
          securityContext: {{ $securityContext | nindent 12 }}
          containers:
            - name: airflow-cleanup-pods
              image: {{ template "airflow_image" . }}
              imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
              securityContext: {{ $containerSecurityContext | nindent 16 }}
              {{- if $containerLifecycleHooks }}
              lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 16 }}
              {{- end }}
              {{- if .Values.cleanup.command }}
              command: {{ tpl (toYaml .Values.cleanup.command) . | nindent 16 }}
              {{- end }}
              {{- if .Values.cleanup.args }}
              args: {{ tpl (toYaml .Values.cleanup.args) . | nindent 16 }}
              {{- end }}
              env:
                {{- include "standard_airflow_environment" . | indent 12 }}
                {{- include "container_extra_envs" (list . .Values.cleanup.env) | indent 12 }}
              volumeMounts:
                {{- include "airflow_config_mount" . | nindent 16 }}
                {{- if .Values.volumeMounts }}
                  {{- toYaml .Values.volumeMounts | nindent 16 }}
                {{- end }}
                {{- include "serviceAccountTokenVolumeMount" (list . .Values.cleanup.serviceAccount)  | nindent 16 }}
              resources: {{- toYaml .Values.cleanup.resources | nindent 16 }}
          volumes:
            - name: config
              configMap:
                name: {{ template "airflow_config" . }}
            {{- if .Values.volumes }}
              {{- toYaml .Values.volumes | nindent 12 }}
            {{- end }}
            {{- include "serviceAccountTokenVolume" (list . .Values.cleanup.serviceAccount) | nindent 12 }}

{{- end }}
