Helm允许在Kubernetes的资源文件中使用Go templating。
名为_helpers.tpl
的文件通常用于使用以下语法定义Go模板助手:
{{- define "yourFnName" -}}
{{- printf "%s-%s" .Values.name .Values.version | trunc 63 -}}
{{- end -}}
然后您可以在*.yaml
资源文件中使用,如下所示:
{{ template "yourFnName" . }}
如何在其他帮助程序定义中使用我定义的帮助程序?
例如,如果我有一个应用程序名称的帮助程序,并希望在定义中使用它来确定入口主机名?
我尝试过几种不同的方式在其他定义中调用助手。鉴于此基本辅助功能:
{{- define "host" -}}
{{- printf "%.example.com" <Somehow get result of "name" helper here> -}}
{{- end -}}
我尝试过以下方法:
{{- printf "%.example.com" {{ template "name" . }} -}}
{{- printf "%.example.com" {{- template "name" . -}} -}}
{{- printf "%.example.com" ( template "name" . ) -}}
{{- printf "%.example.com" template "name" . -}}
# Separator
{{- $name := {{ template "environment" . }} -}}
{{- printf "%.example.com" $name -}}
# Separator
{{- $name := template "environment" . -}}
{{- printf "%.example.com" $name -}}
# Separator
{{- $name := environment -}}
{{- printf "%.example.com" $name -}}
有可能做到这一点吗?如果是这样,怎么样?
你应该使用Nested template definitions。
在您的特定情况下,它是:
{{- define "host" -}}
{{ template "name" . }}.example.com
{{- end -}}
您可以使用(include ... )
语法。包含先前定义的模板foo
的示例:
{{- define "bar" -}}
{{- printf "%s-%s" (include "foo" .) .Release.Namespace | trunc 63 | trimSuffix "-" -}}
{{- end -}}