kubernetesyamlkubernetes-helmconfigmap

Helm Chart - Process File result with multiple lines


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?


Solution

  • 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.