apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.redash.instance.name.viewer }}-scheduler
  labels:
    app.kubernetes.io/name: {{ .Values.redash.instance.name.viewer }}
    app.kubernetes.io/instance: {{ .Values.redash.instance.name.viewer }}
    app.kubernetes.io/version: {{ .Values.redash.version | squote }}
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: scheduler
spec:
  replicas: {{ .Values.scheduler.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ .Values.redash.instance.name.viewer }}
      app.kubernetes.io/instance: {{ .Values.redash.instance.name.viewer }}
      app.kubernetes.io/component: scheduler
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ .Values.redash.instance.name.viewer }}
        app.kubernetes.io/instance: {{ .Values.redash.instance.name.viewer }}
        app.kubernetes.io/component: scheduler
        collect.logs: "plain-text"
      {{- if .Values.scheduler.podAnnotations }}
      annotations:
      {{ toYaml .Values.scheduler.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.viewer }}-scheduler
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.name }}:{{ .Values.image.version }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/bin/sh"]
          args: ["-c", ". /config/dynamicenv.sh && /app/bin/docker-entrypoint scheduler"]
          volumeMounts:
            - name: config
              mountPath: /config
          env:
            - name: REDASH_ROUTE_PREFIX
              value: 'reports'
            - name: REDASH_DATABASE_HOSTNAME
              value: {{ .Values.redash.instance.name.viewer }}-postgresql
            - name: REDASH_REDIS_HOSTNAME
              value: {{ .Values.redash.instance.name.viewer }}-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.viewer | quote }}
          {{- include "redash.env.viewer" . | nindent 12 }}
          envFrom:
          {{- include "redash.envFrom" . | nindent 12 }}
          resources:
{{ toYaml .Values.scheduler.resources | indent 12 }}
      volumes:
        - name: config
          configMap:
            name: {{ include "redash.fullname" . }}
    {{- if .Values.scheduler.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.scheduler.nodeSelector | indent 8 }}
    {{- end }}
    {{- with .Values.scheduler.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.scheduler.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
