apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Values.redash.instance.name.viewer }}-db-upgrade"
  labels:
    helm.sh/chart: {{ .Values.redash.instance.name.viewer }}-2.3.0
    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: upgrade
  annotations:
    # This is what defines this resource as a hook.
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    metadata:
      name: {{ .Values.redash.instance.name.viewer }}
      annotations:
        sidecar.istio.io/inject: "false"
      labels:
        app.kubernetes.io/name: {{ .Values.redash.instance.name.viewer }}
        app.kubernetes.io/instance: {{ .Values.redash.instance.name.viewer }}
    spec:
      serviceAccountName: {{ .Values.serviceAccount.name }}
      restartPolicy: Never
      containers:
      - name: {{ .Values.redash.instance.name.viewer }}-server
        image: "{{ .Values.image.name }}:{{ .Values.image.version }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        command: ["/bin/bash"]
        args:
          - -c
          - >-
              export DATABASE_URL=postgresql://$REDASH_DATABASE_USER:$REDASH_DATABASE_PASSWORD@$REDASH_DATABASE_HOSTNAME/$REDASH_DATABASE_DB;
              python ./manage.py db upgrade
        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
          - name: REDASH_SAML_SSO_URL
            value: "just a dummy text"
          - name: REDASH_SAML_AUTH_TYPE
            value: "dynamic"
          - name: REDASH_SAML_ENTITY_ID
            value: {{ default  .Values.redash.samlEntityId.viewer | quote }}
        {{- include "redash.env.viewer" . | nindent 10 }}
        {{- range $key, $value := .Values.server.env }}
          - name: "{{ $key }}"
            value: "{{ $value }}"
        {{- end }}
        envFrom:
        {{- include "redash.envFrom" . | nindent 10 }}
        {{- if not .Values.global.disableRequestsLimits }}
        resources:
{{ toYaml .Values.server.resources | indent 10 }}
        {{- end }}
      volumes:
        - 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 }}
