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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "superset.worker.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: celery-worker
    app.kubernetes.io/part-of: superset
  {{- if or .Values.worker.deploymentAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.worker.deploymentAnnotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (not .Values.worker.autoscaling.hpa.enabled) (not .Values.worker.autoscaling.vpa.enabled) }}
  replicas: {{ .Values.worker.replicaCount }}
  {{- end }}
  {{- if .Values.worker.updateStrategy }}
  strategy: {{- toYaml .Values.worker.updateStrategy | nindent 4 }}
  {{- 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: celery-worker
      app.kubernetes.io/part-of: superset
  template:
    metadata:
      {{- if .Values.worker.podAnnotations }}
      annotations: {{- 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: celery-worker
        app.kubernetes.io/part-of: superset
    spec:
      {{- include "superset.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "superset.serviceAccountName" . }}
      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" "celery-worker" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.worker.podAntiAffinityPreset "component" "celery-worker" "customLabels" $podLabels "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.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.worker.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.priorityClassName }}
      priorityClassName: {{ .Values.worker.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.worker.schedulerName }}
      schedulerName: {{ .Values.worker.schedulerName | quote }}
      {{- end }}
      {{- if .Values.worker.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.worker.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.worker.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.worker.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.defaultInitContainers.waitForDB.enabled }}
        {{- include "superset.initContainers.waitForDB" . | nindent 8 }}
        {{- end }}
        {{- if .Values.defaultInitContainers.waitForRedis.enabled }}
        {{- include "superset.initContainers.waitForRedis" . | nindent 8 }}
        {{- end }}
        {{- if .Values.worker.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: celery-worker
          image: {{ template "superset.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 "superset.configure.common" . | nindent 12 }}
            {{- include "superset.configure.database" . | nindent 12 }}
            {{- include "superset.configure.redis" . | nindent 12 }}
            - name: SUPERSET_ROLE
              value: "celery-worker"
            {{- if .Values.worker.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.worker.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.worker.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVarsSecret "context" $) }}
            {{- 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 }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- 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 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  {{- if .Values.usePasswordFiles }}
                  export SUPERSET_SECRET_KEY="$(< $SUPERSET_SECRET_KEY_FILE)"
                  {{- end }}
                  . /opt/bitnami/scripts/superset-env.sh && celery -A superset.tasks.celery_app:app inspect ping -d celery@$HOSTNAME
          {{- 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:
                - /bin/bash
                - -ec
                - |
                  {{- if .Values.usePasswordFiles }}
                  export SUPERSET_SECRET_KEY="$(< $SUPERSET_SECRET_KEY_FILE)"
                  {{- end }}
                  . /opt/bitnami/scripts/superset-env.sh && celery -A superset.tasks.celery_app:app inspect ping -d celery@$HOSTNAME
          {{- end }}
          {{- 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 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  {{- if .Values.usePasswordFiles }}
                  export SUPERSET_SECRET_KEY="$(< $SUPERSET_SECRET_KEY_FILE)"
                  {{- end }}
                  . /opt/bitnami/scripts/superset-env.sh && celery -A superset.tasks.celery_app: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 }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/superset/superset_home
              subPath: superset-home
            {{- if .Values.usePasswordFiles }}
            - name: superset-secrets
              mountPath: /opt/bitnami/superset/secrets
              readOnly: true
            {{- end }}
            {{- if or .Values.existingConfigmap .Values.config }}
            - name: superset-config
              mountPath: /bitnami/superset/conf
            {{- end }}
          {{- if .Values.worker.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.worker.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.worker.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.usePasswordFiles }}
        - name: superset-secrets
          projected:
            sources:
              - secret:
                  name: {{ include "superset.secretName" . }}
                  items:
                    - key: superset-secret-key
                      path: superset-secret-key
              - secret:
                  name: {{ include "superset.redis.secretName" . }}
              {{- if or (not .Values.postgresql.enabled) .Values.postgresql.auth.enablePostgresUser }}
              - secret:
                  name: {{ include "superset.postgresql.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if or .Values.existingConfigmap .Values.config }}
        - name: superset-config
          configMap:
            name: {{ include "superset.configMapName" . }}
        {{- end }}
        {{- if .Values.worker.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
