Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 876
CrusaderX Avatar asked Feb 21 '18 13:02

CrusaderX


People also ask

What does {{ }} mean in Helm?

The Helm template syntax is based on the Go programming language's text/template package. The braces {{ and }} are the opening and closing brackets to enter and exit template logic.

What does toYaml do?

The above includes a template called toYaml , passes it $value , and then passes the output of that template to the indent function. Because YAML ascribes significance to indentation levels and whitespace, this is one great way to include snippets of code, but handle indentation in a relevant context.

What is $_ in Helm?

The $_ is used to suppress undesired output as "set" returns the new dictionary. The above returns: - name: mongod-none. Any values added to the dictionary will live beyond the call. If you want to avoid polluting an existing dictionary you can force a deep copy with: {{- $d := merge (dict) . -}}

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

This worked for me:

{{ toYaml .Values.pod.resources | trim | indent 6 }}
like image 123
Eric Nelson Avatar answered Oct 12 '22 11:10

Eric Nelson


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: {}
like image 44
nickgryg Avatar answered Oct 12 '22 10:10

nickgryg