{{/*
 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 Webserver Deployment
#################################
{{- if and .Values.webserver.enabled (semverCompare "<3.0.0" .Values.airflowVersion) }}
{{- $nodeSelector := or .Values.webserver.nodeSelector .Values.nodeSelector }}
{{- $affinity := or .Values.webserver.affinity .Values.affinity }}
{{- $tolerations := or .Values.webserver.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.webserver.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $revisionHistoryLimit := include "airflow.revisionHistoryLimit" (list .Values.webserver.revisionHistoryLimit .Values.revisionHistoryLimit) }}
{{- $securityContext := include "airflowPodSecurityContext" (list .Values.webserver .Values) }}
{{- $containerSecurityContext := include "containerSecurityContext" (list .Values.webserver .Values) }}
{{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list .Values.webserver.waitForMigrations .Values) }}
{{- $containerLifecycleHooks := or .Values.webserver.containerLifecycleHooks .Values.containerLifecycleHooks }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "airflow.fullname" . }}-webserver
  labels:
    tier: airflow
    component: webserver
    release: {{ .Release.Name }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    {{- with .Values.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
  {{- if .Values.webserver.annotations }}
  annotations: {{- toYaml .Values.webserver.annotations | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.webserver.hpa.enabled }}
  replicas: {{ .Values.webserver.replicas }}
  {{- end}}
  {{- if ne $revisionHistoryLimit "" }}
  revisionHistoryLimit: {{ $revisionHistoryLimit }}
  {{- end }}
  strategy:
    {{- if .Values.webserver.strategy }}
    {{- toYaml .Values.webserver.strategy | nindent 4 }}
    {{- else }}
    # Here we define the rolling update strategy
    # - maxSurge define how many pod we can add at a time
    # - maxUnavailable define how many pod can be unavailable
    #   during the rolling update
    # Setting maxUnavailable to 0 would make sure we have the appropriate
    # capacity during the rolling update.
    # You can also use percentage based value instead of integer.
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    {{- end }}
  selector:
    matchLabels:
      tier: airflow
      component: webserver
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        tier: airflow
        component: webserver
        release: {{ .Release.Name }}
        {{- if or .Values.labels .Values.webserver.labels }}
          {{- mustMerge .Values.webserver.labels .Values.labels | toYaml | nindent 8 }}
        {{- end }}
      annotations:
        checksum/metadata-secret: {{ include (print $.Template.BasePath "/secrets/metadata-connection-secret.yaml") . | sha256sum }}
        checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
        checksum/webserver-secret-key: {{ include (print $.Template.BasePath "/secrets/webserver-secret-key-secret.yaml") . | sha256sum }}
        checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
        checksum/webserver-config: {{ include (print $.Template.BasePath "/configmaps/webserver-configmap.yaml") . | sha256sum }}
        checksum/extra-configmaps: {{ include (print $.Template.BasePath "/configmaps/extra-configmaps.yaml") . | sha256sum }}
        checksum/extra-secrets: {{ include (print $.Template.BasePath "/secrets/extra-secrets.yaml") . | sha256sum }}
        {{- if .Values.airflowPodAnnotations }}
          {{- tpl (toYaml .Values.airflowPodAnnotations) . | nindent 8 }}
        {{- end }}
        {{- if .Values.webserver.podAnnotations }}
          {{- tpl (toYaml .Values.webserver.podAnnotations) . | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.webserver.hostAliases }}
      hostAliases: {{- toYaml .Values.webserver.hostAliases | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "webserver.serviceAccountName" . }}
      {{- include "serviceLinks" . | nindent 6 }}
      {{- if .Values.webserver.priorityClassName }}
      priorityClassName: {{ .Values.webserver.priorityClassName }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName }}
      {{- end }}
      nodeSelector: {{- toYaml $nodeSelector | nindent 8 }}
      affinity:
        {{- if $affinity }}
          {{- toYaml $affinity | nindent 8 }}
        {{- else }}
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchLabels:
                  component: webserver
              topologyKey: kubernetes.io/hostname
            weight: 100
        {{- end }}
      tolerations: {{- toYaml $tolerations | nindent 8 }}
      topologySpreadConstraints: {{- toYaml $topologySpreadConstraints | nindent 8 }}
      restartPolicy: Always
      terminationGracePeriodSeconds: {{ .Values.webserver.terminationGracePeriodSeconds }}
      securityContext: {{ $securityContext | nindent 8 }}
      imagePullSecrets: {{- include "image_pull_secrets" . | nindent 8 }}
      initContainers:
        {{- if .Values.webserver.waitForMigrations.enabled }}
        - name: wait-for-airflow-migrations
          resources: {{- toYaml .Values.webserver.resources | nindent 12 }}
          image: {{ template "airflow_image_for_migrations" . }}
          imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
          securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }}
          volumeMounts:
            {{- include "airflow_config_mount" . | nindent 12 }}
            {{- if .Values.volumeMounts }}
              {{- toYaml .Values.volumeMounts | nindent 12 }}
            {{- end }}
            {{- if .Values.webserver.extraVolumeMounts }}
              {{- tpl (toYaml .Values.webserver.extraVolumeMounts) . | nindent 12 }}
            {{- end }}
            {{- if or .Values.webserver.webserverConfig .Values.webserver.webserverConfigConfigMapName }}
              {{- include "airflow_webserver_config_mount" . | nindent 12 }}
            {{- end }}
          args: {{- include "wait-for-migrations-command" . | indent 10 }}
          envFrom: {{- include "custom_airflow_environment_from" . | default "\n  []" | indent 10 }}
          env:
            {{- include "custom_airflow_environment" . | indent 10 }}
            {{- include "standard_airflow_environment" . | indent 10 }}
            {{- if .Values.webserver.waitForMigrations.env }}
              {{- tpl (toYaml .Values.webserver.waitForMigrations.env) $ | nindent 12 }}
            {{- end }}
        {{- end }}
        {{- if .Values.webserver.extraInitContainers }}
          {{- tpl (toYaml .Values.webserver.extraInitContainers) . | nindent 8 }}
        {{- end }}
      containers:
        - name: webserver
          image: {{ template "airflow_image" . }}
          imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
          securityContext: {{ $containerSecurityContext | nindent 12 }}
          {{- if $containerLifecycleHooks  }}
          lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }}
          {{- end }}
          {{- if .Values.webserver.command }}
          command: {{ tpl (toYaml .Values.webserver.command) . | nindent 12 }}
          {{- end }}
          {{- if .Values.webserver.args }}
          args: {{- tpl (toYaml .Values.webserver.args) . | nindent 12 }}
          {{- end }}
          resources: {{- toYaml .Values.webserver.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: {{ include "airflow_pod_template_file" . }}/pod_template_file.yaml
              subPath: pod_template_file.yaml
              readOnly: true
            {{- include "airflow_config_mount" . | nindent 12 }}
            {{- if or .Values.webserver.webserverConfig .Values.webserver.webserverConfigConfigMapName }}
              {{- include "airflow_webserver_config_mount" . | nindent 12 }}
            {{- end }}
            {{- if .Values.logs.persistence.enabled }}
            - name: logs
              mountPath: {{ template "airflow_logs" . }}
              {{- if .Values.logs.persistence.subPath }}
              subPath: {{ .Values.logs.persistence.subPath }}
              {{- end }}
            {{- end }}
            {{- if .Values.volumeMounts }}
              {{- toYaml .Values.volumeMounts | nindent 12 }}
            {{- end }}
            {{- if .Values.webserver.extraVolumeMounts }}
              {{- tpl (toYaml .Values.webserver.extraVolumeMounts) . | nindent 12 }}
            {{- end }}
          ports:
            - name: airflow-ui
              containerPort: {{ .Values.ports.airflowUI }}
          livenessProbe:
            httpGet:
              path: {{ if .Values.config.webserver.base_url }}{{- with urlParse (tpl .Values.config.webserver.base_url .) }}{{ .path }}{{ end }}{{ end }}/health
              port: {{ .Values.ports.airflowUI }}
              {{- if .Values.config.webserver.base_url}}
              httpHeaders:
                - name: Host
                  value: {{ regexReplaceAll ":\\d+$" (urlParse (tpl .Values.config.webserver.base_url .)).host  "" }}
              {{- end }}
              scheme: {{ .Values.webserver.livenessProbe.scheme | default "http" }}
            initialDelaySeconds: {{ .Values.webserver.livenessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.webserver.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.webserver.livenessProbe.failureThreshold }}
            periodSeconds: {{ .Values.webserver.livenessProbe.periodSeconds }}
          readinessProbe:
            httpGet:
              path: {{ if .Values.config.webserver.base_url }}{{- with urlParse (tpl .Values.config.webserver.base_url .) }}{{ .path }}{{ end }}{{ end }}/health
              port: {{ .Values.ports.airflowUI }}
              {{- if .Values.config.webserver.base_url }}
              httpHeaders:
                - name: Host
                  value: {{ regexReplaceAll ":\\d+$" (urlParse (tpl .Values.config.webserver.base_url .)).host  "" }}
              {{- end }}
              scheme: {{ .Values.webserver.readinessProbe.scheme | default "http" }}
            initialDelaySeconds: {{ .Values.webserver.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.webserver.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.webserver.readinessProbe.failureThreshold }}
            periodSeconds: {{ .Values.webserver.readinessProbe.periodSeconds }}
          startupProbe:
            httpGet:
              path: {{ if .Values.config.webserver.base_url }}{{- with urlParse (tpl .Values.config.webserver.base_url .) }}{{ .path }}{{ end }}{{ end }}/health
              port: {{ .Values.ports.airflowUI }}
              {{- if .Values.config.webserver.base_url}}
              httpHeaders:
                - name: Host
                  value: {{ regexReplaceAll ":\\d+$" (urlParse (tpl .Values.config.webserver.base_url .)).host  "" }}
              {{- end }}
              scheme: {{ .Values.webserver.startupProbe.scheme | default "http" }}
            initialDelaySeconds: {{ .Values.webserver.startupProbe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.webserver.startupProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.webserver.startupProbe.failureThreshold }}
            periodSeconds: {{ .Values.webserver.startupProbe.periodSeconds }}
          envFrom: {{- include "custom_airflow_environment_from" . | default "\n  []" | indent 10 }}
          env:
            {{- include "custom_airflow_environment" . | indent 10 }}
            {{- include "standard_airflow_environment" . | indent 10 }}
            {{- include "container_extra_envs" (list . .Values.webserver.env) | indent 10 }}
        {{- if .Values.webserver.extraContainers }}
          {{- tpl (toYaml .Values.webserver.extraContainers) . | nindent 8 }}
        {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ template "airflow_config" . }}
        {{- if or .Values.webserver.webserverConfig .Values.webserver.webserverConfigConfigMapName }}
        - name: webserver-config
          configMap:
            name: {{ template "airflow_webserver_config_configmap_name" . }}
        {{- end }}
        {{- if .Values.logs.persistence.enabled }}
        - name: logs
          persistentVolumeClaim:
            claimName: {{ template "airflow_logs_volume_claim" . }}
        {{- end }}
        {{- if .Values.volumes }}
          {{- toYaml .Values.volumes | nindent 8 }}
        {{- end }}
        {{- if .Values.webserver.extraVolumes }}
          {{- tpl (toYaml .Values.webserver.extraVolumes) . | nindent 8 }}
        {{- end }}
{{- end }}
