What is the difference between helm syntax {{ something }} and {{- something }}?
Asked Answered
J

1

39

I'm trying to understand helm templates and found syntax like this:

  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}

So I thought every thing had to start with {{- but then I found other syntax that did not have that syntax:

        - name: {{ .Chart.Name }}

So my question is what is the difference between those two syntaxs? What does the dash do? When is it needed?

Juvenal answered 28/4, 2021 at 4:20 Comment(0)
J
58

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

Causes the property apiVersion to be output (in the YAML file) without adding blank lines before and after the property.

Simple example

The Go templating documentation says

when executing the template whose source is

"{{23 -}} < {{- 45}}"

the generated output would be

"23<45"

This shows that the dash syntax causes white space to be removed.

Learning to experiment with Helm syntax

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

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

The first two name/value pairs expression1 and expression2 show the difference with and without dash syntax being used.

Notice also the syntax for aTest5 which resulted in several lines being merged into a single line of output.

Notice also aTest6 and aTest7 look different in the source but produce the same output; spaces within the {{ }} are not output unless they within quotes.

Using this approach digest the Helm syntax in bite sized chunks and so when you need to fix something you can understand what you are seeing.

Juvenal answered 28/4, 2021 at 4:20 Comment(4)
Thanks for the useful information one question here. for aTest5 aTest5: Some {{- "Thing Funky is" -}} goingOn {{- " here"}} I deleted the second dash -}} from "Thing Funky is" but when i run helm temaplate command it generates the same result. What is then difference to keep this dash or not.Flesh
@Juvenal How did you test on helm template? Did you have to create an entire chart repository or did you just render a file?Insole
I updated my answer to explain this in more detail. The short answer is I used helm create (see helm.sh/docs/helm/helm_create) to create a sample program for me to play around with.Juvenal
@AmirDamirov, I'm not sure what you are seeing, it works differently when I run it. A space is added when I remove the second dash . I updated the example to include both with and without a space. I also added more spaces to the quoted string " here" to make it clearer that the spaces that are included within a quoted string are always retained even if you use the dash, so {{- " spaces in front and back " -}} will retain the spaces in the quoted string even though spaces on left and right of that expression will be removed.Juvenal

© 2022 - 2024 — McMap. All rights reserved.