Helm _helpers.tpl:在其他模板定义中调用已定义的模板 [英] Helm _helpers.tpl: Calling defined templates in other template definitions

查看:1664
本文介绍了Helm _helpers.tpl:在其他模板定义中调用已定义的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helm允许在Kubernetes的资源文件中使用转到模板.

Helm allows for the use of Go templating in resource files for Kubernetes.

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

A file named _helpers.tpl is usually used to define Go template helpers with this syntax:

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

然后您可以像这样在*.yaml资源文件中使用它:

Which you can then use in your *.yaml resource files like so:

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

我尝试了以下操作:

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

推荐答案

您应使用嵌套模板定义.

在您的特定情况下是:

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

这篇关于Helm _helpers.tpl:在其他模板定义中调用已定义的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆