CrusaderX
CrusaderX

Reputation: 141

helm: how to remove newline after toYaml function

From official documentation: When the template engine runs, it removes the contents inside of {{ and }}, but it leaves the remaining whitespace exactly as is. The curly brace syntax of template declarations can be modified with special characters to tell the template engine to chomp whitespace. {{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed.

But I try all variations with no success. Have anyone solution how to place yaml inside yaml? I don't want to use range

apiVersion: v1
kind: Pod
metadata:
  name: app
  labels:
    app: app
spec:
  containers:
  - name: app
    image: image
    volumeMounts:
      - mountPath: test
        name: test
    resources:
{{ toYaml .Values.pod.resources | indent 6 }}
  volumes:
  - name: test
    emptyDir: {}

when I use this code without -}} it's adding a newline:

    resources:
      limits:
        cpu: 100m
        memory: 128Mi
      requests:
        cpu: 20m
        memory: 64Mi

  volumes:
  - name: test
    emptyDir: {}

but when I use -}} it's concate with another position.

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 20m
    memory: 64Mi
  volumes: <- shoud be in indent 2
- name: test
  emptyDir: {}

values.yaml is

pod:
  resources:
    requests:
      cpu: 20m
      memory: 64Mi
    limits:
      cpu: 100m
      memory: 128Mi

Upvotes: 8

Views: 28129

Answers (4)

Arsen
Arsen

Reputation: 579

{{- toYaml .Values.pod.resources | indent 6 -}}

This removes a new line

Upvotes: 1

Eric Nelson
Eric Nelson

Reputation: 346

This worked for me:

{{ toYaml .Values.pod.resources | trim | indent 6 }}

Upvotes: 16

user2077221
user2077221

Reputation: 934

@Nickolay, it is not a valid yaml file, according to helm - at least helm barfs and says:

error converting YAML to JSON: yaml: line 51: did not find expected key

For me, line 51 is the empty space - and whatever follows should not be indented to the same level

Upvotes: -1

nickgryg
nickgryg

Reputation: 28713

The below variant is correct:

{{ toYaml .Values.pod.resources | indent 6 }} 

Adding a newline doesn't create any issue here.

I've tried your pod.yaml and got the following error:

$ helm install .
Error: release pilfering-pronghorn failed: Pod "app" is invalid: spec.containers[0].volumeMounts[0].mountPath: Invalid value: "test": must be an absolute path

which means that mountPath of volumeMounts should be something like /mnt.

So, the following pod.yaml works pretty good and creates a pod with the exact resources we defined in values.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: app
  labels:
    app: app
spec:
  containers:
  - name: app
    image: image
    volumeMounts:
      - mountPath: /mnt
        name: test
    resources:
{{ toYaml .Values.pod.resources | indent 6 }}
  volumes:
  - name: test
    emptyDir: {}

Upvotes: 3

Related Questions