{{/*
 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 Database Cleanup CronJob
#################################
{{- if and .Values.databaseCleanup.enabled (or .Values.databaseCleanup.command .Values.databaseCleanup.args) }}
{{- $nodeSelector := or .Values.databaseCleanup.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.databaseCleanup.affinity .Values.affinity }}
{{- $tolerations := or .Values.databaseCleanup.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.databaseCleanup.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $securityContext := include "airflowPodSecurityContext" (list .Values.databaseCleanup .Values) }}
{{- $containerSecurityContext := include "containerSecurityContext" (list .Values.databaseCleanup .Values) }}
{{- $containerLifecycleHooks := or .Values.databaseCleanup.containerLifecycleHooks .Values.containerLifecycleHooks }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "airflow.fullname" . }}-database-cleanup
  labels:
    tier: airflow
    component: database-cleanup
    release: {{ .Release.Name }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    {{- with .Values.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- with .Values.databaseCleanup.jobAnnotations }}
  annotations: {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  schedule: "{{ tpl .Values.databaseCleanup.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.databaseCleanup.failedJobsHistoryLimit nil) }}
  failedJobsHistoryLimit: {{ .Values.databaseCleanup.failedJobsHistoryLimit }}
  {{- end }}
  {{- if not (eq .Values.databaseCleanup.successfulJobsHistoryLimit nil) }}
  successfulJobsHistoryLimit: {{ .Values.databaseCleanup.successfulJobsHistoryLimit }}
  {{- end }}
  jobTemplate:
    spec:
      backoffLimit: 1
      {{- if not (kindIs "invalid" .Values.databaseCleanup.ttlSecondsAfterFinished) }}
      ttlSecondsAfterFinished: {{ .Values.databaseCleanup.ttlSecondsAfterFinished }}
      {{- end }}
      template:
        metadata:
          labels:
            tier: airflow
            component: database-cleanup
            release: {{ .Release.Name }}
            {{- if or .Values.labels .Values.databaseCleanup.labels }}
              {{- mustMerge .Values.databaseCleanup.labels .Values.labels | toYaml | nindent 12 }}
            {{- end }}
          annotations:
            {{- if .Values.airflowPodAnnotations }}
              {{- tpl (toYaml .Values.airflowPodAnnotations) . | nindent 12 }}
            {{- end }}
            {{- if .Values.databaseCleanup.podAnnotations }}
              {{- tpl (toYaml .Values.databaseCleanup.podAnnotations) . | nindent 12 }}
            {{- end }}
        spec:
          restartPolicy: Never
          {{- if .Values.databaseCleanup.priorityClassName }}
          priorityClassName: {{ .Values.databaseCleanup.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 "databaseCleanup.serviceAccountName" . }}
          {{- include "serviceLinks" . | nindent 10 }}
          imagePullSecrets: {{- include "image_pull_secrets" . | nindent 12 }}
          securityContext: {{ $securityContext | nindent 12 }}
          containers:
            - name: database-cleanup
              image: {{ template "airflow_image" . }}
              imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
              securityContext: {{ $containerSecurityContext | nindent 16 }}
              {{- if $containerLifecycleHooks }}
              lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 16 }}
              {{- end }}
              {{- if .Values.databaseCleanup.command }}
              command: {{ tpl (toYaml .Values.databaseCleanup.command) . | nindent 16 }}
              {{- end }}
              {{- if .Values.databaseCleanup.args }}
              args: {{ tpl (toYaml .Values.databaseCleanup.args) . | nindent 16 }}
              {{- end }}
              {{- if .Values.databaseCleanup.applyCustomEnv }}
              envFrom: {{- include "custom_airflow_environment_from" . | default "\n  []" | indent 14 }}
              env: {{- include "custom_airflow_environment" . | indent 14 }}
              {{- else }}
              env:
              {{- end }}
                {{- include "standard_airflow_environment" . | indent 14 }}
                {{- include "container_extra_envs" (list . .Values.databaseCleanup.env) | indent 14 }}
              volumeMounts:
                {{- include "airflow_config_mount" . | nindent 16 }}
                {{- if .Values.volumeMounts }}
                  {{- toYaml .Values.volumeMounts | nindent 16 }}
                {{- end }}
              resources: {{- toYaml .Values.databaseCleanup.resources | nindent 16 }}
          volumes:
            - name: config
              configMap:
                name: {{ template "airflow_config" . }}
            {{- if .Values.volumes }}
              {{- toYaml .Values.volumes | nindent 12 }}
            {{- end }}
{{- end }}
