dongzi3434 2017-10-12 21:21
浏览 879
已采纳

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

Helm _helpers.tpl?

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

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

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

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?

  • 写回答

2条回答 默认 最新

  • dounao1856 2017-10-31 14:41
    关注

    You should use Nested template definitions.

    In your particular case it is:

    {{- define "host" -}}
    {{ template "name" . }}.example.com
    {{- end -}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?