apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "common.names.fullname" . }}-initialize-user
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    component: dashboard
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": before-hook-creation
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        component: dashboard
    spec:
      restartPolicy: Never
      containers:
      - name: {{ .Chart.Name }}-login-create-admin
        image: {{ template "backend.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        envFrom:
          - configMapRef:
              name: {{ include "common.names.fullname" . }}
          {{- if .Values.backend.extraEnvVarsCM }}
          - configMapRef:
              name: {{ .Values.backend.extraEnvVarsCM }}
          {{- end }}
          {{- if .Values.backend.extraEnvVarsSecret }}
          - secretRef:
              name: {{ .Values.backend.extraEnvVarsSecret }}
          {{- end }}
        env:
          - name: SETUP_EMAIL
            value: {{ .Values.backend.initialUser.email }}
          - name: SETUP_PASSWORD
            value: {{ .Values.backend.initialUser.password }}
          - name: KRATOS_PUBLIC_URL
            value: http://kratos-public:80
        command: ["/bin/bash", "-c"]
        args:
          - flask db upgrade;
            {{- if .Release.IsInstall }}
            flask cli user create $SETUP_EMAIL;
            flask cli user setpassword $SETUP_EMAIL $SETUP_PASSWORD;
            flask cli app create dashboard Dashboard;
            flask cli user setrole $SETUP_EMAIL dashboard admin;
            {{- end }}

