apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}-admin-init-{{ .Release.Revision }}
  namespace: {{ .Values.global.namespace }}
  labels:
    app: {{ .Release.Name }}-admin-init
    component: init
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-weight": "10"
    "helm.sh/hook-delete-policy": before-hook-creation
spec:
  ttlSecondsAfterFinished: 300
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-admin-init
    spec:
      restartPolicy: Never
      containers:
        - name: admin-init
          image: python:3.12-slim
          command:
            - /bin/sh
            - -c
            - |
              pip install --no-cache-dir psycopg2-binary bcrypt > /dev/null 2>&1
              python3 /scripts/create-admin.py
          env:
            - name: DB_HOST
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_HOST
            - name: DB_PORT
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PORT
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_NAME
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_USER
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PASSWORD
            - name: ADMIN_USERNAME
              value: {{ .Values.admin.username | quote }}
            - name: ADMIN_EMAIL
              value: {{ .Values.admin.email | quote }}
            - name: ADMIN_PASSWORD
              value: {{ .Values.admin.password | quote }}
            - name: ADMIN_FIRST_NAME
              value: {{ .Values.admin.firstName | quote }}
            - name: ADMIN_LAST_NAME
              value: {{ .Values.admin.lastName | quote }}
            - name: ADMIN_DISPLAY_NAME
              value: {{ .Values.admin.displayName | quote }}
          volumeMounts:
            - name: init-script
              mountPath: /scripts
      volumes:
        - name: init-script
          configMap:
            name: {{ .Release.Name }}-admin-init
            defaultMode: 0755
