{{- if eq .Values.global.deploymentMode "development" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.redash.instance.name.admin }}
  labels:
    helm.sh/chart: {{ .Values.redash.instance.name.admin }}-2.3.0
    app.kubernetes.io/name: {{ .Values.redash.instance.name.admin }}
    app.kubernetes.io/instance: {{ .Values.redash.instance.name.admin }}
    app.kubernetes.io/version: {{ .Values.redash.version | squote }}
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: server
spec:
  replicas: {{ .Values.server.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ .Values.redash.instance.name.admin }}
      app.kubernetes.io/instance: {{ .Values.redash.instance.name.admin }}
      app.kubernetes.io/component: server
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ .Values.redash.instance.name.admin }}
        app.kubernetes.io/instance: {{ .Values.redash.instance.name.admin }}
        app.kubernetes.io/component: server
        collect.logs: "plain-text"
      {{- if .Values.server.podAnnotations }}
      annotations:
      {{ toYaml .Values.server.podAnnotations | nindent 8 }}
      {{- end }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "redash.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Values.redash.instance.name.admin }}-server
          image: "{{ .Values.image.name }}:{{ .Values.image.version }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/bin/sh"]
          args: ["-c", ". /config/dynamicenv.sh && /app/bin/docker-entrypoint server"]
          lifecycle:
            postStart:
              exec:
                command:
                - "sh"
                - "-c"
                - bash admin_setup.sh > /dev/null 2>&1 &
          volumeMounts:
            - name: config
              mountPath: /config
            - name: admin-config
              mountPath: /app/admin_setup.sh
              subPath: admin_setup.sh
          env:
            - name: REDASH_ROUTE_PREFIX
              value: 'reports'
            - name: SYSTEM_ADMIN_PASS
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.redash.system.admin.secret }}
                  key: password
            - name: SYSTEM_ADMIN_EMAIL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.redash.system.admin.secret }}
                  key: email
            - name: REDASH_DATABASE_HOSTNAME
              value: {{ .Values.redash.instance.name.admin }}-postgresql
            - name: REDASH_RATELIMIT_ENABLED
              value: 'false'
            - name: REDASH_REDIS_HOSTNAME
              value: {{ .Values.redash.instance.name.admin }}-redis-master
          {{- if .Values.redis.connectionParams }}
            - name: REDASH_REDIS_CONN_PARAMS
              value: {{ .Values.redis.connectionParams }}
          {{- end }}
            - name: REDASH_SAML_ENTITY_ID
              value: {{ default  .Values.redash.samlEntityId.admin | quote }}
          {{- include "redash.env.admin" . | nindent 12 }}
          {{- range $key, $value := .Values.server.env }}
            - name: "{{ $key }}"
              value: "{{ $value }}"
          {{- end }}
            - name: REDASH_SAML_METADATA_URL
              value: "https://{{ .Values.keycloak.host }}/auth/realms/{{ .Release.Namespace }}-{{ .Values.redash.instance.keycloakRealms.admin }}/protocol/saml/descriptor"
            - name: REDASH_SAML_REDIRECT_URL
              value: "https://{{ .Values.keycloak.host }}/auth/realms/{{ .Release.Namespace }}-{{ .Values.redash.instance.keycloakRealms.admin }}/protocol/saml"
          envFrom:
          {{- include "redash.envFrom" . | nindent 12 }}
          ports:
            - containerPort: {{ .Values.server.httpPort }}
          livenessProbe:
            httpGet:
              path: /reports/ping
              port: {{ .Values.server.httpPort }}
            # Redash can take a while to come up initially, so we delay checks.
            initialDelaySeconds: 90
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 10
          readinessProbe:
            httpGet:
              path: /reports/ping
              port: {{ .Values.server.httpPort }}
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          {{- if not .Values.global.disableRequestsLimits }}
          resources:
{{ toYaml .Values.server.resources | indent 12 }}
          {{- end }}
      volumes:
        - name: admin-config
          configMap:
            name: admin-setup-script-cm
            defaultMode: 0777
        - name: config
          configMap:
            name: {{ include "redash.fullname" . }}
    {{- with .Values.server.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.server.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.server.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- end }}
