Helm

Use

1
2
3
4
5
6
7
8
# values
hello: world

# template
{{ .Values.hello | title }}

# output
hello: World  # 开头首字母大写
1
2
3
4
5
6
7
8
# values
hello: world

# template
{{ .Values.hello | upper }}

# output
hello: WORLD  # 全部大写
1
2
3
4
5
6
7
8
# values
hello: world

# template
{{ .Values.hello | quote }}

# output
hello: "world"  # 附加双引号
1
2
3
4
5
6
7
8
# values
hello: world

# template
{{ .Values.hello | repeat 2 }}

# output
hello: world world  # 值乘以2
1
2
3
4
5
6
7
8
# values
hello: world

# template
{{ .Values.hello | quote | upper }}

# output
hello: "WORLD" #可以组合使用 

if else

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# values
workload:
  statefulset: true

# template
apiVersion: apps/v1
{{- if .Values.workload.statefulset }}
kind: StatefulSet
{{- else }}
kind: Deployment
{{- end }}

# output
if == true
apiVersion: apps/v1
kind: StatefulSet

if == false
apiVersion: apps/v1
kind: Deployment

在values.yaml中渲染配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# values
fluentconf: |-
  <source>
    @type tail
    path /var/log/nginx/access.log
    pos_file /var/log/nginx/access.log.pos
    path_key log_path
    tag access.log
    <parse>
      @type nginx
    </parse>
  </source>  

# template
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "server.fullname" . }}-fluentd-config
  labels:
    {{- include "server.labels" . | nindent 4 }}
data:
  fluent.conf: |
{{ .Values.fluentconf | indent 4 }}

# output
apiVersion: v1
kind: ConfigMap
metadata:
  name: hello-server-fluentd-config
[...]
data:
  fluent.conf: |
    <source>
      @type tail
      path /var/log/nginx/access.log
      pos_file /var/log/nginx/access.log.pos
      path_key log_path
      tag access.log
      <parse>  
        @type nginx
      </parse>
    </source>    

with

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# values
nodeSelector:
  hello: world

# template
    {{- with .Values.nodeSelector }}
    nodeSelector:
      {{- toYaml . | nindent 8 }}
    {{- end }}

# output 效果 渲染nodeSelector下的key:values  范围性  这个缩进是以yaml的最左侧开始
nodeSelector:
  hello: world

渲染外部配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# external configuration file conf/app.conf
firstName={{ .Values.firstName }}
lastName={{ .Values.lastName }}

# values
firstName: Peter
lastName: Parker

# template
{{ tpl (.Files.Get "conf/app.conf") . }}

# output
firstName=Peter
lastName=Parker
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# template
data:
{{ (.Files.Glob "conf/*").AsConfig | indent 2 }}
# 渲染conf下所有的文件
https://helm.sh/docs/chart_template_guide/accessing_files/#glob-patterns

# 渲染conf目录所有文件并使用values中定义的值
data:
{{ tpl (.Files.Glob "conf/*").AsConfig .  | indent 2 }}

# 渲染conf中app.conf文件
{{- .Files.Get "conf/app.conf" | nindent 4 }}

# 渲染conf中app.conf文件,可以在文件中引用values.yaml中定义的值
{{- tpl (.Files.Get "conf/app.conf") . | nindent 4}}

# 渲染conf中所有配置文件 
{{ (.Files.Glob "conf/*").AsConfig | indent 2 }}

# 渲染conf中所有配置文件,可以在文件中引用values.yaml中定义的值
{{ tpl (.Files.Glob "conf/*").AsConfig .  | indent 2 }}

在configmap&secret更新时,使pod重启

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# configmap.yaml内容发生变化,deployment中的sha256sum就会改变,重启pod

kind: Deployment
spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
[...]

# 无论修改与不修改都会重启

kind: Deployment
spec:
  template:
    metadata:
      annotations:
        rollme: {{ randAlphaNum 5 | quote }}
[...]

values.yaml 使用变量

1
2
3
4
5
6
7
8
9
# values
template: "{{ .Values.name }}"
name: "Tom"

# template
{{ tpl .Values.template . }}

# output
Tom

Range

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# values Range
env:
  HELLO: world
  HELM: charts

# template
          {{- range $key, $val := .Values.env }}
          - name: {{ $key }}
            value: {{ $val | quote }}
          {{- end }}

# output
          env:
          - name: HELLO
            value: "world"
          - name: HELM
            value: "charts"
# https://stackoverflow.com/questions/58624857/multiple-env-variables-in-helm-charts

更多语法