Search code examples
templatesyamlkubernetes-helmgo-templates

helm templating add empty line when using nindent


My deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: foo
  labels:
    app: foo
spec:
  selector:
    matchLabels:
      app: foo
  template:
    metadata:
      labels:
        app: foo
    spec:
      {{- include "pod" . | nindent 6 }}

My _pod.tpl:

{{- define "pod" -}}
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
  {{- toYaml . | nindent 2 }}
{{- end }}
serviceAccountName: foo
containers:
  - name: foo
    image: nginx:latest
{{- end }}

Give me:

# Source: foo/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: foo
  labels:
    app: foo
spec:
  selector:
    matchLabels:
      app: foo
  template:
    metadata:
      labels:
        app: foo
    spec:
      
      imagePullSecrets:
        - name: bar
      serviceAccountName: foo
      containers:
        - name: foo
          image: nginx:latest

Why is there an empty line after spec:?

Edited

However, there is no empty line when using {{- include "pod" . | indent 6 }} in the Deployment, but there is 6 whitespaces after spec:, like that: spec: .


Solution

  • The nindent function is the same as the indent function, but prepends a new line to the beginning of the string.

    refer : https://helm.sh/docs/chart_template_guide/function_list/#nindent

    You should use indent instead of nindent, to avoid new line after spec: