【问题标题】:Helm _helpers.tpl: Calling defined templates in other template definitionsHelm _helpers.tpl:在其他模板定义中调用已定义的模板
【发布时间】:2018-03-24 22:48:46
【问题描述】:

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" . }}

问题

如何在其他助手定义中使用我定义的助手?

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

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

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


    【解决方案1】:

    你应该使用Nested template definitions

    在您的特定情况下是:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      相关资源
      最近更新 更多