相对于 deployment.yaml 这种主模板,命名模板只是定义部分通用内容,然后在各个主模板中调用。

templates目录下有个_helpers.tpl文件。公共的命名模板都放在这个文件里。

define用法

定义一个只包含字符串的模板,用作资源名称

$ helm create my-template
$ rm -rf templates/*
$ cd my-template
$ cat >  templates/_helpers.tpl <<EOF
{{/* 定义资源名称 */}}
{{ define "mytest.name" -}}
aminglinux
{{- end }}
EOF

使用template引用

$ cd my-template
$ cat > templates/test.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "mytest.name" }}
  labels:
    app: {{ .Values.myname }}
EOF

定义values.yaml

$ cd my-template
$ cat > values.yaml <<EOF
myname: aming
service:
  type: ClusterIP
  port: 80
  myport: 8080
test:
  - 1
  - 2
  - 3
EOF

渲染

$ cd my-template
$ helm template testrelease .

Chart的命名模板-1

include用法

创建了一个名为_helpers.tpl的Helm模板文件,其中定义了两个define块:一个用于定义资源的名称,另一个用于定义标签。

$ helm create my-template
$ rm -rf templates/*
$ cd my-template
$ cat >  templates/_helpers.tpl <<EOF
{{/* 定义资源名称 */}}
{{ define "mytest.name" -}}
aminglinux
{{- end }}

{{/* 定义label */}}
{{- define "mytest.label" -}}
app: {{ .Release.Name }}
release: stable
env: qa
{{- end }}
EOF

在template的yaml文件里调用

$ cd my-template
$ cat > templates/test.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-{{ template "mytest.name" . }}
  labels:
    {{- include "mytest.label" . | nindent 4 }}
EOF

定义values.yaml

$ cd my-template
$ cat > values.yaml <<EOF
myname: aming
service:
  type: ClusterIP
  port: 80
  myport: 8080
test:
  - 1
  - 2
  - 3
EOF

渲染

$ cd my-template
$ helm template testrelease .

Chart的命名模板-2