Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 943
Bluebluebleu Avatar asked Feb 13 '19 12:02

Bluebluebleu


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 is TPL function in Helm?

The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files.

Can we use and operator in Helm YAML files?

As indicated in the Helm documentation on operators: For templates, the operators ( eq , ne , lt , gt , and , or and so on) are all implemented as functions. In pipelines, operations can be grouped with parentheses ( ( , and ) ).

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) . -}}


1 Answers

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

like image 141
Crou Avatar answered Sep 29 '22 03:09

Crou