Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 409
Minh Mai Avatar asked Dec 04 '17 21:12

Minh Mai


People also ask

What does include do in Helm chart?

The include function allows you to bring in another template, and then pass the results to other template functions. For example, this template snippet includes a template called mytpl , then lowercases the result, then wraps that in double quotes.

What does {{- mean in Helm?

{{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed.

What is Nindent?

nindent. The nindent function is the same as the indent function, but prepends a new line to the beginning of the string. nindent 4 $lots_of_text. The above will indent every line of text by 4 space characters and add a new line to the beginning.

What is _helpers TPL in Helm?

tpl? Helm allows for the use of Go templating in resource files for Kubernetes. A file named _helpers.tpl is usually used to define Go template helpers with this syntax: {{- define "yourFnName" -}} {{- printf "%s-%s" .Values.name .Values.version | trunc 63 -}} {{- end -}}


Video Answer


2 Answers

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

like image 107
Marc Avatar answered Nov 15 '22 15:11

Marc


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
like image 45
weibeld Avatar answered Nov 15 '22 17:11

weibeld