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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "superset.web.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: superset-web
    app.kubernetes.io/part-of: superset
  {{- if or .Values.web.deploymentAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.web.deploymentAnnotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (not .Values.web.autoscaling.hpa.enabled) (not .Values.web.autoscaling.vpa.enabled) }}
  replicas: {{ .Values.web.replicaCount }}
  {{- end }}
  {{- if .Values.web.updateStrategy }}
  strategy: {{- toYaml .Values.web.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.web.podLabels .Values.commonLabels) "context" .) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: superset-web
      app.kubernetes.io/part-of: superset
  template:
    metadata:
      {{- if .Values.web.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.web.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: superset-web
        app.kubernetes.io/part-of: superset
    spec:
      {{- include "superset.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ template "superset.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.web.automountServiceAccountToken }}
      {{- if .Values.web.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.web.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.web.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.web.podAffinityPreset "component" "superset-web" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.web.podAntiAffinityPreset "component" "superset-web" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.web.nodeAffinityPreset.type "key" .Values.web.nodeAffinityPreset.key "values" .Values.web.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.web.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.web.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.web.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.priorityClassName }}
      priorityClassName: {{ .Values.web.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.web.schedulerName }}
      schedulerName: {{ .Values.web.schedulerName | quote }}
      {{- end }}
      {{- if .Values.web.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.web.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.web.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.web.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.web.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 and .Values.init.enabled .Values.web.waitForExamples.enabled .Values.loadExamples }}
        {{- include "superset.initContainers.waitForExamples" . | nindent 8 }}
        {{- end }}
        {{- if .Values.web.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: superset-web
          image: {{ template "superset.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.web.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.web.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.web.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.web.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.web.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.web.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: "webserver"
            - name: SUPERSET_WEBSERVER_PORT_NUMBER
              value: {{ .Values.web.containerPorts.http | quote }}
            {{- if .Values.web.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.web.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.web.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.web.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.web.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.web.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.web.resources }}
          resources: {{- toYaml .Values.web.resources | nindent 12 }}
          {{- else if ne .Values.web.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.web.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.web.containerPorts.http }}
            {{- if .Values.flower.extraContainerPorts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.flower.extraContainerPorts "context" $) | nindent 12 }}
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.web.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.web.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: http
          {{- end }}
          {{- if .Values.web.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.web.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.web.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.web.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.web.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.web.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.web.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.web.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.web.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.web.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.web.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.web.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
