{{- if .Values.user.create -}}
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}-user-create"
  labels:
    app: {{ template "fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  annotations:
    # This is what defines this resource as a hook. Without this line, the
    # job is considered part of the release.
    "helm.sh/hook": post-install
    "helm.sh/hook-weight": "5"
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}-user-create"
      labels:
        app: {{ template "fullname" . }}
        release: "{{ .Release.Name }}"
    spec:
      restartPolicy: Never
      {{- if .Values.image.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.imagePullSecrets | indent 8 }}
      {{- end }}
      containers:
      - name: user-create-job
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        command: ["sentry","createuser","--no-input","--email", "{{ .Values.user.email }}", "--superuser","--password","$(SENTRY_USER_PASSWORD)"]
        env:
        - name: SENTRY_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: sentry-secret
        - name: SENTRY_DB_USER
          value: {{ default "sentry" .Values.postgresUser | quote }}
        - name: SENTRY_DB_NAME
          value: {{ default "sentry" .Values.postgresDatabase | quote }}
        - name: SENTRY_DB_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.postgresql.existingSecret }}
              name: {{ .Values.postgresql.existingSecret }}
            {{- else }}
              name: {{ template "postgresql.fullname" . }}
            {{- end }}
              key: postgres-password
        - name: SENTRY_POSTGRES_HOST
          value: {{ template "postgresql.fullname" . }}
        - name: SENTRY_POSTGRES_PORT
          value: "5432"
        - name: SENTRY_REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.redis.existingSecret }}
              name: {{ .Values.redis.existingSecret }}
            {{- else }}
              name: {{ template "redis.fullname" . }}
            {{- end }}
              key: redis-password
        - name: SENTRY_REDIS_HOST
          value: {{ template "redis.fullname" . }}-master
        - name: SENTRY_REDIS_PORT
          value: "6379"
        - name: SENTRY_EMAIL_HOST
          value: {{ default "" .Values.smtpHost | quote }}
        - name: SENTRY_EMAIL_PORT
          value: {{ default "" .Values.smtpPort | quote }}
        - name: SENTRY_EMAIL_USER
          value: {{ default "" .Values.smtpUser | quote }}
        - name: SENTRY_EMAIL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: smtp-password
        - name: SENTRY_USER_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "fullname" . }}
              key: user-password
        - name: SENTRY_EMAIL_USE_TLS
          value: {{ .Values.email.use_tls | quote }}
        - name: SENTRY_SERVER_EMAIL
          value: {{ .Values.email.from_address | quote }}
        volumeMounts:
        - mountPath: /etc/sentry
          name: config
          readOnly: true
      volumes:
      - name: config
        configMap:
          name: {{ template "fullname" . }}
{{- end -}}
