apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "airflow.fullname" . }}-scheduler
  labels:
    app: {{ template "airflow.name" . }}
    component: scheduler
    chart: {{ template "airflow.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: 1
  strategy:
    # Kill the scheduler as soon as possible. It will restart quickly with all the workers,
    # minimizing the time they are not synchronized.
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 100%
  selector:
    matchLabels:
      app: {{ template "airflow.name" . }}
      component: scheduler
      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: scheduler
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.airflow.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.airflow.image.pullSecret }}
      {{- end }}
      restartPolicy: Always
      serviceAccountName: {{ template "airflow.serviceAccountName" . }}
      {{- 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 }}-scheduler
          image: {{ .Values.airflow.image.repository }}:{{ .Values.airflow.image.tag }}
          imagePullPolicy: {{ .Values.airflow.image.pullPolicy}}
          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 }}
          {{- if .Values.airflow.connections }}
            - name: connections
              mountPath: /usr/local/connections
          {{- 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 10s...' &&
              sleep 10 &&
              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 initdb" &&
              airflow initdb &&
            {{- if .Values.airflow.connections }}
              echo "adding connections" &&
              /usr/local/connections/add-connections.sh &&
            {{- end }}
              echo "executing scheduler" &&
              airflow scheduler -n {{ .Values.airflow.schedulerNumRuns }}
          {{- else }}
            - >
              echo 'waiting 10s...' &&
              sleep 10 &&
              mkdir -p /usr/local/airflow/.local/bin &&
              export PATH=/usr/local/airflow/.local/bin:$PATH &&
              echo "executing initdb" &&
              airflow initdb &&
            {{- if .Values.airflow.connections }}
              echo "adding connections" &&
              /usr/local/connections/add-connections.sh &&
            {{- end }}
              echo "executing scheduler" &&
              airflow scheduler -n {{ .Values.airflow.schedulerNumRuns }}
          {{- end }}
{{- 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 }}
        {{- if .Values.airflow.connections }}
        - name: connections
          secret:
            secretName: {{ template "airflow.fullname" . }}-connections
            defaultMode: 0755
        {{- end }}
        {{- range .Values.airflow.extraConfigmapMounts }}
        - name: {{ .name }}
          configMap:
            name: {{ .configMap }}
        {{- end }}
{{- if .Values.airflow.extraVolumes }}
{{ toYaml .Values.airflow.extraVolumes | indent 8 }}
{{- end }}
