14

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?

Community
  • 1
  • 1
MarkNS
  • 3,811
  • 2
  • 43
  • 60

4 Answers4

12

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
Timofey Drozhzhin
  • 4,416
  • 3
  • 28
  • 31
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 }}
MarkNS
  • 3,811
  • 2
  • 43
  • 60
1

I think it should work like this:

$fullDescription: | +
   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
S.Spieker
  • 7,005
  • 8
  • 44
  • 50
  • 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 break` – MarkNS Jul 03 '18 at 12:40
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" -}}
geliba187
  • 357
  • 2
  • 11