{{/* Use the bootstrap password from values.yaml if an existing secret is not found */}}
{{- $bootstrapPassword := .Values.bootstrapPassword -}}
{{- $existingSecret := lookup "v1" "Secret" .Release.Namespace "bootstrap-secret" -}}
{{- if $existingSecret -}}
    {{- if $existingSecret.data -}}
        {{- if $existingSecret.data.bootstrapPassword -}}
            {{- $bootstrapPassword = $existingSecret.data.bootstrapPassword | b64dec -}}
        {{- end -}}
    {{- end -}}
{{- end -}}
{{/* If a bootstrap password was found in the values or an existing password was found create the secret */}}
{{- if $bootstrapPassword }}
apiVersion: v1
kind: Secret
metadata:
  name: "bootstrap-secret"
  namespace: {{ .Release.Namespace }}
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-weight": "-5"
    "helm.sh/resource-policy": keep
type: Opaque
data:
  bootstrapPassword: {{ $bootstrapPassword | b64enc | quote }}
{{- end }}
