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

{{- if or (contains "CeleryExecutor" .Values.executor) (eq .Values.executor "CeleryKubernetesExecutor") }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "airflow.worker.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: worker
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.worker.podManagementPolicy }}
  podManagementPolicy: {{ .Values.worker.podManagementPolicy | quote }}
  {{- end }}
  {{- if not (coalesce .Values.worker.autoscaling.hpa.enabled .Values.worker.autoscaling.enabled) }}
  replicas: {{ .Values.worker.replicaCount }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.worker.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: worker
  serviceName: {{ printf "%s-hl" (include "airflow.worker.fullname" .) | trunc 63 | trimSuffix "-" }}
  {{- if .Values.worker.updateStrategy }}
  updateStrategy: {{- toYaml .Values.worker.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/config/configmap.yaml") . | sha256sum }}
        checksum/webserver-configmap: {{ include (print $.Template.BasePath "/web/configmap.yaml") . | sha256sum }}
        {{- if .Values.worker.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: worker
    spec:
      {{- include "airflow.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.worker.automountServiceAccountToken }}
      {{- if .Values.worker.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.worker.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.worker.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.worker.podAffinityPreset "component" "worker" "customLabels" $podLabels "topologyKey" .Values.worker.topologyKey "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.worker.podAntiAffinityPreset "component" "worker" "customLabels" $podLabels "topologyKey" .Values.worker.topologyKey "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.worker.nodeAffinityPreset.type "key" .Values.worker.nodeAffinityPreset.key "values" .Values.worker.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.worker.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.worker.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.worker.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.worker.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.worker.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.worker.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.priorityClassName }}
      priorityClassName: {{ .Values.worker.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.worker.schedulerName }}
      schedulerName: {{ .Values.worker.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "airflow.serviceAccountName" . }}
      {{- if .Values.worker.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.worker.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 }}
        {{- include "airflow.defaultInitContainers.waitForDBMigrations" . | nindent 8 }}
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
        {{- if .Values.worker.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.initContainers "context" $) | trim | nindent 8 }}
        {{- end }}
      containers:
        - name: airflow-worker
          image: {{ include "airflow.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.worker.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.worker.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.worker.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.worker.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.worker.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.worker.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            {{- include "airflow.configure.airflow.common"  . | nindent 12 }}
            - name: AIRFLOW_COMPONENT_TYPE
              value: "worker"
            - name: AIRFLOW_EXECUTOR
              value: {{ include "airflow.worker.executor" . }}
            - name: PYTHONPYCACHEPREFIX
              value: "/opt/bitnami/airflow/venv/tmp"
            {{- if or (contains "CeleryExecutor" .Values.executor) (eq .Values.executor "CeleryKubernetesExecutor") }}
            - name: REDIS_HOST
              value: {{ include "airflow.redis.host" . | quote }}
            - name: REDIS_PORT_NUMBER
              value: {{ include "airflow.redis.port" . | quote }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.worker.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.worker.extraEnvVarsCM .Values.worker.extraEnvVarsCMs .Values.worker.extraEnvVarsSecret .Values.worker.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.worker.extraEnvVarsCMs }}
            {{- range .Values.worker.extraEnvVarsCMs }}
            - configMapRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.worker.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.worker.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.worker.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.worker.extraEnvVarsSecret }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecrets }}
            {{- range .Values.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
            {{- if .Values.worker.extraEnvVarsSecrets }}
            {{- range .Values.worker.extraEnvVarsSecrets }}
            - secretRef:
                name: {{ . }}
            {{- end }}
            {{- end }}
          {{- end }}
          ports:
            - name: worker
              containerPort: {{ .Values.worker.containerPorts.http }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.worker.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: worker
          {{- end }}
          {{- if .Values.worker.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: worker
          {{- end }}
          {{- if .Values.worker.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - sh
                - -c
                - |
                  . /opt/bitnami/airflow/venv/bin/activate && \
                  CONNECTION_CHECK_MAX_COUNT=0 python -m celery --app airflow.providers.celery.executors.celery_executor.app inspect ping -d celery@$(hostname)
          {{- end }}
          {{- end }}
          {{- if .Values.worker.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.worker.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.worker.resources }}
          resources: {{- toYaml .Values.worker.resources | nindent 12 }}
          {{- else if ne .Values.worker.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.worker.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 .Values.dags.enabled }}
            {{- include "airflow.dags.volumeMounts" . | nindent 12 }}
            {{- end }}
            {{- if .Values.plugins.enabled }}
            {{- include "airflow.plugins.volumeMounts" . | nindent 12 }}
            {{- end }}
            {{- if .Values.web.tls.enabled }}
            - name: tls-certificates
              mountPath: /opt/bitnami/airflow/certs
              readOnly: true
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.worker.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .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.worker.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.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.web.tls.enabled }}
        - name: tls-certificates
          secret:
            secretName: {{ template "airflow.web.tls.secretName" . }}
            defaultMode: 256
        {{- end }}
        {{- 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 .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.worker.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if .Values.worker.extraVolumeClaimTemplates }}
  volumeClaimTemplates: {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumeClaimTemplates "context" $) | nindent 8 }}
  {{- end }}
{{- end }}
