简体   繁体   English

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

[英]Helm _helpers.tpl: Calling defined templates in other template definitions

Helm _helpers.tpl? Helm _helpers.tpl?

Helm allows for the use of Go templating in resource files for Kubernetes. Helm允许在Kubernetes的资源文件中使用Go模板

A file named _helpers.tpl is usually used to define Go template helpers with this syntax: 名为_helpers.tpl的文件通常用于使用以下语法定义Go模板助手:

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

Which you can then use in your *.yaml resource files like so: 然后您可以在*.yaml资源文件中使用它,如下所示:

{{ template "yourFnName" . }}

The Question 问题

How can I use the helpers I define, in other helper definitions? 如何在其他帮助程序定义中使用我定义的帮助程序?

For example, what if I have a helper for the application name, and want to use that in the definition for a helper which determines the ingress host name? 例如,如果我有一个应用程序名称的帮助程序,并希望在定义中使用它来确定入口主机名?

I have tried calling helpers in other definitions a couple different ways. 我尝试过几种不同的方式在其他定义中调用助手。 Given this basic helper function: 鉴于此基本辅助功能:

{{- define "host" -}}
{{- printf "%.example.com" <Somehow get result of "name" helper here> -}}
{{- end -}}

I have tried the following: 我尝试过以下方法:

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

Is it possible to even do this? 有可能做到这一点吗? If so, how? 如果是这样,怎么样?

You should use Nested template definitions . 您应该使用嵌套模板定义

In your particular case it is: 在您的特定情况下,它是:

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

You can use (include ... ) syntax. 您可以使用(include ... )语法。 Example of including previously defined template foo : 包含先前定义的模板foo示例:

{{- define "bar" -}}
{{- printf "%s-%s" (include "foo" .) .Release.Namespace | trunc 63 | trimSuffix "-" -}}
{{- end -}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM