Home > OS >  Helm Chart - Process File result in multiple lines
Helm Chart - Process File result in multiple lines

Time:02-11

I have a ConfigMap where I am including a file in its data attribute and I need to replace several strings from it. But I'm not able to divide it (the "replaces") into several lines so that it doesn't get a giant line. How can I do this?

This is what I don't want:

apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 | replace "var1_enabled" (toString .Values.myVar1.enabled) | replace "var2_enabled" (toString .Values.myVar2.enabled) }}

This is what I'm trying to do:

apiVersion: v1
kind: ConfigMap
data:
{{ (.Files.Glob "myFolder/*.json").AsConfig | indent 2 |
  replace "var1_enabled" (toString .Values.myVar1.enabled) |
  replace "var2_enabled" (toString .Values.myVar2.enabled) }}

What is the right syntax to do this?

CodePudding user response:

What is the right syntax to do this?

It is well described in this documentation. There are many different ways to achieve your goal, it all depends on the specific situation. You have everything in that documentation. Look at the example most connected to your current situation:

When writing templates, you may find yourself wanting to inject the contents of a file into the template. As we saw in previous chapters, there are two ways of doing this:

  • Use {{ .Files.Get "FILENAME" }} to get the contents of a file in the chart.
  • Use {{ include "TEMPLATE" . }} to render a template and then place its contents into the chart.

When inserting files into YAML, it's good to understand the multi-line rules above. Often times, the easiest way to insert a static file is to do something like this:

myfile: |
{{ .Files.Get "myfile.txt" | indent 2 }}

Note how we do the indentation above: indent 2 tells the template engine to indent every line in "myfile.txt" with two spaces. Note that we do not indent that template line. That's because if we did, the file content of the first line would be indented twice.

For more look also at the similar problem on github and question on stack.


EDIT:

But I'm not able to divide it (the "replaces") into several lines so that it doesn't get a giant line. How can I do this?

It is impossible to achieve. Go Template doesn't support newlines. For more see this question. and this documentation

The input text for a template is UTF-8-encoded text in any format. "Actions"--data evaluations or control structures--are delimited by "{{" and "}}"; all text outside actions is copied to the output unchanged. Except for raw strings, actions may not span newlines, although comments can.

CodePudding user response:

As is not possible to split the expression into multiple lines at the ConfigMap, I finally got a solution using the _helpers.tpl file.

Basically I created a dictionary with all the variables I want to replace and the respective new value to replace, and then I iterated over that dict and did the replace at my files's config:

{{/*
Manage the files and replace some variables
*/}}
{{- define "myFiles" -}}
{{ $filesConfig := (.Files.Glob "myFolder/*.json").AsConfig }}

{{ $myVars := dict "var1_enabled" (toString .Values.myVar1.enabled) }}
{{ $myVars = merge $myVars (dict "var2_enabled" (toString .Values.myVar2.enabled)) }}

{{ range $key, $value := $myVars }}
{{ $filesConfig = ($filesConfig | replace $key $value) }}
{{ end }}

{{ $filesConfig }}
{{- end -}}

And then I changed my ConfigMap to something like this:

{{- $myFiles := include "myFiles" . -}}
apiVersion: v1
kind: ConfigMap
data:
  {{ $myFiles }}

I don't nothing about this kind of language, so if you know how to improve this, please feel free to comment.

  • Related