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

{{- if .Values.migration.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "common.names.fullname" . }}-migrate
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: migration
  {{- if or .Values.migration.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.migration.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  template:
    metadata:
      {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.migration.podLabels .Values.commonLabels ) "context" . ) }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: migration
      {{- if .Values.migration.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.migration.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      restartPolicy: OnFailure
      {{- if .Values.migration.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.migration.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.migration.automountServiceAccountToken }}
      {{- if .Values.migration.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.migration.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: harbor-migrate
          image: {{ template "harbor.core.image" . }}
          imagePullPolicy: {{ .Values.core.image.pullPolicy }}
          {{- if .Values.migration.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.migration.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.migration.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.migration.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - /opt/bitnami/scripts/harbor-core/run.sh
            - -mode=migrate
          {{- end }}
          {{- if .Values.migration.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.migration.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.core.image.debug | quote }}
            {{- if and (not .Values.postgresql.enabled) .Values.externalDatabase.existingSecret }}
            - name: POSTGRESQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalDatabase.existingSecret }}
                  key: {{ .Values.externalDatabase.existingSecretPasswordKey }}
            {{- end }}
            {{- if or .Values.core.configOverwriteJson .Values.core.configOverwriteJsonSecret }}
            - name: CONFIG_OVERWRITE_JSON
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.coreOverridesJsonSecret" . }}
                  key: overrides.json
            {{- end }}
            {{- if .Values.migration.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.migration.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ printf "%s-envvars" (include "harbor.core" .) }}
            - secretRef:
                {{- if .Values.core.existingEnvVarsSecret }}
                name: {{ .Values.core.existingEnvVarsSecret }}
                {{- else }}
                name: {{ printf "%s-envvars" (include "harbor.core" .) }}
                {{- end }}
            {{- if .Values.migration.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.migration.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.migration.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.migration.extraEnvVarsSecret }}
            {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /etc/ssl/certs
              subPath: etc-ssl-certs
            - name: config
              mountPath: /etc/core/app.conf
              subPath: app.conf
            - name: token-service-private-key
              mountPath: /etc/core/private_key.pem
              subPath: tls.key
            - name: secret-key
              mountPath: /etc/core/key
              subPath: key
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/core
            {{- end }}
            {{- if .Values.migration.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.migration.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          {{- if .Values.migration.resources }}
          resources: {{- toYaml .Values.migration.resources | nindent 12 }}
          {{- else if ne .Values.migration.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.migration.resourcesPreset) | nindent 12 }}
          {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config
          configMap:
            name: {{ include "harbor.core" . }}
            items:
              - key: app.conf
                path: app.conf
        - name: token-service-private-key
          secret:
            {{- if .Values.core.secretName }}
            secretName: {{ .Values.core.secretName }}
            {{- else }}
            secretName: {{ template "harbor.core" . }}
            {{- end }}
        - name: secret-key
          secret:
            {{- if .Values.core.existingSecret }}
            secretName: {{ .Values.core.existingSecret }}
            items:
              - key: secretKey
                path: key
            {{- else }}
            secretName: {{ include "harbor.core" . }}
            items:
              - key: secretKey
                path: key
            {{- end }}
        {{- if .Values.internalTLS.caBundleSecret }}
        {{- include "harbor.caBundleVolume" . | nindent 8 }}
        {{- end }}
        {{- if .Values.internalTLS.enabled }}
        - name: internal-tls-certs
          secret:
            secretName: {{ include "harbor.core.tls.secretName" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
