I have defined a helm template like the one below to get a predefined set of private registries in values and create a dockerconfigjson type secret in the namespace if needed by copying the templates into the helm chart.
value.yaml
privateregistries:
- registry: internal1.local
username: "sxs"
token: "sxs"
- registry: internal2.local
username: "sxs"
token: "sxs"
template file
{{- $auths := dict }}
{{ range $key, $value := .Values.privateregistries }}
{{- $auth := printf "%s:%s" $value.username $value.token | b64enc }}
{{- $_ := set $auths $value.registry (dict "auth" $auth) }}
{{ end }}
{{- $json := dict "auths" $auths | toJson }}
apiVersion: v1
kind: Secret
metadata:
name: regcred
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: {{- quote (b64enc $json) }}
But when trying to apply, I see the error below. What could be the error?
cannot unmarshal string into Go struct field Secret.data of type map[string][]uint8
The hyphen in the final line makes the YAML structure invalid.
data:
.dockerconfigjson: {{- quote (b64enc $json) }}
# ^ this one
You can just remove it. You don't specifically need to quote
the value either. (YAML doesn't require the quotes and a base64 string won't have punctuation that potentially confuses YAML; if you do need to quote something, toJson
will be more robust.)
data:
.dockerconfigjson: {{ b64enc $json }}
The hyphen inside the curly braces causes the Go templating engine to remove all of the whitespace outside the curly braces. That puts the value directly up against the key, but the YAML syntax requires at least one space after the colon.
# original form, doesn't parse:
.dockerconfigjson:"e30="
# final form (without `quote`), works:
.dockerconfigjson: e30=
Running helm template --debug
will dump out the output of the template even if it's not valid YAML, which can occasionally help you to find problems like this. It tends to be more obvious with extra or missing hyphens at the start or end of whole lines where you can get lines joined together or missing indentation.