我正在尝试构建一个动态的configmap清单,使用文件夹和文件名遍历一个值文件列表。这是我的values.development.yaml的样子:
templates:
folder: "foldername"
files:
- filename1
- filename2
这是我的原始configmap,带有硬编码的文件名:
apiVersion: v1
kind: ConfigMap
metadata:
name: templates
binaryData:
filename1: {{ .Files.Get "foldername/filename1.zip" | b64enc }}
filename2: {{ .Files.Get "foldername/filename2.zip" | b64enc }}
这就是我尝试开始的方式,但是.File.Get函数不想在范围内运行,因为我可以在外部正常使用它。当我像这样使用它时:
apiVersion: v1
kind: ConfigMap
metadata:
name: templates
binaryData:
{{- range $.Values.templates.files }}
{{ . }}: {{ .Files.Get "foldername/filename1.zip" | b64enc }}
{{- end }}
获取此错误:
helm template template -f deploy/template/values.development.yaml deploy/template --debug
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH:
Error: template: template/templates/configmap.yaml:14:20: executing "template/templates/configmap.yaml" at <.Files.Get>: can't evaluate field Files in type interface {}
helm.go:81: [debug] template: template/templates/configmap.yaml:14:20: executing "template/templates/configmap.yaml" at <.Files.Get>: can't evaluate field Files in type interface {}
不用说,我不太懂go语言。你能给我一些关于如何正确建立这个列表的提示吗?谢谢!
发布于 2021-08-21 21:17:23
在Go text/template
语言中,.
是一个具有多种含义的特殊变量。值得注意的是,在range
循环中,.
成为循环中的当前项,因此.Files
引用当前循环项中的Files
字段,而不是顶级Helm对象。
您可以通过将.
或.Files
保存到循环外部的局部变量中来解决此问题:
binaryData:
{{- $files := .Files }}
{{- range $.Values.templates.files }}
{{ . }}: {{ $files.Get "foldername/filename1.zip" | b64enc }}
{{- end }}
现在在循环.
中是字符串filename1
,filename2
,...但是您已经将顶级.Files
对象保存到$files
局部变量中,所以您可以引用它。
https://stackoverflow.com/questions/68864596
复制相似问题