一、子模板的定义

在helm3中,子模板有两种定义方法:

  1. 在templates/xx.yaml文件中定义
  2. 在templates/xx.tpl文件内定义

定义子模板通过define定义,语法如下:

{{- define "子模板名" }}
...
{{- end }}

在实际使用中推荐在helpers.tpl文件中定义,结构更加清晰。

1. yaml文件中定义

[root@k8s-master m1]# cat chart6/templates/configmap.yaml
{{- define "childTemplates" }}
  labels:
    name: test
    date: {{ now | date "2006-01-02" }}
{{- end }}

2. tpl文件中定义

{{- define "childTemplates" }}
  labels:
    name: {{ .Values.labels.name }}
    date: {{ now | date "2006-01-02" }}
{{- end }}

注意:如果使用了templates/目录的.tpl文件,应该使用rm -rf templates/*删除掉当前目录内的所有内容,重新创建需要的文件,否则会出错。 报错内容为:

Error: INSTALLATION FAILED: template: chart6/templates/tests/test-connection.yaml:4:12: executing "chart6/templates/tests/test-connection.yaml" at <include "chart6.fullname" .>: error calling include: template: no template "chart6.fullname" associated with template "gotpl"
helm.go:88: [debug] template: chart6/templates/tests/test-connection.yaml:4:12: executing "chart6/templates/tests/test-connection.yaml" at <include "chart6.fullname" .>: error calling include: template: no template "chart6.fullname" associated with template "gotpl"

二、子模板的调用

子模板的调用有两种方式,分别是template和include,但更推荐使用include调用子模板。因为include支持管道,能够对返回值进行处理。

调用子模板语法如下:

{{- template/include "子模板名" . }}

.表示当前的作用域,其中调用的对象可以从整个chart的上下文中去获取值,比如values.yaml。

1. include调用(推荐)

调用后处理结果:在helpers.tpl文件中定义子模板,在主yaml文件中调用
并对结果进行处理

_helpers.tpl文件

[root@k8s-master m1]# cat mychart/templates/_helpers.tpl 
# 默认子模板为顶格对齐
{{- define "childTemplates" }}
labels:
  date: {{ now | date "2006-01-02" }}
{{- end }}

configmap.yaml文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
  {{- include "childTemplates" . | indent 2}}   # 缩进2个字符
data:
  value1: test

试运行结果

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart  
  labels:
    date: 2025-11-04
data:
  value1: test

包含对象的调用:在helpers.tpl文件中定义子模板,在主yaml文件中调用

values.yml文件

[root@k8s-master m1]# cat mychart/values.yaml 
labels:
  app: test

configmap.yaml文件

[root@k8s-master m1]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
  {{- include "childTemplates" . }}
data:
  value1: test

_helpers.tpl文件

[root@k8s-master m1]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
  {{- template "childTemplates" . }}   # 注意. 缺少会导致变量无法获取值
data:
  value1: test

试运行

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: mychart
  labels:
    app: test
    date: 2025-11-04
data:
  value1: test

2. template调用

在templates/configmap.yaml文件中定义子模板并直接调用

[root@k8s-master m1]# cat chart6/templates/configmap.yaml
{{- define "childTemplates" }}
  labels:
    app: test
    date: {{ now | date "2006-01-02" }}
{{- end }}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}
  {{- template "childTemplates" . }}
data:
  value1: test

试运行查看调用结果

# Source: chart6/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: chart6
  labels:
    name: test
    date: 2025-11-04
data:
  value1: test

更多推荐