multiline string to a variable in a helm template?
Asked Answered
C

5

21

Is it possible to assign a multiline string to a variable in a helm template?

I have a variable as follows:

{{- $fullDescription := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -}}

but I would prefer to keep it in my code base as

{{- $fullDescription :|- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -}}

.. but this is not valid yaml.

Can this be done?

Capitulate answered 20/6, 2018 at 14:52 Comment(0)
C
9

I worked around this issue by including the content I require from a separate file.

Eg.

  fullDescription: |-
{{ .Files.Get files/description.html | indent 4 }}
Capitulate answered 3/7, 2018 at 12:42 Comment(1)
how does this even work? I get the same error using this style like from answer below.Rhetor
D
22

Input

values.yaml

myFile: |
  This is a multiline
  value, aka heredoc.

myArray:
  - key1=value1
  - key2=value2

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: myTemplate
data:
  myFile: {{- .Values.myFile | toYaml | indent 1 }}
  myArray: |
  {{- range $k, $v := .Values.myArray }}
    - {{ . | toYaml | indent 4 | trim }}
  {{- end }}

Output

apiVersion: v1
kind: ConfigMap
metadata:
  name: myTemplate
data:
  myFile: |
   This is a multiline
   value, aka heredoc.
  myArray: |
    - key1=value1
    - key2=value2
Differentia answered 17/5, 2022 at 3:35 Comment(0)
C
9

I worked around this issue by including the content I require from a separate file.

Eg.

  fullDescription: |-
{{ .Files.Get files/description.html | indent 4 }}
Capitulate answered 3/7, 2018 at 12:42 Comment(1)
how does this even work? I get the same error using this style like from answer below.Rhetor
T
1

I think it should work like this:

$fullDescription: | +
   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Tiertza answered 27/6, 2018 at 7:35 Comment(1)
Thanks for the answer, but it doesn't work for me. I get Error: UPGRADE FAILED: YAML parse error on x/y/z.yaml: error converting YAML to JSON: yaml: line 15: did not find expected comment or line breakCapitulate
H
1

The solution given by @geliba187 works. It has to mention that declaring the variable $fullDescription is needed.

A solution without an extra files looks completely like this:

values.yaml

fullDescription: |+
line1
line2

in template

{{ $fullDescription := .Values.fullDescription }}
fullDescription: |
{{ $fullDescription | indent 2}}
Haggerty answered 8/2 at 13:2 Comment(0)
R
0

You could've used values.yaml, which would save you from adding an extra file.

fullDescription: |+
  line1
  line2

then in template

fullDescription: |
{{ $fullDescription | indent 2}}

Probably not you wanted, but you could use newline char

{{- $fullDescription := "a\nb\nc\n" -}}
Resound answered 18/1, 2022 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.