{{- $secretName := "db-details" }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretName }}
data:
  {{/*
  Lookup for the user shouldn't be needed in normal circumstances
  But there was a typo before that resulted to a db-user with weird
  characters. So to keep that user for existing installations we retrieve
  it from the existing secret.
  */}}
  {{/* Init values */}}
  {{- $dbUser := ((include "postgres.DatabaseName" .Values) | b64enc) -}}
  {{- $dbPass := (randAlphaNum 15 | b64enc) -}}
  {{- with (lookup "v1" "Secret" .Release.Namespace $secretName) -}}
    {{/* If there is a previous secret, use that */}}
    {{- $dbUser = (index .data "db-user") -}}
    {{- $dbPass = (index .data "db-password") -}}
  {{- end }}
  db-user: {{ $dbUser }}
  db-password: {{ $dbPass }}

{{/* Temprary store them on values to display it on NOTES */}}
{{ $_ := set .Values "nextcloudDbPass" $dbPass }}
{{ $_ := set .Values "nextcloudDbUser" $dbUser }}
