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

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