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

{{- if and .Values.passwordUpdateJob.enabled (eq ( include "mysql.createPreviousSecret" . ) "true") }}
{{- $rootPassword := .Values.passwordUpdateJob.previousPasswords.rootPassword }}
{{- $password := .Values.passwordUpdateJob.previousPasswords.password }}
{{- $replicationPassword := .Values.passwordUpdateJob.previousPasswords.replicationPassword }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-previous-secret" (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: mysql
  {{- $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 }}
type: Opaque
data:
  mysql-root-password: {{ required "The previous root password is required!" $rootPassword | b64enc | quote }}
  {{- if .Values.auth.username }}
  mysql-password: {{ required "The previous user password is required!" $password | b64enc | quote }}
  {{- end }}
  {{- if eq .Values.architecture "replication" }}
  mysql-replication-password: {{ required "The previous replication password is required!" $replicationPassword | b64enc | quote }}
  {{- end }}
{{- end }}
