{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and (include "mastodon.createInitJob" .) (include "mastodon.web.domain" .) (or .Values.enableS3 .Values.persistence.enabled) }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "common.names.fullname" . }}-init
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mastodon
  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:
      {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.initJob.podLabels .Values.commonLabels ) "context" . ) }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: init
      {{- 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: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.initJob.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.initJob.automountServiceAccountToken }}
      {{- if .Values.initJob.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.initJob.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.defaultInitContainers.prepareDirs.enabled }}
        - name: copy-assets-dir
          image: {{ template "mastodon.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash
              cp -r /opt/bitnami/mastodon/public/* /public
          {{- if .Values.defaultInitContainers.prepareDirs.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.defaultInitContainers.prepareDirs.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultInitContainers.prepareDirs.resources }}
          resources: {{- toYaml .Values.defaultInitContainers.prepareDirs.resources | nindent 12 }}
          {{- else if ne .Values.defaultInitContainers.prepareDirs.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.defaultInitContainers.prepareDirs.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /public
              subPath: app-public-dir
        {{- end }}
        {{- if .Values.defaultInitContainers.waitForBackends.enabled }}
        - name: wait-for-backend
          image: {{ template "mastodon.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/bash
            - -ec
          args:
            - /scripts/wait-for-backend.sh
          {{- if .Values.defaultInitContainers.waitForBackends.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.defaultInitContainers.waitForBackends.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultInitContainers.waitForBackends.resources }}
          resources: {{- toYaml .Values.defaultInitContainers.waitForBackends.resources | nindent 12 }}
          {{- else if ne .Values.defaultInitContainers.waitForBackends.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.defaultInitContainers.waitForBackends.resourcesPreset) | 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 and .Values.initJob.precompileAssets.enabled .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 }}
          envFrom:
            - configMapRef:
                name: {{ include "mastodon.defaultConfigmapName" . }}
            - secretRef:
                name: {{ include "mastodon.defaultSecretName" . }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: scripts
              mountPath: /scripts
        {{- 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 .Values.initJob.migrateAndCreateAdmin.enabled }}
        - 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.migrateAndCreateAdmin.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.initJob.migrateAndCreateAdmin.containerSecurityContext "context" $) | 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.migrateAndCreateAdmin.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.migrateAndCreateAdmin.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.migrateAndCreateAdmin.extraEnvVarsCM .Values.initJob.migrateAndCreateAdmin.extraEnvVarsSecret }}
            {{- if .Values.initJob.migrateAndCreateAdmin.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.initJob.migrateAndCreateAdmin.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.initJob.migrateAndCreateAdmin.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.initJob.migrateAndCreateAdmin.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: scripts
              mountPath: /scripts
          {{- if .Values.initJob.migrateAndCreateAdmin.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.migrateAndCreateAdmin.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.initJob.migrateAndCreateAdmin.resources }}
          resources: {{- toYaml .Values.initJob.migrateAndCreateAdmin.resources | nindent 12 }}
          {{- else if ne .Values.initJob.migrateAndCreateAdmin.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.initJob.migrateAndCreateAdmin.resourcesPreset) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.initJob.precompileAssets.enabled }}
        - name: mastodon-assets-precompile
          image: {{ template "mastodon.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - /bin/bash
            - -ec
          args:
            - /scripts/precompile-assets.sh
          {{- if .Values.initJob.precompileAssets.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.initJob.precompileAssets.containerSecurityContext "context" $) | 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.precompileAssets.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.precompileAssets.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.precompileAssets.extraEnvVarsCM .Values.initJob.precompileAssets.extraEnvVarsSecret }}
            {{- if .Values.initJob.precompileAssets.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.initJob.precompileAssets.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.initJob.precompileAssets.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.initJob.precompileAssets.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /opt/bitnami/mastodon/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mastodon/public
              subPath: app-public-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /.cache
              subPath: cache-dir
            - name: scripts
              mountPath: /scripts
          {{- if not .Values.enableS3 }}
            - name: data
              mountPath: {{ .Values.persistence.mountPath }}
          {{- end }}
          {{- if .Values.initJob.precompileAssets.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.initJob.precompileAssets.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.initJob.precompileAssets.resources }}
          resources: {{- toYaml .Values.initJob.precompileAssets.resources | nindent 12 }}
          {{- else if ne .Values.initJob.precompileAssets.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.initJob.precompileAssets.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - 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.initJob.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.initJob.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
