Helm:Chart的命名模板

来自AI助手的总结
介绍Helm中define和include模板的用法与示例
Helm:Chart的命名模板

一、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

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容