k8s1.14-helm模板变量

变量

赋值变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@master101 sy]# cat mychart/templates/configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-configmap
data:
{{- $releaseName := .Release.Name -}}
{{- with .Values.course }}
release: {{ $releaseName }}
k8s: {{ .k8s | upper | quote }}
python: {{ .python | repeat 5 | quote }}
{{- if eq .python "django" }}
web: true
{{- end }}
{{- end }}
courselist:
{{- range $index, $course := .Values.courselist }}
- {{ $index }}: {{ $course | title | quote }}
{{- end }}
1
2
3
with语句上面增加了一句{{- $releaseName := .Release.Name -}},其中$releaseName就是后面的对象的一个引用变量,它的形式就是$name,赋值操作使用:=,这样with语句块内部的$releaseName变量仍然指向的是.Release.Name

变量在range循环使用,我们可以在循环中用变量来同时捕获索引的值

结果:

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: '42101'

[debug] SERVER: "127.0.0.1:42101"

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

NAME: needled-labradoodle
REVISION: 1
RELEASED: Fri Aug 23 14:08:08 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: needled-labradoodle-configmap
data:
release: needled-labradoodle
k8s: "DEVOPS"
python: "djangodjangodjangodjangodjango"
web: true
courselist:
- 0: "K8s"
- 1: "Python"
- 2: "Go"
- 3: "Java"
- 4: "Php"
Donate