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

{{- if or (eq .Values.database "postgresql") (eq .Values.database "cassandra") }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "common.names.fullname" . }}-migrate
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: server
  {{- 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 "kong.imagePullSecrets" . | nindent 6 }}
      restartPolicy: OnFailure
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.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: kong-migrate
          image: {{ template "kong.migration.image" . }}
          imagePullPolicy: {{ .Values.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 }}
          {{- end }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: KONG_PREFIX
              value: /opt/bitnami/kong/server
            - name: KONG_MIGRATE
              value: "yes"
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: KONG_EXIT_AFTER_MIGRATE
              value: "yes"
            - name: KONG_DATABASE
              value: {{ include "kong.database" . | quote }}
            {{- if (eq .Values.database "postgresql") }}
            {{- if .Values.usePasswordFiles }}
            - name: KONG_PG_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/kong/secrets/%s" (include "kong.postgresql.databaseSecretKey" .) }}
            {{- else }}
            - name: KONG_PG_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kong.postgresql.secretName" . }}
                  key: {{ include "kong.postgresql.databaseSecretKey" . }}
            {{- end }}
            - name: KONG_PG_HOST
              value: {{ include "kong.postgresql.host" . }}
            - name: KONG_PG_PORT
              value: {{ include "kong.postgresql.port" . }}
            - name: KONG_PG_USER
              value: {{ include "kong.postgresql.user" . }}
            {{- end }}
            {{- if (eq .Values.database "cassandra") }}
            {{- if .Values.usePasswordFiles }}
            - name: KONG_CASSANDRA_PASSWORD_FILE
              value: {{ printf "/opt/bitnami/kong/secrets/%s" (include "kong.cassandra.databaseSecretKey" .) }}
            {{- else }}
            - name: KONG_CASSANDRA_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "kong.cassandra.secretName" . }}
                  key: {{ include "kong.cassandra.databaseSecretKey" . }}
            {{- end }}
            - name: KONG_CASSANDRA_CONTACT_POINTS
              value: {{ include "kong.cassandra.contactPoints" . }}
            - name: KONG_CASSANDRA_PORT
              value: {{ include "kong.cassandra.port" . | quote }}
            - name: KONG_CASSANDRA_USER
              value: {{ include "kong.cassandra.user" . | quote }}
            {{- end }}
            {{- if .Values.migration.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.migration.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.migration.extraEnvVarsCM .Values.migration.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.migration.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.migration.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.migration.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.migration.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/kong/server
              subPath: app-server-dir
            {{- if and .Values.usePasswordFiles (or (eq .Values.database "cassandra") (eq .Values.database "postgresql")) }}
            - name: kong-secrets
              mountPath: /opt/bitnami/kong/secrets
            {{- 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: {}
        {{- if and .Values.usePasswordFiles (or (eq .Values.database "cassandra") (eq .Values.database "postgresql")) }}
        - name: kong-secrets
          secret:
            {{- if (eq .Values.database "cassandra") }}
            secretName: {{ include "kong.cassandra.secretName" . }}
            {{- else if (eq .Values.database "postgresql") }}
            secretName: {{ include "kong.postgresql.secretName" . }}
            {{- end }}
        {{- end -}}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
