How to remove the new line added with .toYaml in helm?

10/11/2019

I added the following section to values.yaml of a helm chart:

   extraEnv:
      - name: APPSERVER_RETURN_CLIENT_ERRORS
        value: true

And used those values in deployment.yaml as:

  env:
    - name: DYNA_GATEWAY_HOST
      value: "$(DYNAGATEWAY_SERVICE_HOST)"
  {{- with .Values.extraEnv }}
    {{- toYaml . | nindent 12 }}
  {{- end }} 

But when it reads, it appends a new line after the env section as :

  imagePullPolicy: Always
  env:
    - name: DYNA_GATEWAY_HOST
      value: "$(DYNAGATEWAY_SERVICE_HOST)"
    - name: APPSERVER_RETURN_CLIENT_ERRORS
      value: true

  ports:

How can I resolve this?

-- AnjanaDyna
kubernetes-helm
yaml

1 Answer

10/11/2019

The Sprig trim function will remove leading and trailing whitespace. In terms of the pipeline, you want to do this before nindent puts a leading newline in front of it.

{{- toYaml . | trim | nindent 12 }}

Your other option, if you know toYaml will always include a trailing newline (it will whenever .Values.extraEnv is a non-empty list or dictionary), is to try to suppress the whitespace around it

{{- with .Values.extraEnv }}
  {{- toYaml . | nindent 12 }}
{{/*   vvv add this hyphen */}}
{{- end -}}

but this can interfere with the indentation on the following line.

-- David Maze
Source: StackOverflow