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.
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.
{{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed.
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.
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 -}}
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 ... -}}
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With