k8s1.14-helm模板控制流程

控制流程

模板函数和管道是通过转换信息并将其插入到YAML文件中的强大方法。但有时候需要添加一些比插入字符串更复杂一些的模板逻辑。这就需要使用到模板语言中提供的控制结构了。

控制流程为我们提供了控制模板生成流程的一种能力,Helm 的模板语言提供了以下几种流程控制:

  • if/else 条件块
  • with 指定范围
  • range 循环块

除此之外,它还提供了一些声明和使用命名模板段的操作:

  • define在模板中声明一个新的命名模板
  • template导入一个命名模板
  • block声明了一种特殊的可填写的模板区域

关于命名模板的相关知识点,我们会在后面的课程中和大家接触到,这里我们暂时和大家介绍if/elsewithrange这3中控制流程的用法。

if/else 条件

if/else块是用于在模板中有条件地包含文本块的方法,条件块的基本结构如下:

1
2
3
4
5
6
7
{{ if PIPELINE }}
# Do something
{{ else if OTHER PIPELINE }}
# Do something else
{{ else }}
# Default case
{{ end }}

当然要使用条件块就得判断条件是否为真,如果值为下面的几种情况,则管道的结果为 false:

  • 一个布尔类型的
  • 一个数字
  • 一个的字符串
  • 一个nil(空或null
  • 一个空的集合(mapslicetupledictarray

除了上面的这些情况外,其他所有条件都为

例子

1
2
3
4
5
6
7
8
9
10
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
k8s: {{ .Values.course.k8s | upper | quote }}
python: {{ .Values.course.python | repeat 5 | quote }}
{{ if eq .Values.course.python "django" }}web: true{{ end }}

在底部加了if条件,如果等于django,就加一个web:true。

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
43
44
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
k8s: {{ .Values.course.k8s | upper | quote }}
python: {{ .Values.course.python | repeat 5 | quote }}
{{ if eq .Values.course.python "django" }}web: true{{ end }}
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '41755'

[debug] SERVER: "127.0.0.1:41755"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: quaffing-mastiff
REVISION: 1
RELEASED: Fri June 20 10:41:54 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: quaffing-mastiff-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"
web: true

现在把values.yaml值替换下:

1
2
3
4
[root@master101 sy]# cat mychart/values.yaml
course:
k8s: devops
python: aa
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
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '41944'

[debug] SERVER: "127.0.0.1:41944"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: zeroed-uakari
REVISION: 1
RELEASED: Fri June 20 10:47:55 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: aa

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: zeroed-uakari-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "aaaaaaaaaa"

发现,if语句判断出来了

空格

例子1

先改回django

1
2
3
4
[root@master101 sy]# cat mychart/values.yaml
course:
k8s: devops
python: django

加空格,换行

1
2
3
4
5
6
7
8
9
10
11
12
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
k8s: {{ .Values.course.k8s | upper | quote }}
python: {{ .Values.course.python | repeat 5 | quote }}
{{ if eq .Values.course.python "django" }}
web: true
{{ end }}

debug看一下:

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
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '46726'

[debug] SERVER: "127.0.0.1:46726"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: bumptious-gorilla
REVISION: 1
RELEASED: Fri June 20 10:54:52 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: bumptious-gorilla-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"

web: true

注意:会发现有空格出来

例子2

1
可以通过使用在模板标识{{后面添加破折号和空格{{-来表示将空白左移,而在}}前面添加一个空格和破折号-}}表示应该删除右边的空格,另外需要注意的是换行符也是空格!
1
2
3
4
5
6
7
8
9
10
11
12
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
k8s: {{ .Values.course.k8s | upper | quote }}
python: {{ .Values.course.python | repeat 5 | quote }}
{{- if eq .Values.course.python "django" }}
web: true
{{- end }}
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
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '41705'

[debug] SERVER: "127.0.0.1:41705"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: goodly-quail
REVISION: 1
RELEASED: Fri June 20 10:57:39 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: goodly-quail-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"
web: true

with

with语句就可以来控制变量的作用域范围

1
2
3
{{ with PIPELINE }}
# restricted scope
{{ end }}

例子

###

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
{{- with .Values.course }}
k8s: {{ .k8s | upper | quote }}
python: {{ .python | repeat 5 | quote }}
{{- if eq .python "django" }}
web: true
{{- end }}
{{- end }}
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
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '40660'

[debug] SERVER: "127.0.0.1:40660"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: loitering-gibbon
REVISION: 1
RELEASED: Fri June 20 11:03:56 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: loitering-gibbon-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"
web: true

有with的.Values.course ,之后的就不用加了

range循环

例子

1
2
3
4
5
6
7
8
9
10
[root@master101 sy]# cat mychart/values.yaml
course:
k8s: devops
python: django
courselist:
- k8s
- python
- go
- java
- php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
myvalue: {{ .Values.hello | default "Hello World" | quote }}
{{- with .Values.course }}
k8s: {{ .k8s | upper | quote }}
python: {{ .python | repeat 5 | quote }}
{{- if eq .python "django" }}
web: true
{{- end }}
{{- end }}
courselist:
{{- range .Values.courselist }}
- {{ . | title | quote }}
{{- end }}
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
43
44
45
46
[root@master101 sy]# helm install --dry-run --debug mychart/
[debug] Created tunnel using local port: '40352'

[debug] SERVER: "127.0.0.1:40352"

[debug] Original chart version: ""
[debug] CHART PATH: /root/sy/mychart

NAME: idle-hummingbird
REVISION: 1
RELEASED: Fri June 20 11:11:38 2019
CHART: mychart-0.1.0
USER-SUPPLIED VALUES:
{}

COMPUTED VALUES:
course:
k8s: devops
python: django
courselist:
- k8s
- python
- go
- java
- php

HOOKS:
MANIFEST:

---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: idle-hummingbird-configmap
data:
myvalue: "Hello World"
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"
web: true
courselist:
- "K8s"
- "Python"
- "Go"
- "Java"
- "Php"

我们可以看到courselist按照我们的要求循环出来了。除了 list 或者 tuple,range 还可以用于遍历具有键和值的集合(如map 或 dict)。

Donate