Helm模板创建dockerconfigjson Secret报错:无法反序列化字符串为map[string][]uint8
问题分析:Kubernetes Secret创建失败错误排查
相关配置
values.yaml
privateregistries: - registry: internal1.local username: "sxs" token: "sxs" - registry: internal2.local username: "sxs" token: "sxs"
Helm模板文件
{{- $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) }}
错误信息
应用模板时出现错误:
cannot unmarshal string into Go struct field Secret.data of type map[string][]uint8
错误原因
问题出在.dockerconfigjson字段的取值处理上:你用quote函数把Base64编码后的字符串包裹成了带双引号的格式,但Kubernetes要求Secret的data字段中,每个值必须是纯Base64编码的原始字符串,不能额外添加引号。
添加quote后,最终生成的YAML里该字段的值会变成类似"abcdef..."的形式,Kubernetes解析时会把这个带引号的内容识别为普通字符串,而非预期的Base64字节数组,因此触发了反序列化失败的错误。
修复方法很简单,去掉quote函数,直接输出b64enc $json的结果即可:
data: .dockerconfigjson: {{- b64enc $json }}
内容的提问来源于stack exchange,提问作者Chamisxs
相关产品推荐
相关产品推荐

