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

{{- $host := include "mysql.primary.fullname" . }}
{{- $port := print .Values.primary.service.ports.mysql }}
{{- $rootPassword := include "common.secrets.passwords.manage" (dict "secret" (include "mysql.secretName" .) "key" "mysql-root-password" "length" 10 "providedValues" (list "auth.rootPassword") "honorProvidedValues" true "context" $) | trimAll "\"" | b64dec }}
{{- $password := include "common.secrets.passwords.manage" (dict "secret" (include "mysql.secretName" .) "key" "mysql-password" "length" 10 "providedValues" (list "auth.password") "honorProvidedValues" true "context" $) | trimAll "\"" | b64dec }}
{{- if eq (include "mysql.createSecret" .) "true" }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mysql
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: Opaque
data:
  mysql-root-password: {{ print $rootPassword | b64enc | quote }}
  mysql-password: {{ print $password | b64enc | quote }}
  {{- if eq .Values.architecture "replication" }}
  mysql-replication-password: {{ include "common.secrets.passwords.manage" (dict "secret" (include "common.names.fullname" .) "key" "mysql-replication-password" "length" 10 "providedValues" (list "auth.replicationPassword") "honorProvidedValues" true "context" $) }}
  {{- end }}
{{- end }}
{{- if .Values.serviceBindings.enabled }}
{{- $database := .Values.auth.database  }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}-svcbind-root
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mysql
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: servicebinding.io/mysql
data:
  provider: {{ print "bitnami" | b64enc | quote }}
  type: {{ print "mysql" | b64enc | quote }}
  host: {{ print $host | b64enc | quote }}
  port: {{ print $port | b64enc | quote }}
  username: {{ print "root" | b64enc | quote }}
  {{- if $database }}
  database: {{ print $database | b64enc | quote }}
  {{- end }}
  password: {{ print $rootPassword | b64enc | quote }}
  uri: {{ printf "mysql://root:%s@%s:%s/%s" $rootPassword $host $port $database | b64enc | quote }}

{{- if .Values.auth.username }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "common.names.fullname" . }}-svcbind-custom-user
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: mysql
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
type: servicebinding.io/mysql
data:
  provider: {{ print "bitnami" | b64enc | quote }}
  type: {{ print "mysql" | b64enc | quote }}
  host: {{ print $host | b64enc | quote }}
  port: {{ print $port | b64enc | quote }}
  username: {{ print .Values.auth.username | b64enc | quote }}
  {{- if $database }}
  database: {{ print $database | b64enc | quote }}
  {{- end }}
  password: {{ print $password | b64enc | quote }}
  uri: {{ printf "mysql://%s:%s@%s:%s/%s" .Values.auth.username $password $host $port $database | b64enc | quote }}
{{- end }}
{{- end }}