{{- if .Values.workers.enabled }}
## A StatefulSet is used to give workers consistent names for DNS,
## allowing the web server to access the log files.
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "airflow.fullname" . }}-worker
  {{- if .Values.workers.annotations }}
  annotations:
    {{- toYaml .Values.workers.annotations | nindent 4 }}
  {{- end }}
  labels:
    app: {{ include "airflow.labels.app" . }}
    component: worker
    chart: {{ include "airflow.labels.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- if .Values.workers.labels }}
    {{- toYaml .Values.workers.labels | nindent 4 }}
    {{- end }}
spec:
  serviceName: "{{ include "airflow.fullname" . }}-worker"
  replicas: {{ .Values.workers.replicas }}
  updateStrategy:
    type: RollingUpdate
  ## we do not need to guarantee the order in which workers are scaled
  podManagementPolicy: Parallel
  selector:
    matchLabels:
      app: {{ include "airflow.labels.app" . }}
      component: worker
      release: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        checksum/config-env: {{ include (print $.Template.BasePath "/config/configmap-env.yaml") . | sha256sum }}
        checksum/config-git-clone: {{ include (print $.Template.BasePath "/config/configmap-scripts-git.yaml") . | sha256sum }}
        checksum/config-scripts: {{ include (print $.Template.BasePath "/config/configmap-scripts.yaml") . | sha256sum }}
        {{- if and (.Values.dags.git.url) (.Values.dags.git.ref) }}
        checksum/dags-git-ref: {{ .Values.dags.git.ref | sha256sum }}
        {{- end }}
        {{- if .Values.airflow.podAnnotations }}
        {{- toYaml .Values.airflow.podAnnotations | nindent 8 }}
        {{- end }}
        {{- if .Values.workers.podAnnotations }}
        {{- toYaml .Values.workers.podAnnotations | nindent 8 }}
        {{- end }}
        {{- if .Values.workers.safeToEvict }}
        cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
        {{- end }}
      labels:
        app: {{ include "airflow.labels.app" . }}
        component: worker
        release: {{ .Release.Name }}
        {{- if .Values.workers.podLabels }}
        {{- toYaml .Values.workers.podLabels | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.airflow.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.airflow.image.pullSecret }}
      {{- end }}
      restartPolicy: Always
      {{- if .Values.workers.celery.gracefullTermination }}
      terminationGracePeriodSeconds: {{ add .Values.workers.terminationPeriod .Values.workers.celery.gracefullTerminationPeriod }}
      {{- else }}
      terminationGracePeriodSeconds: {{ .Values.workers.terminationPeriod }}
      {{- end }}
      serviceAccountName: {{ include "airflow.serviceAccountName" . }}
      {{- if .Values.workers.nodeSelector }}
      nodeSelector:
        {{- toYaml .Values.workers.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.workers.affinity }}
      affinity:
        {{- toYaml .Values.workers.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.workers.tolerations }}
      tolerations:
        {{- toYaml .Values.workers.tolerations | nindent 8 }}
      {{- end }}
      {{- if .Values.workers.securityContext }}
      securityContext:
        {{- toYaml .Values.workers.securityContext | nindent 8 }}
      {{- end }}
      {{- if .Values.dags.initContainer.enabled }}
      initContainers:
        - name: git-clone
          image: {{ .Values.dags.initContainer.image.repository }}:{{ .Values.dags.initContainer.image.tag }}
          imagePullPolicy: {{ .Values.dags.initContainer.image.pullPolicy }}
          envFrom:
            - configMapRef:
                name: "{{ include "airflow.fullname" . }}-env"
          env:
            {{- include "airflow.mapenvsecrets" . | indent 12 }}
          resources:
            {{- toYaml .Values.dags.initContainer.resources | nindent 12 }}
          command:
            - /home/airflow/git/git-clone.sh
          args:
            - "{{ .Values.dags.git.url }}"
            - "{{ .Values.dags.git.ref }}"
            - "{{ .Values.dags.initContainer.mountPath }}{{ .Values.dags.initContainer.syncSubPath }}"
            - "{{ .Values.dags.git.repoHost }}"
            - "{{ .Values.dags.git.repoPort }}"
            - "{{ .Values.dags.git.privateKeyName }}"
          volumeMounts:
            - name: git-clone
              mountPath: /home/airflow/git
            - name: dags-data
              mountPath: "{{ .Values.dags.initContainer.mountPath }}"
            {{- if .Values.dags.git.secret }}
            - name: git-clone-secret
              mountPath: /keys
            {{- end }}
      {{- end }}
      containers:
        {{- if .Values.dags.git.gitSync.enabled }}
        - name: git-sync
          image: {{ .Values.dags.git.gitSync.image.repository }}:{{ .Values.dags.git.gitSync.image.tag }}
          imagePullPolicy: {{ .Values.dags.git.gitSync.image.pullPolicy }}
          envFrom:
            - configMapRef:
                name: "{{ include "airflow.fullname" . }}-env"
          env:
            {{- include "airflow.mapenvsecrets" . | indent 12 }}
          command:
            - /home/airflow/git/git-sync.sh
          args:
            - "{{ .Values.dags.git.url }}"
            - "{{ .Values.dags.git.ref }}"
            - "{{ .Values.dags.initContainer.mountPath }}{{ .Values.dags.initContainer.syncSubPath }}"
            - "{{ .Values.dags.git.repoHost }}"
            - "{{ .Values.dags.git.repoPort }}"
            - "{{ .Values.dags.git.privateKeyName }}"
            - "{{ .Values.dags.git.gitSync.refreshTime }}"
          volumeMounts:
            - name: git-clone
              mountPath: /home/airflow/git
            - name: dags-data
              mountPath: "{{ .Values.dags.initContainer.mountPath }}"
            {{- if .Values.dags.git.secret }}
            - name: git-clone-secret
              mountPath: /keys
            {{- end }}
          resources:
            {{- toYaml .Values.dags.git.gitSync.resources | nindent 12 }}
        {{- end }}
        - name: {{ .Chart.Name }}-worker
          imagePullPolicy: {{ .Values.airflow.image.pullPolicy }}
          image: "{{ .Values.airflow.image.repository }}:{{ .Values.airflow.image.tag }}"
          {{- if and (eq .Values.airflow.executor "CeleryExecutor") (.Values.workers.celery.gracefullTermination) }}
          lifecycle:
            preStop:
              exec:
                command:
                  - "timeout"
                  - "{{ .Values.workers.celery.gracefullTerminationPeriod }}s"
                  - "/home/airflow/scripts/graceful-stop-celery-worker.sh"
          {{- end}}
          envFrom:
            - configMapRef:
                name: "{{ include "airflow.fullname" . }}-env"
          env:
            {{- include "airflow.mapenvsecrets" . | indent 12 }}
          volumeMounts:
            - name: scripts
              mountPath: /home/airflow/scripts
            {{- $secretsDir := .Values.workers.secretsDir }}
            {{- if .Values.workers.secretsMap }}
            - name: {{ .Values.workers.secretsMap }}-volume
              readOnly: true
              mountPath: {{ $secretsDir }}
            {{- else }}
            {{- range .Values.workers.secrets }}
            - name: {{ . }}-volume
              readOnly: true
              mountPath: {{ $secretsDir }}/{{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.dags.persistence.enabled }}
            - name: dags-data
              mountPath: {{ .Values.dags.path }}
              subPath: {{ .Values.dags.persistence.subPath }}
            {{- else if or (.Values.dags.initContainer.enabled) (.Values.dags.git.gitSync.enabled) }}
            - name: dags-data
              mountPath: {{ .Values.dags.path }}
            {{- end }}
            {{- if .Values.logs.persistence.enabled }}
            - name: logs-data
              mountPath: {{ .Values.logs.path }}
              subPath: {{ .Values.logs.persistence.subPath }}
            {{- end }}
            {{- range .Values.airflow.extraConfigmapMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              readOnly: {{ .readOnly }}
              {{- if .subPath }}
              subPath: {{ .subPath }}
              {{- end }}
            {{- end }}
            {{- if .Values.airflow.extraVolumeMounts }}
            {{- toYaml .Values.airflow.extraVolumeMounts | nindent 12 }}
            {{- end }}
          command:
            - "/usr/bin/dumb-init"
            - "--"
          args:
            - "bash"
            - "-c"
            - >
              true \
              {{- if gt .Values.workers.initialStartupDelay 0.0 }}
               && echo "*** waiting {{ .Values.workers.initialStartupDelay }}s..." \
               && sleep {{ .Values.workers.initialStartupDelay }} \
              {{- end }}
               && mkdir -p /home/airflow/.local/bin \
               && export PATH="/home/airflow/.local/bin:$PATH" \
              {{- if .Values.dags.installRequirements }}
               && echo "*** installing requirements..." \
               && /home/airflow/scripts/install-requirements.sh \
              {{- end }}
              {{- if .Values.airflow.extraPipPackages }}
               && echo "*** installing global extra pip packages..." \
               && pip install --user {{ range .Values.airflow.extraPipPackages }} {{ . | quote }} {{ end }} \
              {{- end }}
               && echo "*** running worker..." \
               && exec airflow worker
          ports:
            - name: wlog
              containerPort: 8793
              protocol: TCP
          resources:
            {{- toYaml .Values.workers.resources | nindent 12 }}
        {{- if .Values.airflow.extraContainers }}
        {{- toYaml .Values.airflow.extraContainers | nindent 8 }}
        {{- end }}
      volumes:
        - name: scripts
          configMap:
            name: {{ include "airflow.fullname" . }}-scripts
            defaultMode: 0755
        {{- if .Values.workers.secretsMap }}
        - name: {{ .Values.workers.secretsMap }}-volume
          secret:
            secretName: {{ .Values.workers.secretsMap }}
        {{- else }}
        {{- range .Values.workers.secrets }}
        - name: {{ . }}-volume
          secret:
            secretName: {{ . }}
        {{- end }}
        {{- end }}
        {{- if .Values.dags.persistence.enabled }}
        - name: dags-data
          persistentVolumeClaim:
            claimName: {{ .Values.dags.persistence.existingClaim | default (include "airflow.fullname" . ) }}
        {{- else if or (.Values.dags.initContainer.enabled) (.Values.dags.git.gitSync.enabled) }}
        - name: dags-data
          emptyDir: {}
        {{- end }}
        {{- if .Values.logs.persistence.enabled }}
        - name: logs-data
          persistentVolumeClaim:
            claimName: {{ .Values.logs.persistence.existingClaim | default (printf "%s-logs" (include "airflow.fullname" . | trunc 58 )) }}
        {{- end }}
        {{- if or (.Values.dags.initContainer.enabled) (.Values.dags.git.gitSync.enabled) }}
        - name: git-clone
          configMap:
            name: {{ include "airflow.fullname" . }}-scripts-git
            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 | nindent 8 }}
        {{- end }}
{{- end }}
