Minh
Minh

Reputation: 2310

Helm: Extra newline when using "include" for templating

I get a weird new line when my chart is rendered that templates from another file using {{ include }}. For instance, my manifest looks like this

      containers:
      - name: {{ .Release.Name }}
        image: {{ .Values.global.image}}:{{ .Values.global.imageTag }}
        imagePullPolicy: {{ .Values.global.pullPolicy }}
        ports:
          - containerPort: {{ .Values.gloabl.containerPort }}
{{ include "common_deployment" . }}

and my common_deployment is defined as

{{- define "common_deployment" }}
        envFrom:
          - secretRef:
              name: {{ .Release.Name }}-secret
{{- end -}}

when I look at my manifest after doing a dry run on Helm, my template looks something like this

 containers:
  - name: test
    image: myrepo/myimage:latest
    imagePullPolicy: Always
    ports:
      - containerPort: 4444

envFrom:
  - secretRef:
      name: test-secret

Notice how there is a new lie between the ports and the envFrom. I'm wondering if this will affect how my pods will turn out because there are issues with volumes being mounted and I want to be able to make sure that this templating problem is the culprit before going down another rabbit hole.

Upvotes: 14

Views: 26709

Answers (2)

weibeld
weibeld

Reputation: 15292

There's one thing to consider when you use the include function with the indent function, to indent the template, like so:

{{- include "common_deployment" . | indent 4 }}

The above command also indents the leading newline introduced by include by 4 spaces, so the resulting output will be as follows (spaces indicated as $):

    ports:
      - containerPort: 4444$$$$\n
$$$$envFrom:
$$$$  - secretRef:
$$$$      name: test-secret

This doesn't break the YAML syntax, as whitespaces are ignored anyway, but it may be reported, e.g. in diff outputs.

To prevent this, you can trim the leading newline generated by include with the trim function, and use the nindent function, like so:

{{- include "common_deployment" . | trim | nindent 4 }}

Now, only the actual lines of the template are indented, and the newline is inserted by the nindent function:

    ports:
      - containerPort: 4444\n
$$$$envFrom:
$$$$  - secretRef:
$$$$      name: test-secret

Upvotes: 15

Marc
Marc

Reputation: 21095

You can use a hyphen to suppress the newline on template commands. You're already using it for define and end.

Similarly, you should use {{- include ... -}}.

Upvotes: 21

Related Questions