apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "superset.fullname" . }}
  labels:
    app: {{ include "superset.name" . }}
    chart: {{ include "superset.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ include "superset.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ include "superset.fullname" . }}
      labels:
        app: {{ include "superset.name" . }}
        chart: {{ include "superset.chart" . }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
      annotations:
        checksum/secrets: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- with .Values.deploymentAnnotations }}
        {{- . | toYaml | nindent 8 }}
        {{- end }}
    spec:
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
      volumes:
        - name: superset-configs
          secret:
            secretName: {{ include "superset.fullname" . }}
        - name: storage-volume
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "superset.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/usr/bin/env"]
          args:
          - bash
          - /home/superset/init_superset.sh
          {{- range .Values.extraArguments }}
          - {{ . }}
          {{- end }}
          volumeMounts:
            - name: superset-configs
              mountPath: /home/superset
            - name: storage-volume
              mountPath: /var/lib/superset
          ports:
            - name: http
              containerPort: 8088
              protocol: TCP
          {{- if .Values.extraEnv }}
          env:
            {{- range $key, $value := .Values.extraEnv }}
            - name: "{{ $key }}"
              value: "{{ $value }}"
            {{- end }}
          {{- end }}
          {{- if .Values.extraEnvFromSecret }}
          envFrom:
            - secretRef:
                name: {{ .Values.extraEnvFromSecret }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- . | toYaml | nindent 12 }}
          {{- end }}
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- . | toYaml | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{- . | toYaml | nindent 12 }}
          {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- . | toYaml | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- . | toYaml | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- . | toYaml | nindent 8 }}
      {{- end }}
