1. 控制空格

在查看条件时,我们应该快速查看模板中的空格控制方式。让我们看一下前面的例子,并将其格式化为更容易阅读的格式:

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 }}

最初,这看起来不错。但是如果我们通过模板引擎运行它,我们会得到一个错误的结果:

$ helm install --dry-run --debug ./mychart
SERVER: "localhost:44134"
CHART PATH: /Users/mattbutcher/Code/Go/src/k8s.io/helm/_scratch/mychart
Error: 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: eyewitness-elk-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 }}

当我们发送该信息时,我们会得到有效的 YAML,但仍然看起来有点意思:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: telling-chimp-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"

  mug: true

请注意,我们在 YAML 中收到了一些空行。为什么?当模板引擎运行时,它将删除 {{}} 中的空白内容,但是按原样保留剩余的空白。

YAML 中的缩进空格是严格的,因此管理空格变得非常重要。幸运的是,Helm 模板有几个工具可以帮助我们。

首先,可以使用特殊字符修改模板声明的大括号语法,以告诉模板引擎填充空白。{{-(添加了破折号和空格)表示应该将格左移,而 -}} 意味着应该删除右空格。

[!NOTE|style:flat] 注意!换行符也是空格!

确保 - 和其他指令之间有空格。{{- 3}} 意思是 “删除左空格并打印 3”,而 {{-3}} 意思是 “打印 -3”。

使用这个语法,我们可以修改我们的模板来摆脱这些新行:

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 }}

为了清楚说明这一点,让我们调整上面的内容,将空格替换为 *, 按照此规则将每个空格将被删除。一个在该行的末尾的 * 指示换行符将被移除

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 }}

牢记这一点,我们可以通过 Helm 运行我们的模板并查看结果:

# Source: mychart/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: clunky-cat-configmap
data:
  myvalue: "Hello World"
  drink: "coffee"
  food: "PIZZA"
  mug: true

小心使用 chomping 修饰符。这样很容易引起意外:

  food: {{ .Values.favorite.food | upper | quote }}
  {{- if eq .Values.favorite.drink "coffee" -}}
  mug: true
  {{- end -}}

这将会产生 food: "PIZZA"mug:true,因为删除了双方的换行符。

有关模板中空格控制的详细信息,请参阅官方 Go 模板文档 Official Go template documentation

最后,有时候告诉模板系统如何缩进更容易,而不是试图掌握模板指令的间距。因此,有时可能会发现使用 indent 函数({{ indent 2 "mug:true" }})会很有用。

Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2022-01-08 03:09:47

results matching ""

    No results matching ""