{{-(包括添加的横杠和空格)表示向左删除空白-}}表示右边的空格应该被去掉。
一定注意空格就是换行
正确的写法
volumeMounts:
- name: nginx-conf
mountPath: /opt/nginx
subPath: conf
{{- if eq .Values.global.env "dev" }}
- name: nginx-html
mountPath: /opt/html
subPath: dev
{{- end }}
{{- if eq .Values.global.env "prod" }}
- name: nginx-html
mountPath: /opt/html
subPath: prod
{{- end }}
错误的写法
volumeMounts:
- name: nginx-conf
mountPath: /opt/nginx
subPath: conf
{{- if eq .Values.global.env "dev" -}}
- name: nginx-html
mountPath: /opt/html
subPath: dev
{{- end -}}
{{- if eq .Values.global.env "prod" -}}
- name: nginx-html
mountPath: /opt/html
subPath: prod
{{- end -}}
就会遇到这样的错误:
templates/deployment.yaml: error converting YAML to JSON: yaml: line 59: mapping values are not allowed in this context
原因是因为{{- , -}} 会把两边的新行都删除了,变成下面的内容,导致缩进问题。
volumeMounts:
- name: nginx-conf
mountPath: /opt/nginx
subPath: conf-name:nginx-html
mountPath: /opt/html
subPath: dev
因此,
{{- end -}}会在生成的输出中删除end指令之前和之后的空格和换行符,而{{- end }}只会删除end指令之前的空格和换行符。