在命名模板中使用父作用域中定义的变量
Use defined variables from parent scope in a named template
我正在尝试向 include
d 的命名模板提供对模板中定义的值的惯用访问。这么多有效:
# values.yml
versions:
- 1.2.3.4
- 5.6.7.8
# _helpers.tmpl
{{/*
Get the service path from the version string (passed as ".")
*/}}
{{- define "sample.servicePath" -}}
{{- $pat := "^[0-9]+\.[0-9]+\.([0-9]+)\.[0-9]+$" }}
{{- $version := . | trimAll "\"" }}
{{- if regexMatch $pat $version }}
{{- regexReplaceAll $pat $version "" }}
{{- else }}
{{- printf "Can't extract service number from version string %s" . | fail -}}
{{- end }}
{{- end }}
# sample.yml
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
# Here is the servicePath: {{ $servicePath }} which works
{{- end }}
但我想这也行得通:
# _helpers.tmpl (further down in the file)
{{- define "sample.labels" -}}
servicePath: {{ $servicePath }}
{{- end }}
# sample.yml
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
# Here is the servicePath: {{ $servicePath }} which still works
# but these labels: {{ include "sample.labels" . }} do not
{{- end }}
最终我不介意实现,我只需要能够获取单个值列表,对它们进行范围(以创建多个资源)并解析它们的值。
Go text/template 语言,以及 Helm 图表的扩展,没有全局变量或动态范围;你不能按照你建议的方式做到这一点。每个模板调用都有自己的范围,它唯一能看到的是它的单个参数 .
和它在本地声明的任何变量。
但是,Helm 包括 Sprig 扩展库,其中包括 list
和 dict
函数来创建复合值。您可以使用它们将多个值传递到模板中:
{{- define "sample.labels" -}}
{{- $top := index . 0 -}}
{{- $servicePath := index . 1 -}}
app.kubernetes.io/instance: {{ $top.Release.Name }}
servicePath: {{ $servicePath }}
{{ end -}}
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
{{- include "sample.labels" (list . $servicePath) }}
在最后一行中,我们使用两个参数列表调用模板,顶级 Helm 对象(此处为 .
)和 $servicePath
局部变量。 sample.labels
模板的顶部然后将其解包回其自己模板中的两个局部变量,然后使用它们生成一组参考标签。
我正在尝试向 include
d 的命名模板提供对模板中定义的值的惯用访问。这么多有效:
# values.yml
versions:
- 1.2.3.4
- 5.6.7.8
# _helpers.tmpl
{{/*
Get the service path from the version string (passed as ".")
*/}}
{{- define "sample.servicePath" -}}
{{- $pat := "^[0-9]+\.[0-9]+\.([0-9]+)\.[0-9]+$" }}
{{- $version := . | trimAll "\"" }}
{{- if regexMatch $pat $version }}
{{- regexReplaceAll $pat $version "" }}
{{- else }}
{{- printf "Can't extract service number from version string %s" . | fail -}}
{{- end }}
{{- end }}
# sample.yml
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
# Here is the servicePath: {{ $servicePath }} which works
{{- end }}
但我想这也行得通:
# _helpers.tmpl (further down in the file)
{{- define "sample.labels" -}}
servicePath: {{ $servicePath }}
{{- end }}
# sample.yml
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
# Here is the servicePath: {{ $servicePath }} which still works
# but these labels: {{ include "sample.labels" . }} do not
{{- end }}
最终我不介意实现,我只需要能够获取单个值列表,对它们进行范围(以创建多个资源)并解析它们的值。
Go text/template 语言,以及 Helm 图表的扩展,没有全局变量或动态范围;你不能按照你建议的方式做到这一点。每个模板调用都有自己的范围,它唯一能看到的是它的单个参数 .
和它在本地声明的任何变量。
但是,Helm 包括 Sprig 扩展库,其中包括 list
和 dict
函数来创建复合值。您可以使用它们将多个值传递到模板中:
{{- define "sample.labels" -}}
{{- $top := index . 0 -}}
{{- $servicePath := index . 1 -}}
app.kubernetes.io/instance: {{ $top.Release.Name }}
servicePath: {{ $servicePath }}
{{ end -}}
{{- range $version := .Values.versions }}
{{- $servicePath := (include "sample.servicePath" $version) }}
{{- include "sample.labels" (list . $servicePath) }}
在最后一行中,我们使用两个参数列表调用模板,顶级 Helm 对象(此处为 .
)和 $servicePath
局部变量。 sample.labels
模板的顶部然后将其解包回其自己模板中的两个局部变量,然后使用它们生成一组参考标签。