我试图理解 helm 模板并发现如下语法:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)
所以我认为每件事都必须从以下开始{{-,但后来我发现其他语法没有该语法:
- name: {{ .Chart.Name }}
Run Code Online (Sandbox Code Playgroud)
So my question is what is the difference between those two syntaxs? What does the dash do? When is it needed?
Pat*_*atS 52
The Helm template syntax is based on the Go programming language's text/template package.
The braces {{ and }} are the opening and closing brackets to enter and exit template logic.
The Helm documentation at https://helm.sh/docs/chart_template_guide/control_structures/ discusses why this syntax is needed in an example.
YAML ascribes meaning to whitespace, so managing the whitespace becomes pretty important. [...] the curly brace syntax
{{of template declarations can be modified with special characters to tell the template engine to chomp whitespace.{{-(with the dash and space added) indicates that whitespace should be chomped left, while-}}means whitespace to the right should be consumed. Be careful! Newlines are whitespace!
So the answer is this. The difference between the {{ syntax and the {{- syntax is that the {{- something }} will result in space on the left being removed. Without this any extra space would be included which could result in incorrectly formatted YAML.
Refer to the Helm documentation which goes into great length about how this syntax works and removes extra spaces.
You'll frequently see the dash showing up in control structures because without this extra space would be added to your YAML file which could result in invalid syntax being created. So, for example,
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
Run Code Online (Sandbox Code Playgroud)
Causes the property apiVersion to be output (in the YAML file) without adding blank lines before and after the property.
The Go templating documentation says
when executing the template whose source is
"{{23 -}} < {{- 45}}"
Run Code Online (Sandbox Code Playgroud)
the generated output would be
"23<45"
Run Code Online (Sandbox Code Playgroud)
This shows that the dash syntax causes white space to be removed.
Below I'll explain how you can begin experimenting with helm syntax using a simple throw away project.
The commands below I create a temp directory testhelm and go into it, and then run create helm mytest to create a helm application.
Next, I create a sample helm YAML file. This is the file you want to put what you want to test inside of. Below I used the file mytest/templates/my.yaml but any file can be created.
Helm apparently takes all of the files in the templates directory and parses/processes them to create the YAML output (which is used to create a Kubernetes YAML file to configure a K8S application).
In our case, we just leverage the helm command create a test bed for us to play around with.
If you are on a UNIX-based system you should be able to copy and paste the entire code sample below to create the testbed to begin experimenting.
mkdir testhelm
cd testhelm
helm create mytest
cat <<EOF > mytest/templates/my.yaml
expression1: "{{ 23 }} < {{ 45 }}"
expression2: "{{ 23 -}} < {{- 45 }}"
aTest0: ArgWithNoSpace
aTest1: Arg with spaces on left and right
aTest2: " spaces-on-left-and-right "
aTest3: {{ " spaces-on-left-and-right " }}
aTest4: {{ " spaces-on-left-and-right " | trim | quote }}
aTest5: Some
{{- "Thing Funky is" -}} goingOn
{{- " here"}}
drink2: {{ .Values.drink2 | default "coffee" | quote }}
aTest6: Some {{ "Thing Funky is" }}goingOn {{ " here"}}
aTest7: Some {{ "Thing Funky is" }}goingOn {{ " here"}}
EOF
Run Code Online (Sandbox Code Playgroud)
Then run run the helm template command as shown below, and study the output that you get.
helm template myproj ./mychart | less
. . . output trimmed . . .
# Source: mychart/templates/my.yaml
expression1: "23 < 45"
expression2: "23<45"
aTest0: ArgWithNoSpace
aTest1: Arg with spaces on left and right
aTest2: " spaces-on-left-and-right "
aTest3: spaces-on-left-and-right
aTest4: "spaces-on-left-and-right"
aTest5: SomeThing Funky isgoingOn here
drink2: "coffee"
aTest6: Some Thing Funky isgoingOn here
aTest7: Some Thing Funky isgoingOn here
Run Code Online (Sandbox Code Playgroud)
前两个名称/值对expression1和expression2显示使用和不使用破折号语法的差异。
aTest5另请注意导致多行合并为单行输出的语法。
还要注意aTest6,aTest7在源中看起来不同,但产生相同的输出;中的空格{{ }}不会输出,除非它们在引号内。
使用这种方法将 Helm 语法消化为小块,因此当您需要修复某些内容时,您可以理解您所看到的内容。
| 归档时间: |
|
| 查看次数: |
19847 次 |
| 最近记录: |