gpt4 book ai didi

templates - Helm _helpers.tpl : Calling defined templates in other template definitions

转载 作者:IT老高 更新时间:2023-10-28 13:08:25 27 4
gpt4 key购买 nike

Helm _helpers.tpl?

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

一个名为 _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 -}}

甚至可以做到这一点吗?如果有,怎么做?

最佳答案

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

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

关于templates - Helm _helpers.tpl : Calling defined templates in other template definitions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719082/

27 4 0