{{- $root := . -}}
{{- range $compName, $comp := .Values.components }}
{{- if $comp.secretsFile }}
{{- $sf := $comp.secretsFile }}
{{- if ne ($sf.existingSecret | default "") "" }}
{{- else if not $sf.key }}
{{- fail (printf "components.%s.secretsFile.key is required unless secretsFile.existingSecret is set" $compName) }}
{{- else if not (hasKey $sf "content") }}
{{- fail (printf "components.%s.secretsFile.content is required unless secretsFile.existingSecret is set" $compName) }}
{{- else }}
{{- $secretName := printf "%s-%s-secrets" (include "certs-ui.fullname" $root) $compName }}
{{- $existing := lookup "v1" "Secret" $root.Release.Namespace $secretName }}
{{- if and $sf.keep $existing }}
{{- else }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ $secretName }}
  namespace: {{ $root.Release.Namespace }}
  labels:
    {{- include "certs-ui.labels" $root | nindent 4 }}
    app.kubernetes.io/component: {{ $compName }}
  {{- if $sf.keep }}
  annotations:
    helm.sh/resource-policy: keep
  {{- end }}
type: Opaque
stringData:
  {{ $sf.key }}: |
{{- if default false $sf.tpl }}
{{ tpl ($sf.content | toString) $root | nindent 4 }}
{{- else }}
{{ $sf.content | nindent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
