Helm _helpers.tpl:在其他模板定义中调用已定义的模板

问题描述 投票:9回答:2

Helm _helpers.tpl?

Helm允许在Kubernetes的资源文件中使用Go templating

名为_helpers.tpl的文件通常用于使用以下语法定义Go模板助手:

{{- define "yourFnName" -}}
{{- printf "%s-%s" .Values.name .Values.version | trunc 63 -}}
{{- end -}}

然后您可以在*.yaml资源文件中使用,如下所示:

{{ template "yourFnName" . }}

The Question

如何在其他帮助程序定义中使用我定义的帮助程序?

例如,如果我有一个应用程序名称的帮助程序,并希望在定义中使用它来确定入口主机名?

我尝试过几种不同的方式在其他定义中调用助手。鉴于此基本辅助功能:

{{- 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 -}}

有可能做到这一点吗?如果是这样,怎么样?

templates go kubernetes go-templates kubernetes-helm
2个回答
6
投票

你应该使用Nested template definitions

在您的特定情况下,它是:

{{- define "host" -}}
{{ template "name" . }}.example.com
{{- end -}}

4
投票

您可以使用(include ... )语法。包含先前定义的模板foo的示例:

{{- define "bar" -}}
{{- printf "%s-%s" (include "foo" .) .Release.Namespace | trunc 63 | trimSuffix "-" -}}
{{- end -}}
© www.soinside.com 2019 - 2024. All rights reserved.