{{- if and (include "mastodon.createInitJob" .) (include "mastodon.web.domain" .) (include "mastodon.s3.aliasHost" .) }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "common.names.fullname" . }}-init
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: mastodon
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.initJob.annotations "context" $ ) | nindent 4 }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  backoffLimit: {{ .Values.initJob.backoffLimit }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: init
        {{- if .Values.initJob.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initJob.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.initJob.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.initJob.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "mastodon.imagePullSecrets" . | nindent 6 }}
      restartPolicy: OnFailure
      {{- if .Values.initJob.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.initJob.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.initJob.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.initJob.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      containers:
        # We separate the job in multiple containers to be able to run them in parallel. We put everything on the same job
        # as it follows the Job Pattern best practices
        # https://kubernetes.io/docs/concepts/workloads/controllers/job/#job-patterns
        {{- if or .Values.initJob.migrateDB .Values.initJob.createAdmin .Values.initJob.migrateElasticsearch }}
        - name: migrate-and-create-admin
          image: {{ template "mastodon.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/bash
            - -ec
          args:
            - /scripts/migrate-and-create-admin.sh
          {{- if .Values.initJob.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.initJob.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MASTODON_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mastodon.database.secretName" . }}
                  key: {{ include "mastodon.database.passwordKey" . | quote }}
            {{- if (include "mastodon.redis.auth.enabled" .) }}
            # The rake db:migrate job requires access to Redis
            - name: MASTODON_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mastodon.redis.secretName" . }}
                  key: {{ include "mastodon.redis.passwordKey" . | quote }}
            {{- end }}
            {{- if (include "mastodon.elasticsearch.auth.enabled" .) }}
            - name: MASTODON_ELASTICSEARCH_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mastodon.elasticsearch.secretName" . }}
                  key: {{ include "mastodon.elasticsearch.passwordKey" . | quote }}
            {{- end }}
            {{- if .Values.initJob.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "mastodon.defaultConfigmapName" . }}
            - secretRef:
                name: {{ include "mastodon.defaultSecretName" . }}
            {{- if or .Values.extraConfig .Values.extraConfigExistingConfigmap }}
            - configMapRef:
                name: {{ include "mastodon.extraConfigmapName" . }}
            {{- end }}
            {{- if or .Values.extraSecretConfig .Values.extraConfigExistingSecret }}
            - secretRef:
                name: {{ include "mastodon.extraSecretName" . }}
            {{- end }}
          {{- if or .Values.initJob.extraEnvVarsCM .Values.initJob.extraEnvVarsSecret }}
            {{- if .Values.initJob.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.initJob.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.initJob.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.initJob.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: scripts
              mountPath: /scripts
          {{- if .Values.initJob.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.initJob.resources }}
          resources: {{- toYaml .Values.initJob.resources | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.initJob.precompileAssets }}
        - name: mastodon-assets-precompile
          image: {{ template "mastodon.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/bash
            - -ec
          args:
            - /scripts/precompile-assets.sh
          {{- if .Values.initJob.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.initJob.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            {{- if .Values.enableS3 }}
            - name: MASTODON_S3_HOSTNAME
              value: {{ include "mastodon.s3.host" . | quote }}
            - name: MASTODON_S3_PORT_NUMBER
              value: {{ include "mastodon.s3.port" . | quote }}
            - name: MASTODON_AWS_ACCESS_KEY_ID
              valueFrom:
                secretKeyRef:
                  name: {{ include "mastodon.s3.secretName" . }}
                  key: {{ include "mastodon.s3.accessKeyIDKey" . | quote }}
            - name: MASTODON_AWS_SECRET_ACCESS_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "mastodon.s3.secretName" . }}
                  key: {{ include "mastodon.s3.secretAccessKeyKey" . | quote }}
            {{- end }}
            {{- if .Values.initJob.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ include "mastodon.defaultConfigmapName" . }}
            - secretRef:
                name: {{ include "mastodon.defaultSecretName" . }}
            {{- if or .Values.extraConfig .Values.extraConfigExistingConfigmap }}
            - configMapRef:
                name: {{ include "mastodon.extraConfigmapName" . }}
            {{- end }}
            {{- if or .Values.extraSecretConfig .Values.extraConfigExistingSecret }}
            - secretRef:
                name: {{ include "mastodon.extraSecretName" . }}
            {{- end }}
          {{- if or .Values.initJob.extraEnvVarsCM .Values.initJob.extraEnvVarsSecret }}
            {{- if .Values.initJob.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.initJob.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.initJob.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.initJob.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: scripts
              mountPath: /scripts
          {{- if not .Values.enableS3 }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
          {{- end }}
          {{- if .Values.initJob.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.initJob.resources }}
          resources: {{- toYaml .Values.initJob.resources | nindent 12 }}
          {{- end }}
          {{- end }}
      volumes:
        - name: scripts
          configMap:
            name: {{ printf "%s-init-scripts" (include "common.names.fullname" .) }}
            defaultMode: 0755
      {{- if not .Values.enableS3 }}
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ include "mastodon.pvc" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
      {{- end }}
      {{- if .Values.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraVolumes "context" $) | nindent 6 }}
      {{- end }}
{{- end }}
