apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "airflow.fullname" . }}-web
  labels:
    app: {{ template "airflow.name" . }}
    component: web
    chart: {{ template "airflow.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.airflow.webReplicas }}
  minReadySeconds: 120
  strategy:
    # Smooth rolling update of the Web UI
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: {{ template "airflow.name" . }}
      component: web
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap-airflow.yaml") . | sha256sum }}
        configmap.fabric8.io/update-on-change: "{{ template "airflow.fullname" . }}-env"
{{- if .Values.airflow.podAnnotations }}
{{ toYaml .Values.airflow.podAnnotations | indent 8 }}
{{- end }}
      labels:
        app: {{ template "airflow.name" . }}
        component: web
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.airflow.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.airflow.image.pullSecret }}
      {{- end }}
      restartPolicy: Always
      {{- if .Values.dags.initContainer.enabled }}
      initContainers:
        - name: git-clone
          image: {{ .Values.dags.initContainer.image.repository }}:{{ .Values.dags.initContainer.image.tag }} # Any image with git will do
          imagePullPolicy: {{ .Values.dags.initContainer.image.pullPolicy }}
          command:
            - /usr/local/git/git-clone.sh
          args:
            - "{{ .Values.dags.git.url }}"
            - "{{ .Values.dags.git.ref }}"
            - "/dags"
          volumeMounts:
            - name: git-clone
              mountPath: /usr/local/git
            - name: dags-data
              mountPath: /dags
            {{- if .Values.dags.git.secret }}
            - name: git-clone-secret
              mountPath: /keys
            {{- end }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}-web
          image: {{ .Values.airflow.image.repository }}:{{ .Values.airflow.image.tag }}
          imagePullPolicy: {{ .Values.airflow.image.pullPolicy}}
          ports:
            - name: web
              containerPort: 8080
              protocol: TCP
          envFrom:
            - configMapRef:
                name: "{{ template "airflow.fullname" . }}-env"
          env:
            {{- include "airflow.mapenvsecrets" . | indent 10 }}
          volumeMounts:
            - name: scripts
              mountPath: /usr/local/scripts
          {{- if .Values.persistence.enabled }}
            - name: dags-data
              mountPath: {{ .Values.dags.path }}
          {{- else if .Values.dags.initContainer.enabled }}
            - name: dags-data
              mountPath: {{ .Values.dags.path }}
          {{- end }}
          {{- if .Values.logsPersistence.enabled }}
            - name: logs-data
              mountPath: {{ .Values.logs.path }}
          {{- end }}
          {{- range .Values.airflow.extraConfigmapMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
{{- if .Values.airflow.extraVolumeMounts }}
{{ toYaml .Values.airflow.extraVolumeMounts | indent 12 }}
{{- end }}
          args:
            - "bash"
            - "-c"
          {{- if and ( .Values.dags.initContainer.enabled ) ( .Values.dags.initContainer.installRequirements ) }}
            - >
              echo 'waiting 60s...' &&
              sleep 60 &&
              echo 'installing requirements...' &&
              mkdir -p /usr/local/airflow/.local/bin &&
              export PATH=/usr/local/airflow/.local/bin:$PATH &&
              /usr/local/scripts/install-requirements.sh &&
              echo 'executing webserver...' &&
              airflow webserver
          {{- else }}
            - >
              echo 'waiting 60s...' &&
              sleep 60 &&
              mkdir -p /usr/local/airflow/.local/bin &&
              export PATH=/usr/local/airflow/.local/bin:$PATH &&
              echo 'executing webserver...' &&
              airflow webserver
          {{- end }}
          livenessProbe:
            httpGet:
              path: "{{ .Values.ingress.web.path }}/health"
              port: web
            ## Keep 6 minutes the delay to allow clean wait of postgres and redis containers
            initialDelaySeconds: 360
            periodSeconds: 60
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 5
{{- if .Values.airflow.extraContainers }}
{{ toYaml .Values.airflow.extraContainers | indent 8 }}
{{- end }}
      volumes:
        - name: scripts
          configMap:
            name: {{ template "airflow.fullname" . }}-scripts
            defaultMode: 0755
        - name: dags-data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
              claimName: {{ .Values.persistence.existingClaim | default (include "airflow.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if .Values.logsPersistence.enabled }}
        - name: logs-data
          persistentVolumeClaim:
            claimName: {{ .Values.logsPersistence.existingClaim | default (printf "%s-logs" (include "airflow.fullname" . | trunc 58 )) }}
        {{- end }}
        {{- if .Values.dags.initContainer.enabled }}
        - name: git-clone
          configMap:
            name: {{ template "airflow.fullname" . }}-git-clone
            defaultMode: 0755
        {{- if .Values.dags.git.secret }}
        - name: git-clone-secret
          secret:
            secretName: {{ .Values.dags.git.secret }}
            defaultMode: 0700
        {{- end }}
        {{- end }}
        {{- range .Values.airflow.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
        {{- end }}
{{- if .Values.airflow.extraVolumes }}
{{ toYaml .Values.airflow.extraVolumes | indent 8 }}
{{- end }}
