Image showing Multiline Properties in kubernetes Helm with ConfigMaps

Multiline Properties in kubernetes Helm with ConfigMaps

affiliate best offer

Summary

There are several APIs in Kubernetes. A ConfigMap is one of these. It is an object which helps to storing some non-confidential data. The data is in the form of key-value pairs. Some use envrionment variables. In this blog post, we will see how you can show multi-value lines.

Working with Kubernetes Helm, I went to the documentation. The link to the documentation is here.

In Helm’s v3 documentation, in the section Accessing Files Inside Templates, you have an example of 3 properties (toml) files; where each file has only one key/value pair.

The configmap.yaml looks like this. It contains one config.toml for simplicity.

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-config
data:
  {{- $files := .Files }}
  {{- range tuple "config.toml" }}
  {{ . }}: |-
    {{ $files.Get . }}
  {{- end }}

I was happy with it. Then I add a second line to the config.toml file.

config.toml



replicaCount=1
foo=bar

Then boom, I get an Error:

INSTALLATION FAILED: YAML parse error on deploy/templates/configmap.yaml: error converting YAML to JSON: yaml: line 9: could not find expected ':'

Digging in this error, I found a solution.

The solution

Helm will read in that file, but as it is a text templating engine, It does not understand that I was trying to compose a YAML file.

As a consquence, it was not helping me in the error.

That’s actually why you will see so many, many templates in the wild with

{{ .thing | indent 8 }}

or

{{ .otherThing | toYaml }}

– because you need to help Helm know in what context it is emitting the text.

So, in my specific case, I needed to indent the filter with a value of 4 because mycurrent template has two spaces for the key indent level, and two more spaces for the value block scalar

data:

  {{- $files := .Files }}
  {{- range tuple "config.toml" }}
  {{ . }}: |-
{{ $files.Get . | indent 4 }}
{{/* notice this ^^^ template expression is flush left,
because the 'indent' is handling whitespace, not the golang template itself */}}
  {{- end }}

I hope this quick post will help someone in his research. Helm is a very powerfull tool in the Kubernetes world. It is very useful when building code-server on Digital Ocean.

Inspiration from this post.

Full Bright

Full Bright

A professional and sympathic business man.

Contact

Contact Us

Not sure which of these you need? Book the free 20-minute audit — we look at one week of your work and tell you what is worth automating, what is not, and what it would cost. Book it here.

One audit call a week, Thursdays or Fridays — book early if a specific day matters to you.

Address

10 rue François 1er,
75008 Paris

When we work

Monday - Friday
9:00AM - 05:00PM