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

{{- if .Values.passwordUpdateJob.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ printf "%s-password-update" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mariadb
    app.kubernetes.io/component: update-job
  {{- $defaultAnnotations := dict "helm.sh/hook" "pre-upgrade" "helm.sh/hook-delete-policy" "hook-succeeded" }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonAnnotations $defaultAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
spec:
  backoffLimit: {{ .Values.passwordUpdateJob.backoffLimit }}
  template:
    metadata:
      {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.passwordUpdateJob.podLabels .Values.commonLabels ) "context" . ) }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: mariadb
        app.kubernetes.io/component: update-job
      {{- if .Values.passwordUpdateJob.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "mariadb.imagePullSecrets" . | nindent 6 }}
      restartPolicy: OnFailure
      {{- if .Values.passwordUpdateJob.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.passwordUpdateJob.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.passwordUpdateJob.automountServiceAccountToken }}
      {{- if .Values.passwordUpdateJob.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.passwordUpdateJob.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: update-credentials
          image: {{ template "mariadb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.passwordUpdateJob.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /bin/bash
            - -ec
          {{- end }}
          {{- if .Values.passwordUpdateJob.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - |
              {{- if .Values.usePasswordFiles }}
              # We need to load all the secret env vars to the system
              for file in $(find /bitnami/mariadb/secrets -type f); do
                  env_var_name="$(basename $file)"
                  echo "Exporting $env_var_name"
                  export $env_var_name="$(< $file)"
              done
              {{- end }}

              . /opt/bitnami/scripts/mariadb-env.sh
              . /opt/bitnami/scripts/libmariadb.sh
              . /opt/bitnami/scripts/liblog.sh

              primary_host={{ include "mariadb.primary.fullname" . }}-0.{{  printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
              info "Starting password update job"
              if [[ -f /job-status/root-password-changed ]]; then
                  info "Root password already updated. Skipping"
              else
                  info "Updating root password"
                  echo "SET PASSWORD for 'root'@'%' = PASSWORD('$MARIADB_NEW_ROOT_PASSWORD');" | mysql_remote_execute $primary_host {{ .Values.primary.containerPorts.mysql }} "" root $MARIADB_PREVIOUS_ROOT_PASSWORD
                  touch /job-status/root-password-changed
                  info "Root password successfully updated"
              fi
              {{- if not (empty .Values.auth.username) }}
              if [[ -f /job-status/password-changed ]]; then
                  info "User password already updated. Skipping"
              else
                  info "Updating user password"
                  echo "SET PASSWORD for '$MARIADB_USER'@'%' = PASSWORD('$MARIADB_NEW_PASSWORD');" | mysql_remote_execute $primary_host {{ .Values.primary.containerPorts.mysql }} "" $MARIADB_USER $MARIADB_PREVIOUS_PASSWORD
                  touch /job-status/password-changed
                  info "User password successfully updated"
              fi
              {{- end }}
              {{- if eq .Values.architecture "replication" }}
              if [[ -f /job-status/replication-password-changed ]]; then
                  info "Replication password already updated. Skipping"
              else
                  info "Updating replication password"
                  echo "SET PASSWORD for '$MARIADB_REPLICATION_USER'@'%' = PASSWORD('$MARIADB_NEW_REPLICATION_PASSWORD');" | mysql_remote_execute $primary_host {{ .Values.primary.containerPorts.mysql }} "" $MARIADB_REPLICATION_USER  $MARIADB_PREVIOUS_REPLICATION_PASSWORD
                  touch /job-status/replication-password-changed
                  info "Replication password successfully updated"
              fi

              for i in $(seq 0 {{ sub .Values.secondary.replicaCount 1 }}); do
                  if [[ -f /job-status/replica-$i-changed ]]; then
                      info "Replica $i already updated. Skipping"
                  else
                      replica_host={{ include "mariadb.secondary.fullname" . }}-$i.{{  printf "%s-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
                      info "Updating primary password in replica $i"
                      echo "STOP SLAVE; CHANGE MASTER TO MASTER_PASSWORD='$MARIADB_NEW_REPLICATION_PASSWORD'; START SLAVE;" | mysql_remote_execute $replica_host {{ .Values.secondary.containerPorts.mysql }} "" root $MARIADB_NEW_ROOT_PASSWORD
                      touch /job-status/replica-$i-changed
                      info "Replica $i updated"
                  fi
              done
              {{- end }}
              {{- if .Values.passwordUpdateJob.extraCommands }}
              info "Running extra commmands"
              {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.extraCommands "context" $) | nindent 14 }}
              {{- end }}
              info "Password update job finished successfully"
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if not .Values.auth.usePasswordFiles }}
            - name: MARIADB_PREVIOUS_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.previousSecretName" . }}
                  key: mariadb-root-password
            - name: MARIADB_NEW_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.newSecretName" . }}
                  key: mariadb-root-password
            {{- end }}
            {{- if not (empty .Values.auth.username) }}
            - name: MARIADB_USER
              value: {{ .Values.auth.username | quote }}
            {{- if not .Values.auth.usePasswordFiles }}
            - name: MARIADB_PREVIOUS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.previousSecretName" . }}
                  key: mariadb-password
            - name: MARIADB_NEW_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.newSecretName" . }}
                  key: mariadb-password
            {{- end }}
            {{- end }}
            {{- if eq .Values.architecture "replication" }}
            - name: MARIADB_REPLICATION_USER
              value: {{ .Values.auth.replicationUser | quote }}
            {{- if not .Values.auth.usePasswordFiles }}
            - name: MARIADB_PREVIOUS_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.previousSecretName" . }}
                  key: mariadb-replication-password
            - name: MARIADB_NEW_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "mariadb.update-job.newSecretName" . }}
                  key: mariadb-replication-password
            {{- end }}
            {{- end }}
            {{- if .Values.passwordUpdateJob.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.passwordUpdateJob.extraEnvVarsCM .Values.passwordUpdateJob.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.passwordUpdateJob.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.passwordUpdateJob.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.passwordUpdateJob.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.passwordUpdateJob.extraEnvVarsSecret }}
            {{- end }}
          {{- end }}
          {{- if .Values.passwordUpdateJob.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.passwordUpdateJob.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.passwordUpdateJob.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.passwordUpdateJob.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.passwordUpdateJob.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /job-status
              subPath: job-dir
            {{- if .Values.usePasswordFiles }}
            - name: mariadb-previous-credentials
              mountPath: /bitnami/mariadb/secrets/previous
            - name: mariadb-new-credentials
              mountPath: /bitnami/mariadb/secrets/new
            {{- end }}
          {{- if .Values.passwordUpdateJob.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.passwordUpdateJob.resources }}
          resources: {{- toYaml .Values.passwordUpdateJob.resources | nindent 12 }}
          {{- else if ne .Values.passwordUpdateJob.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.passwordUpdateJob.resourcesPreset) | nindent 12 }}
          {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if and .Values.auth.usePasswordFiles }}
        - name: mariadb-previous-credentials
          secret:
            secretName: {{ template "mariadb.update-job.previousSecretName" . }}
            items:
              - key: mariadb-root-password
                path: MARIADB_PREVIOUS_ROOT_PASSWORD
              {{- if .Values.auth.username }}
              - key: mariadb-password
                path: MARIADB_PREVIOUS_PASSWORD
              {{- end }}
              {{- if eq .Values.architecture "replication" }}
              - key: mariadb-replication-password
                path: MARIADB_PREVIOUS_REPLICATION_PASSWORD
              {{- end }}
        - name: mariadb-new-credentials
          secret:
            secretName: {{ template "mariadb.update-job.newSecretName" . }}
            items:
              - key: mariadb-root-password
                path: MARIADB_NEW_ROOT_PASSWORD
              {{- if .Values.auth.username }}
              - key: mariadb-password
                path: MARIADB_NEW_PASSWORD
              {{- end }}
              {{- if eq .Values.architecture "replication" }}
              - key: mariadb-replication-password
                path: MARIADB_NEW_REPLICATION_PASSWORD
              {{- end }}
        {{- end }}
      {{- if .Values.passwordUpdateJob.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.passwordUpdateJob.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
