How to control indent after including file content in Helm configMap?
Asked Answered
B

1

14

I'm setting up a ConfigMap for my Helm chart.

As per good practice, I want to include non-yaml resources through separate files rather than inline. Currently I am trying to include both an xml file and a tpl helper in my ConfigMap under "data". Both are read without issue in the below code. But I cannot seem to make the indentation for the keys work properly.

My ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ template "name" . }}
    labels:
        app: {{ template "name" . }}
        chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
        release: {{ quote .Release.Name }}
        heritage: {{ quote .Release.Service }}
        name: {{ template "name" . }}
data:
    logback.xml: |-
        {{- .Files.Get "application-resources/logback.xml" | nindent 8 -}}
    application.yml: |-
        {{- include "application.yml" . | nindent 8 -}}

This produces the following indentation (actual values are removed for readability):

apiVersion: v1
kind: ConfigMap
metadata:
    name: erklaering-anden-lov-detektor-app
    labels:
        app: name-of-app
        chart: name-of-chart
        release: "release-name"
        heritage: "Tiller"
        name: name-of-app
data:
    logback.xml: |-
      <?xml version="1.0" encoding="UTF-8"?>
      <configuration scan="true">
          <xml-stuff>
      </configuration>
      application.yml: |-    
      application.yml.contents

Which should be:

apiVersion: v1
kind: ConfigMap
metadata:
    name: erklaering-anden-lov-detektor-app
    labels:
        app: name-of-app
        chart: name-of-chart
        release: "release-name"
        heritage: "Tiller"
        name: name-of-app
data:
    logback.xml: |-
      <?xml version="1.0" encoding="UTF-8"?>
      <configuration scan="true">
          <xml-stuff>
      </configuration>
    application.yml: |-    
      application.yml.contents

I'm at my wit's end. What am I doing wrong? How do I make yaml snap back to recognizing configMap's own indentation and/or explicitly control it?

Britten answered 13/2, 2019 at 12:57 Comment(0)
C
17

Try this:

data:
    logback.xml: |-
        {{- .Files.Get "application-resources/logback.xml" | nindent 8 }}
    application.yml: |-
        {{- include "application.yml" . | nindent 8 -}}

I've removed the "-" from the 3rd line as it removes following whitespace.

You can also have a look at this GitHub Issue #3470.

If you need more help, you can check the documentation for Chart Development Tips and Tricks

Consolidate answered 14/2, 2019 at 10:19 Comment(1)
Note on the side: Files.Get assumes files terminated by linefeed. In case the XML file has carriage returns (as under Windows) Helm will issue the error message: error converting YAML to JSON: yaml: line 12: could not find expected ':'. Just convert the XML file to linefeed.Gaskins

© 2022 - 2024 — McMap. All rights reserved.