以下操作,均原取自官网,由个人进行实操。
官网地址:Helm | 流控制模板中流结构的快速概述https://helm.sh/zh/docs/chart_template_guide/control_structures/
操作前准备:
1. 有一台云服务器(已配置成为master结点),确保集群所有pod正常运行。
2. 已安装下载HELM。
3. 已创建了一个chart, 并且删除templates文件夹下的所有。
4. 已在values.yaml文件中插入:
- favorite:
- drink: coffee
- food: pizza
5. 在templates下新建configmap.yaml文件,下面的操作均只针对该文件。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{ if eq .Values.favorite.drink "coffee" }}mug: "true"{{ end }}
操作:
(下面都使用类似的命令,仅改变chart名:chart+顺序)
- [root@k8s-master testchart]# vim mychart/templates/configmap.yaml
- [root@k8s-master testchart]# helm install chart1 ./mychart --dry-run --debug
- ...
- ...
- # Source: mychart/templates/configmap.yaml
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: chart1-configmap
- data:
- myvalue: "Hello World"
- drink: "coffee"
- food: "PIZZA"
- mug: "true"
结果:
与第一个的不同在于换了行,而且在mug前打多了两位缩进。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{ if eq .Values.favorite.drink "coffee" }}
- mug: "true"
- {{ end }}
得到错误:
Error: INSTALLATION FAILED: YAML parse error on mychart/templates/configmap.yaml: error converting YAML to JSON: yaml: line 9: did not find expected key
原因即多打出了空格,而报错,导致yaml格式错误,变成如下效果,运行命令时是没有显示出来的:
- # Source: mychart/templates/configmap.yaml
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: chart2-configmap
- data:
- myvalue: "Hello World"
- drink: "coffee"
- food: "PIZZA"
- mug: "true"
与第二个的不同在于在mug前打缩进,是正常的,与food平行的。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{ if eq .Values.favorite.drink "coffee" }}
- mug: "true"
- {{ end }}
因为YAML文件在helm模板引擎运行时会把{{}}移除后留下空白,在单独一行只有{{}}的情况下,就会形成一行空白。
运行结果:
与第三个的不同在于在if控制语句中,左{{后加了“-”一个横杠,它会去掉空白,相当于回退一格。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{- if eq .Values.favorite.drink "coffee" }}
- mug: "true"
- {{- end }}
这里还可以使用indent控制缩进,比直接在前面打空格控制缩进要清晰一些。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{- if eq .Values.favorite.drink "coffee" }}
- {{ indent 2 "mug: true" }}
- {{- end }}
运行结果:
与第四个的不同在于在if控制语句中,右{{也后加了“-”一个横杠,那么导致右边也回退了一位。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- drink: {{ .Values.favorite.drink | default "tea" | quote }}
- food: {{ .Values.favorite.food | upper | quote }}
- {{- if eq .Values.favorite.drink "coffee" -}}
- mug: "true"
- {{- end -}}
运行就会报错,Error: INSTALLATION FAILED: YAML parse error on mychart/templates/configmap.yaml: error converting YAML to JSON: yaml: line 7: did not find expected key
因为会形成food: "PIZZA"mug:"true"违背yaml的语法:
- # Source: mychart/templates/configmap.yaml
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: chart5-configmap
- data:
- myvalue: "Hello World"
- drink: "coffee"
- food: "PIZZA"mug: "true"
与第五个configmap相比,去掉了if条件语句,加是了with语句,并在drink和food部分的.Values.favorite提取出来。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- {{- end }}
运行结果:
与第六个configmap的区别是加了release。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- release: {{ .Release.Name }}
- {{- end }}
运行结果:
报错,Error: INSTALLATION FAILED: template: mychart/templates/configmap.yaml:10:22: executing "mychart/templates/configmap.yaml" at <.Release.Name>: nil pointer evaluating interface {}.Name。
与第七个configmap的区别是把release部分移到with语句之外。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- {{- end }}
- release: {{ .Release.Name }}
运行结果:
与第八个configmap的区别是release前,加“$”作为访问前缀。
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- release: {{ $.Release.Name }}
- {{- end }}
$会映射到根作用域,相当于原始的“.”,运行结果:
这里先需改动一下values.yaml,以至于能看到循环效果:
加上属性
- pizzaToppings:
- - mushrooms
- - cheese
- - peppers
- - onions
(注:|-
标识在YAML中是指多行字符串)
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- {{- end }}
- toppings: |-
- {{- range .Values.pizzaToppings }}
- - {{ . | title | quote }}
- {{- end }}
运行结果:
与第十个相比,把toppings属性放入with语句范围内,运用“$”引用Values对象
- apiVersion: v1
- kind: ConfigMap
- metadata:
- name: {{ .Release.Name }}-configmap
- data:
- myvalue: "Hello World"
- {{- with .Values.favorite }}
- drink: {{ .drink | default "tea" | quote }}
- food: {{ .food | upper | quote }}
- toppings: |-
- {{- range $.Values.pizzaToppings }}
- - {{ . | title | quote }}
- {{- end }}
- {{- end }}
运行结果: