{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- $kube := (contains "KubernetesExecutor" .Values.executor) -}}
{{- $isStatefulset := (eq "true" (include "airflow.scheduler.requiresStatefulset" .)) -}}
apiVersion: {{ ternary (include "common.capabilities.statefulset.apiVersion" .) (include "common.capabilities.deployment.apiVersion" .) $isStatefulset }}
kind: {{ ternary "StatefulSet" "Deployment" $isStatefulset }}
metadata:
  name: {{ template "airflow.scheduler.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: scheduler
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | trim | nindent 4 }}
  {{- end }}
spec:
  {{- if $isStatefulset }}
  serviceName: {{ include "airflow.scheduler.serviceName" . }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.scheduler.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: scheduler
  {{- if not .Values.scheduler.autoscaling.hpa.enabled }}
  replicas: {{ .Values.scheduler.replicaCount }}
  {{- end }}
  {{- if .Values.scheduler.updateStrategy }}
  {{ ternary "updateStrategy" "strategy" $isStatefulset }}: {{- toYaml .Values.scheduler.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: scheduler
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/config/configmap.yaml") . | sha256sum }}
        {{- if .Values.scheduler.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "airflow.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.scheduler.automountServiceAccountToken }}
      {{- if .Values.scheduler.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.scheduler.podAffinityPreset "component" "scheduler" "customLabels" $podLabels "topologyKey" .Values.scheduler.topologyKey "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.scheduler.podAntiAffinityPreset "component" "scheduler" "customLabels" $podLabels "topologyKey" .Values.scheduler.topologyKey "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.scheduler.nodeAffinityPreset.type "key" .Values.scheduler.nodeAffinityPreset.key "values" .Values.scheduler.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.scheduler.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.scheduler.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.scheduler.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.scheduler.priorityClassName }}
      priorityClassName: {{ .Values.scheduler.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.scheduler.schedulerName }}
      schedulerName: {{ .Values.scheduler.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "airflow.serviceAccountName" . }}
      {{- if .Values.scheduler.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.scheduler.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- include "airflow.defaultInitContainers.prepareConfig" . | nindent 8 }}
        {{- include "airflow.defaultInitContainers.prepareVenv" . | nindent 8 }}
        {{- if and .Values.dags.enabled }}
        {{- include "airflow.defaultInitContainers.loadDAGs" . | nindent 8 }}
        {{- end }}
        {{- if .Values.plugins.enabled }}
        {{- include "airflow.defaultInitContainers.loadPlugins" . | nindent 8 }}
        {{- end }}
        {{- include "airflow.defaultInitContainers.waitForDBMigrations" . | nindent 8 }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
      containers:
        - name: airflow-scheduler
          image: {{ include "airflow.image" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.scheduler.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.scheduler.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            {{- include "airflow.configure.airflow.common" . | nindent 12 }}
            - name: AIRFLOW_COMPONENT_TYPE
              value: "scheduler"
            - name: AIRFLOW_EXECUTOR
              value: {{ .Values.executor }}
            - name: PYTHONPYCACHEPREFIX
              value: "/opt/bitnami/airflow/venv/tmp"
            {{- if or (contains "CeleryExecutor" .Values.executor) (eq .Values.executor "CeleryKubernetesExecutor") }}
            - name: REDIS_HOST
              value: {{ include "airflow.redis.host" . | quote }}
            - name: REDIS_PORT_NUMBER
              value: {{ include "airflow.redis.port" . | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.scheduler.extraEnvVarsCM .Values.scheduler.extraEnvVarsCMs .Values.scheduler.extraEnvVarsSecret .Values.scheduler.extraEnvVarsSecrets .Values.extraEnvVarsCM .Values.extraEnvVarsCMs .Values.extraEnvVarsSecret .Values.extraEnvVarsSecrets }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.extraEnvVarsCMs }}
            {{- range .Values.extraEnvVarsCMs }}
            - configMapRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsCMs }}
            {{- range .Values.scheduler.extraEnvVarsCMs }}
            - configMapRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.scheduler.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.scheduler.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecrets }}
            {{- range .Values.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.scheduler.extraEnvVarsSecrets }}
            {{- range .Values.scheduler.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.scheduler.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customLivenessProbe "context" $) | trim | nindent 12 }}
          {{- else if .Values.scheduler.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - -f
                - "airflow scheduler"
          {{- end }}
          {{- if .Values.scheduler.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customReadinessProbe "context" $) | trim | nindent 12 }}
          {{- else if .Values.scheduler.readinessProbe.enabled }}
          {{- $readinessTimeout := sub (int .Values.scheduler.readinessProbe.timeoutSeconds) 1 }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  export CONNECTION_CHECK_MAX_COUNT=0
                  timeout {{ $readinessTimeout }} airflow jobs check --job-type SchedulerJob --local --limit 0 {{- if not .Values.diagnosticMode.enabled }} 2>/dev/null {{- end }}
          {{- end }}
          {{- if .Values.scheduler.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.scheduler.startupProbe.enabled }}
          {{- $startupTimeout := sub (int .Values.scheduler.startupProbe.timeoutSeconds) 1 }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.scheduler.startupProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  export CONNECTION_CHECK_MAX_COUNT=0
                  timeout {{ $startupTimeout }} airflow jobs check --job-type SchedulerJob --local --limit 0 {{- if not .Values.diagnosticMode.enabled }} 2>/dev/null {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.scheduler.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.scheduler.resources }}
          resources: {{- toYaml .Values.scheduler.resources | nindent 12 }}
          {{- else if ne .Values.scheduler.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.scheduler.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/nss-wrapper
              subPath: app-nss-wrapper-dir
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/logs
              subPath: app-logs-dir
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/airflow.db
              subPath: app-base-dir/airflow.db
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/airflow.cfg
              subPath: app-base-dir/airflow.cfg
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/config/airflow_local_settings.py
              subPath: app-conf-dir/airflow_local_settings.py
            - name: empty-dir
              mountPath: /opt/bitnami/airflow/venv
              subPath: venv-base-dir
            {{- if  .Values.usePasswordFiles }}
            - name: airflow-secrets
              mountPath: /opt/bitnami/airflow/secrets
            {{- end }}
            {{- if $kube }}
            - name: pod-template
              mountPath: /opt/bitnami/airflow/config/pod_template.yaml
              subPath: pod_template.yaml
            {{- end }}
            {{- if and .Values.dags.enabled }}
            {{- include "airflow.dags.volumeMounts" . | nindent 12 }}
            {{- end }}
            {{- if .Values.plugins.enabled }}
            {{- include "airflow.plugins.volumeMounts" . | nindent 12 }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.scheduler.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if and .Values.dags.enabled }}
        {{- include "airflow.defaultSidecars.syncDAGs" . | nindent 8 }}
        {{- end }}
        {{- if .Values.plugins.enabled }}
        {{- include "airflow.defaultSidecars.syncPlugins" . | nindent 8 }}
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: configuration
          configMap:
            name: {{ include "airflow.configMapName"  . }}
            optional: true
        {{- if .Values.usePasswordFiles }}
        - name: airflow-secrets
          projected:
            sources:
              - secret:
                  name:  {{ include "airflow.secretName" . }}
              - secret:
                  name: {{ include "airflow.database.secretName" . }}
              {{- if or (contains "CeleryExecutor" .Values.executor) (eq .Values.executor "CeleryKubernetesExecutor") }}
              - secret:
                  name: {{ include "airflow.redis.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if $kube }}
        - name: pod-template
          configMap:
            name: {{ printf "%s-pod-template" (include "common.names.fullname" .) }}
        {{- end }}
        {{- if and .Values.dags.enabled }}
        {{- include "airflow.dags.volumes" . | nindent 8 }}
        {{- end }}
        {{- if .Values.plugins.enabled }}
        {{- include "airflow.plugins.volumes" . | nindent 8 }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.scheduler.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.scheduler.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
