apiVersion: batch/v1
kind: Job
metadata:
  name: "sentry-user-create"
  labels:
    app: sentry
  annotations:
    "kots.io/hook-delete-policy": "hook-succeeded, hook-failed"
spec:
  backoffLimit: 12 # Because it takes about 6 minutes for DB to come up and migration job to run. This will ensure the job is re-tried a few more times than default.
  template:
    metadata:
      name: "sentry-user-create"
      labels:
        app: sentry
        component: sentry
    spec:
      restartPolicy: Never
      containers:
      - name: user-create-job
        image: "sentry:9.1.1"
        command: ["sentry","createuser","--no-input","--email", "$(SENTRY_ADMIN_USERNAME)", "--superuser","--password","$(SENTRY_USER_PASSWORD)"]
        env:
        - name: SENTRY_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: sentry
              key: sentry-secret
        - name: SENTRY_DB_USER
          value: '{{repl if ConfigOptionEquals "postgres_type" "embedded_postgres"}}sentry{{repl else}}{{repl ConfigOption "external_postgres_user" }}{{repl end}}'
        - name: SENTRY_DB_NAME
          value: '{{repl if ConfigOptionEquals "postgres_type" "embedded_postgres"}}sentry{{repl else}}{{repl ConfigOption "external_postgres_db" }}{{repl end}}'
        - name: SENTRY_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sentry-postgresql
              key: postgres-password
        - name: SENTRY_POSTGRES_HOST
          value: '{{repl if ConfigOptionEquals "postgres_type" "embedded_postgres"}}sentry-postgresql{{repl else}}{{repl ConfigOption "external_postgres_host"}}{{repl end}}'
        - name: SENTRY_POSTGRES_PORT
          value: '{{repl if ConfigOptionEquals "postgres_type" "embedded_postgres"}}5432{{repl else}}{{repl ConfigOption "external_postgres_port" }}{{repl end}}'
        - name: SENTRY_REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sentry-redis
              key: redis-password
        - name: SENTRY_REDIS_HOST
          value: sentry-redis-master
        - name: SENTRY_REDIS_PORT
          value: "6379"
        - name: SENTRY_EMAIL_HOST
          value: '{{repl ConfigOption "smtp_host"}}'
        - name: SENTRY_EMAIL_PORT
          value: '{{repl ConfigOption "smtp_port"}}'
        - name: SENTRY_EMAIL_USER
          value: '{{repl ConfigOption "smtp_user"}}'
        - name: SENTRY_EMAIL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sentry
              key: smtp-password
        - name: SENTRY_USER_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sentry
              key: user-password
        - name: SENTRY_ADMIN_USERNAME
          value: '{{repl ConfigOption "admin_username"}}'
        - name: SENTRY_EMAIL_USE_TLS
          value: "false"
        - name: SENTRY_SERVER_EMAIL
          value: "sentry@sentry.local"
        volumeMounts:
        - mountPath: /etc/sentry
          name: config
          readOnly: true
      volumes:
      - name: config
        configMap:
          name: sentry
