{{/* Make sure all variables are set properly */}}
{{- include "common.values.setup" . }}

{{/*
If there's an existing secret, reuse it, otherwise generate a new one.
*/}}
{{- define "traefik-forward-auth.secret" -}}
{{- $secret := (lookup "v1" "Secret" .Release.Namespace (include "common.names.fullname" .) ) -}}
  {{- if $secret -}}
    {{-  index $secret "data" "secret" -}}
  {{- else -}}
    {{- randAlphaNum 16 | b64enc | quote -}}
  {{- end -}}
{{- end -}}

{{/* Append the hardcoded settings */}}
{{- define "traefik-forward-auth.harcodedValues" -}}
{{- if not .Values.env.SECRET }}
env:
  SECRET:
    valueFrom:
      secretKeyRef:
        name: {{ include "common.names.fullname" . }}
        key: secret
{{- end }}
{{- end -}}
{{- $_ := mergeOverwrite .Values (include "traefik-forward-auth.harcodedValues" . | fromYaml) -}}

{{ include "common.all" . }}
