{{ $pgValues := (. | mustDeepCopy) }}
{{ $_ := set $pgValues "common" (dict "nameSuffix" (include "postgres.nameSuffix" .)) }}

{{ $dbPass := randAlphaNum 32 | b64enc }}

apiVersion: v1
kind: Secret
metadata:
  name: {{ include "postgres.secretName" . }}
data:
  {{ with (lookup "v1" "Secret" .Release.Namespace (include "postgres.secretName" .)) }}
  {{ $dbPass = (index .data "db_password") }}
  {{ end }}
  db_password: {{ $dbPass }}

  db_user: {{ include "postgres.dbUser" . | b64enc }}
  db_name: {{ include "postgres.dbName" . | b64enc }}

  postgresURL: {{ printf "postgres://%v:%v@%v-pg:5432/%v?sslmode=disable" (include "postgres.dbUser" .) ($dbPass | b64dec) (include "common.names.fullname" $pgValues) (include "postgres.dbName" .) | b64enc }}
  postgresHost: {{ printf "%v-pg" (include "common.names.fullname" $pgValues) | b64enc }}
