# helm_charts_db-init.job.yaml
# https://docs.sentry.io/server/installation/docker/#running-migrations
{{- if .Values.hooks.dbInit.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: "{{ .Release.Name }}-db-init"
  labels:
    app: {{ template "sentry.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,post-upgrade"
    "helm.sh/hook-delete-policy": "hook-succeeded,before-hook-creation"
    "helm.sh/hook-weight": "-5"
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}-db-init"
      annotations:
        checksum/secrets.yaml: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
      labels:
        app: {{ template "sentry.fullname" . }}
        release: "{{ .Release.Name }}"
        {{- if .Values.worker.podLabels }}
{{ toYaml .Values.worker.podLabels | indent 8 }}
        {{- end }}
    spec:
      {{- with .Values.hooks.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
      {{- end }}
      {{- if .Values.hooks.tolerations }}
      tolerations:
{{ toYaml .Values.hooks.tolerations | indent 8 }}
      {{- end }}
      restartPolicy: Never
      {{- if .Values.image.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.imagePullSecrets | indent 8 }}
      {{- end }}
      containers:
      - name: db-init-job
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        command: ["sentry","upgrade","--noinput"]
        env:
        - name: SENTRY_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ template "sentry.fullname" . }}
              key: sentry-secret
        - name: SENTRY_DB_USER
          value: {{ default "sentry" .Values.postgresql.postgresqlUsername | quote }}
        - name: SENTRY_DB_NAME
          value: {{ default "sentry" .Values.postgresql.postgresqlDatabase | quote }}
        - name: SENTRY_DB_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.postgresql.existingSecret }}
              name: {{ .Values.postgresql.existingSecret }}
            {{- else }}
              name: {{ template "sentry.postgresql.secret" . }}
            {{- end }}
              key: {{ template "sentry.postgresql.secretKey" . }}
        - name: SENTRY_POSTGRES_HOST
          value: {{ template "sentry.postgresql.host" . }}
        - name: SENTRY_POSTGRES_PORT
          value: {{ template "sentry.postgresql.port" . }}
        {{- if or (.Values.redis.enabled) (.Values.redis.password) (.Values.redis.existingSecret)  }}
        - name: SENTRY_REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.redis.existingSecret }}
              name: {{ .Values.redis.existingSecret }}
            {{- else }}
              name: {{ template "sentry.redis.secret" . }}
            {{- end }}
              key: {{ template "sentry.redis.secretKey" . }}
        {{- end }}
        - name: SENTRY_REDIS_HOST
          value: {{ template "sentry.redis.host" . }}
        - name: SENTRY_REDIS_PORT
          value: {{ template "sentry.redis.port" . }}
        - name: SENTRY_EMAIL_HOST
          value: {{ default "" .Values.email.host | quote }}
        - name: SENTRY_EMAIL_PORT
          value: {{ default "" .Values.email.port | quote }}
        - name: SENTRY_EMAIL_USER
          value: {{ default "" .Values.email.user | quote }}
        - name: SENTRY_EMAIL_PASSWORD
          valueFrom:
            secretKeyRef:
            {{- if .Values.email.existingSecret }}
              name: {{ .Values.email.existingSecret }}
            {{- else }}
              name: {{ template "sentry.fullname" . }}
            {{- end }}
              key: smtp-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
        resources:
{{ toYaml .Values.hooks.dbInit.resources | indent 10 }}
      volumes:
      - name: config
        configMap:
          name: {{ template "sentry.fullname" . }}
{{- end -}}