Jibin
Jibin

Reputation: 111

Helm : How do we decide indent value in helm templates?

I have seen {{ toYaml .Values.deployment.updateStrategy | indent 4 }} - this in one of our helm deployment yamls. I was wondering how do one decide the indent was 4?

In another line of same yaml , {{ toYaml .Values.imagePullSecrets | indent 8 }} , the indent was set to 8.

How does one figure the indent value in yaml files?

Upvotes: 11

Views: 50503

Answers (1)

PatS
PatS

Reputation: 11524

The indent of 4 or 8 is dependent on the current indenting going on with the YAML file. If you need the output to be 4 spaces then you use 4, if you need the output to be 8 space (to align with the surrounding YAML, then you use 8).

An Example

For example, create a chart:

mkdir testing-helm
cd testing-helm
helm create anvil
head -7 anvil/templates/service.yaml

You'll see:

apiVersion: v1
kind: Service
metadata:
  name: {{ include "anvil.fullname" . }}
  labels:
    {{- include "anvil.labels" . | nindent 4 }}
spec:

So in this example, the output is being indented 4 spaces. When the output is generated via helm template anvil you'll see this:

# Source: anvil/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: RELEASE-NAME-anvil
  labels:
    helm.sh/chart: anvil-0.1.0               # <<<--- From Template
    app.kubernetes.io/name: anvil            # <<<--- From Template
    app.kubernetes.io/instance: RELEASE-NAME # <<<--- From Template
    app.kubernetes.io/version: "1.16.0"      # <<<--- From Template
    app.kubernetes.io/managed-by: Helm       # <<<--- From Template
spec:

Notice that the output from the template has 4 spaces before each line, so the output is clearly part of the labels: property.

Now change output from 4 spaces to 8 spaces

If we changed the indent to 8 the syntax would look like this:

# Source: anvil/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: RELEASE-NAME-anvil
  labels:
        helm.sh/chart: anvil-0.1.0               # <<<--- From Template
        app.kubernetes.io/name: anvil            # <<<--- From Template
        app.kubernetes.io/instance: RELEASE-NAME # <<<--- From Template
        app.kubernetes.io/version: "1.16.0"      # <<<--- From Template
        app.kubernetes.io/managed-by: Helm       # <<<--- From Template
spec:

Notice that the output from the template now has 8 spaces before each line.

So the number is picked to format the YAML appropriately. Depending on the surrounding YAML the resulting output could produce malformed YAML syntax which would result in an error when trying to parse it.

You can use the online YAML validator at http://www.yamllint.com/ if you want to see if your changes are valid.

Now change output from 4 spaces to 1 space (invalid YAML syntax)

When changing the output to 1 space, the resulting YAML is not valid.

Running helm template gives an error:

helm template anvil
Error: YAML parse error on anvil/templates/service.yaml: error converting YAML to JSON: yaml: line 5:
 did not find expected key

Use --debug flag to render out invalid YAML

When I add the --debug flag I can see the following output which is no longer valid YAML syntax.

helm template anvil --debug
. . . trimmed output . . .
# Source: anvil/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: RELEASE-NAME-anvil
  labels:
 helm.sh/chart: anvil-0.1.0                # <<<--- From Template
 app.kubernetes.io/name: anvil             # <<<--- From Template
 app.kubernetes.io/instance: RELEASE-NAME  # <<<--- From Template
 app.kubernetes.io/version: "1.16.0"       # <<<--- From Template
 app.kubernetes.io/managed-by: Helm        # <<<--- From Template
    another: value                         # I added 
spec:

I added another: value that was indented properly to make it easier to see that the indentation from the template was not valid.

The following is how the anvil/templates/service.yaml file looked for the above test:

head -8 anvil/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ include "anvil.fullname" . }}
  labels:
    {{- include "anvil.labels" . | nindent 1 }}
    another: value
spec:

So even though the source looks like it is indented properly, the resulting output that was produced was not formatted properly. See also What is the difference between helm syntax {{ something }} and {{- something }}? to better understand why the indenting looks fine in the source but produces output that was not valid.

Upvotes: 12

Related Questions