apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "common.names.fullname" . }}-migrate
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: server
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.migration.annotations "context" $ ) | nindent 4 }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: migration
        {{- if .Values.migration.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.migration.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- 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: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- 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: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          env:
            - 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: {{ ternary "postgres" "cassandra" (eq .Values.database "postgresql") | quote }}
            {{- if (eq .Values.database "postgresql") }}
            {{- if .Values.postgresql.auth.usePasswordFiles }}
            - name: KONG_POSTGRESQL_PASSWORD_FILE
              value: {{ printf "/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.cassandra.usePasswordFile }}
            - name: KONG_CASSANDRA_PASSWORD_FILE
              value: {{ printf "/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 }}
          {{- if .Values.migration.extraVolumeMounts }}
          volumeMounts:
            {{- include "common.tplvalues.render" (dict "value" .Values.migration.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.migration.resources }}
          resources: {{- toYaml .Values.migration.resources | nindent 12 }}
          {{- end }}
      {{- if .Values.extraVolumes }}
      volumes:
      {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 6 }}
      {{- end }}
