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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "airflow.web.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: web
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.web.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: web
  {{- if not .Values.web.autoscaling.hpa.enabled }}
  replicas: {{ .Values.web.replicaCount }}
  {{- end }}
  {{- if .Values.web.updateStrategy }}
  strategy: {{- toYaml .Values.web.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: web
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/config/configmap.yaml") . | sha256sum }}
        checksum/webserver-configmap: {{ include (print $.Template.BasePath "/web/configmap.yaml") . | sha256sum }}
        {{- if .Values.web.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "airflow.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.web.automountServiceAccountToken }}
      {{- if .Values.web.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.web.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.web.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.web.podAffinityPreset "component" "web" "customLabels" $podLabels "topologyKey" .Values.web.topologyKey "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.web.podAntiAffinityPreset "component" "web" "customLabels" $podLabels "topologyKey" .Values.web.topologyKey "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.web.nodeAffinityPreset.type "key" .Values.web.nodeAffinityPreset.key "values" .Values.web.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.web.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.web.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.web.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.web.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.web.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.web.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.priorityClassName }}
      priorityClassName: {{ .Values.web.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.web.schedulerName }}
      schedulerName: {{ .Values.web.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "airflow.serviceAccountName" . }}
      {{- if .Values.web.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.web.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- include "airflow.defaultInitContainers.prepareConfig" . | nindent 8 }}
        {{- include "airflow.defaultInitContainers.prepareWebConfig" . | nindent 8 }}
        {{- include "airflow.defaultInitContainers.prepareVenv" . | nindent 8 }}
        {{- if .Values.dags.enabled }}
        {{- include "airflow.defaultInitContainers.loadDAGs" . | nindent 8 }}
        {{- end }}
        {{- if .Values.plugins.enabled }}
        {{- include "airflow.defaultInitContainers.loadPlugins" . | nindent 8 }}
        {{- end }}
        {{- if .Values.setupDBJob.enabled }}
        {{- include "airflow.defaultInitContainers.waitForDBMigrations" . | nindent 8 }}
        {{- end }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
        {{- if .Values.web.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
      containers:
        - name: airflow-web
          image: {{ include "airflow.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.web.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.web.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.web.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.web.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.web.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.web.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            {{- include "airflow.configure.airflow.common"  . | nindent 12 }}
            - name: AIRFLOW_SKIP_DB_SETUP
              value: {{ ternary "yes" "no" .Values.setupDBJob.enabled | quote }}
            {{- if not .Values.setupDBJob.enabled }}
            - name: AIRFLOW_USERNAME
              value: {{ .Values.auth.username }}
            {{- if .Values.usePasswordFiles }}
            - name: AIRFLOW_PASSWORD_FILE
              value: "/opt/bitnami/airflow/secrets/airflow-password"
            {{- else }}
            - name: AIRFLOW_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "airflow.secretName" . }}
                  key: airflow-password
            {{- end }}
            {{- end }}
            - name: PYTHONPYCACHEPREFIX
              value: "/opt/bitnami/airflow/venv/tmp"
            {{- if (include "airflow.isImageMajorVersion2" .) }}
            - name: AIRFLOW_COMPONENT_TYPE
              value: "webserver"
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.web.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.web.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.web.extraEnvVarsCM .Values.web.extraEnvVarsCMs .Values.web.extraEnvVarsSecret .Values.web.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.web.extraEnvVarsCMs }}
            {{- range .Values.web.extraEnvVarsCMs }}
            - configMapRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.web.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.web.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.web.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.web.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecrets }}
            {{- range .Values.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.web.extraEnvVarsSecrets }}
            {{- range .Values.web.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.web.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.web.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.web.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - -f
                - airflow
          {{- end }}
          {{- if .Values.web.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.readinessProbe "enabled") "context" $) | nindent 12 }}
            {{- if contains "127.0.0.1" (include "airflow.baseUrl" .) }}
            httpGet:
              {{- if (include "airflow.isImageMajorVersion3" .) }}
              path: /api/v2/monitor/health
              {{- else }}
              path: /health
              {{- end }}
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.web.tls.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.web.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.web.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.web.resources }}
          resources: {{- toYaml .Values.web.resources | nindent 12 }}
          {{- else if ne .Values.web.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.web.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/webserver_config.py
              subPath: app-base-dir/webserver_config.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 and .Values.ldap.enabled .Values.ldap.tls.enabled }}
            - name: airflow-ldap-ca-certificate
              mountPath: {{ .Values.ldap.tls.certificatesMountPath }}
              readOnly: true
            {{- end }}
            {{- if .Values.web.tls.enabled }}
            - name: tls-certificates
              mountPath: /opt/bitnami/airflow/certs
              readOnly: true
            {{- end }}
            {{- if .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.web.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.web.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.web.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: configuration
          configMap:
            name: {{ include "airflow.configMapName"  . }}
            optional: true
        - name: webserver-configuration
          configMap:
            name: {{ include "airflow.web.configMapName" . }}
        {{- if .Values.usePasswordFiles }}
        - name: airflow-secrets
          projected:
            sources:
              - secret:
                  name: {{ include "airflow.secretName" . }}
              - secret:
                  name: {{ include "airflow.database.secretName" . }}
              {{- if .Values.ldap.enabled }}
              - secret:
                  name: {{ include "airflow.ldap.secretName" . }}
              {{- end }}
              {{- if or (contains "CeleryExecutor" .Values.executor) (eq .Values.executor "CeleryKubernetesExecutor") }}
              - secret:
                  name: {{ include "airflow.redis.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if .Values.dags.enabled }}
        {{- include "airflow.dags.volumes" . | nindent 8 }}
        {{- end }}
        {{- if .Values.plugins.enabled }}
        {{- include "airflow.plugins.volumes" . | nindent 8 }}
        {{- end }}
        {{- if and .Values.ldap.enabled .Values.ldap.tls.enabled }}
        - name: airflow-ldap-ca-certificate
          secret:
            secretName: {{ required "A secret containing the LDAP CA certificate. It is required when SSL in enabled" .Values.ldap.tls.certificatesSecret }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.web.tls.enabled }}
        - name: tls-certificates
          secret:
            secretName: {{ template "airflow.web.tls.secretName" . }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.web.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
