{{- $ref := required "Credential mount requires controlPathRef" $mount.controlPathRef -}}
{{- if not (hasKey $paths $ref) }}{{ fail "Credential mount names an undeclared control path" }}{{ end -}}
{{- if hasKey $mounted $ref }}{{ fail "Duplicate credential mount for a control path" }}{{ end -}}
{{- $directory := required "Credential mount requires mountPath" $mount.mountPath -}}
{{- $key := required "Credential mount requires key" $mount.key -}}
{{- if hasKey $directories $directory }}{{ fail "Credential mount directories must be unique" }}{{ end -}}
{{- $_ := set $directories $directory true -}}
{{- if or (not (hasPrefix "/etc/openop/kubeconfigs/" $directory)) (ne (clean $directory) $directory) }}{{ fail "Credential mountPath must be a directory under /etc/openop/kubeconfigs/" }}{{ end -}}
{{- if or (not (regexMatch "^[A-Za-z0-9._-]+$" $key)) (hasPrefix ".." $key) (eq $key ".") }}{{ fail "Credential key must be a safe Secret file key" }}{{ end -}}
{{- if not $mount.existingSecret }}{{ fail "Credential mount requires existingSecret" }}{{ end -}}
{{- $config := get $paths $ref -}}
{{- if or $config.in_cluster (ne (default "" $config.kubeconfig_path) (printf "%s/%s" $directory $key)) }}{{ fail "Credential mount does not match its bootstrap kubeconfig_path" }}{{ end -}}
{{- $_ := set $mounted $ref true -}}
{{- end -}}
{{- range $ref, $config := $paths -}}
{{- if and $config.kubeconfig_path (not (hasKey $mounted $ref)) }}{{ fail "Bootstrap kubeconfig_path requires a matching credential mount" }}{{ end -}}
{{- if and $config.runtime_directory (ne $config.runtime_directory "/var/run/openop-tfsdk") }}{{ fail "This chart mounts runtime_directory at /var/run/openop-tfsdk" }}{{ end -}}