我有这样的目录结构:
helm
|-->mappings
|--> foo
foo1.yaml foo2.yaml
|-->templates
mapping.yaml
values.yaml
其中values.yaml
由于{{ .Values.data.hostname }}
和mapping.yaml
{{- $files := .Files }}
{{- range .Values.mappings.foo }}
{{- $genericfilepath := printf "mappings/foo/%s.yaml" . }}
{{ $files.Get $genericfilepath }}
{{- end }}
当前,mapping.yaml文件循环遍历指定目录并加载yaml文件,但是我无法访问Values变量。
我还尝试了将charts.yaml文件放在helm / mappings / foo / values.yaml下的子图表,但它也无法解决,或者我不是100%理解子图表是否是解决文件的正确解决方案路径
答案 0 :(得分:0)
在Go text/template
language中,.
是一个特殊的“上下文”变量,像.Files
或.Values
这样的引用实际上是从.
检索字段。例如,您可以编写一个示例模板:
{{- $dot := . -}}
# These both print the same value
dot-values-foo: {{ .Values.foo }}
dollars-dot-values-foo: {{ $dot.Values.foo }}
.
的特殊方式之一是,range
语句在遍历集合时为每个项目设置.
。在您的示例中:
{{/* . is the top item; .Values is valid */}}
{{- range .Values.mappings.foo }}
{{/* . is one of the items in `mappings.foo` */}}
{{- end }}
{{/* . is the top item again */}}
如果出于某种特殊目的需要使用.
,则倾向于将原始的顶层项目保存在变量中,然后可以在其中引用字段。
{{- $top := . }}
{{- range .Values.mappings.foo }}
{{- $genericfilepath := printf "mappings/foo/%s.yaml" . }}
{{ $top.Files.Get $genericfilepath }}
{{ index $top.Values.enabled . }}
{{- end }}