RTC EG
RTC EG

Reputation: 15

helm multiline file inside range

Im trying to create one secret with multiple file in it.

My value.yaml ( the format of the multiline is not yaml or json)

secretFiles:
  - name: helm-template-file
    subPath: ".file1"
    mode: "0644"
    value: |
      This is a multiline
      value, aka heredoc.

Then my secret file template is secret.yaml:

apiVersion: v1
kind: Secret
metadata:
  name: {{ include "helm-template.fullname" . }}-file
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ include "helm-template.name" . }}
    chart: {{ include "helm-template.chart" . }}
type: Opaque
stringData: 
  {{- range .Values.secretFiles }}

  {{ .subPath}}: |
    {{  .value  | indent 4}}
  {{- end }}

The helm install gives error "error converting YAML to JSON: yaml: line 12: did not find expected comment or line break". How can I fix it? Thank you.

Upvotes: 0

Views: 525

Answers (2)

David Maze
David Maze

Reputation: 159475

When you indent a line, you need to make sure the markup in the line starts at the very first column.

stringData: 
  {{- range .Values.secretFiles }}
  {{ .subPath}}: |
{{  .value  | indent 4}}{{/* no space at start of this line */}}
  {{- end }}

In the form you have in the original question, the four spaces at the start of the line are copied to the output, then the indent expression adds four more spaces to the start of every line. This results in the first line being indented by eight spaces, and the second line by 4, and that inconsistency results in the YAML parse error you see.

You'd also be able to see this visually if you run helm template --debug over your chart; you'll see the same error but also the template output that can't be parsed.

(@z.x's answer solves the same problem a different way: putting the - inside the curly braces removes both the leading whitespace and also the preceding newline, and then changing indent to nindent puts the newline back.)

Upvotes: 0

z.x
z.x

Reputation: 2527

values.yaml

secretFiles:
  - name: helm-template-file
    subPath: ".file1"
    mode: "0644"
    value: |
      This is a multiline
      value, aka heredoc.
  - name: helm-template-file2
    subPath: ".file2"
    mode: "0644"
    value: |
      This is a multiline222
      value, aka heredoc.

xxx_tpl.yaml

...
stringData: 
  {{- range .Values.secretFiles }}
  {{ .subPath}}: |
    {{-  .value  | nindent 4}}
  {{- end }}

output

...
stringData:
  .file1: |
    This is a multiline
    value, aka heredoc.
    
  .file2: |
    This is a multiline222
    value, aka heredoc.

ref:

nindent
The nindent function is the same as the indent function, but prepends a new line to the beginning of the string.

indent
The indent function indents every line in a given string to the specified indent width

Upvotes: 1

Related Questions