accessing dynamic file in helm

2/21/2022

I am new to kubernetes helm chart. There is a yaml file named configmap. This file containing all the configuration which are related to the application. Since this file containing a lot of data, I was trying to put some data to the new file and access using FILE object. So created two different file with the name: data1.yaml and data2.yaml <br> data1.yaml file have only static data. On the other hand data2.yaml file contains dynamic data (some variables also like $.Values.appUrl). I am able to read static file (data1.yaml) to the configmap.yaml file using FILE object. I am also able to read data2.yaml file but since this file containing some variable also, the variable value is not replacing by actual value. It printing the same variable in configmap file. So my question is, Is there any way to access dynamic file (which contains variable type data) ? <br> Below is the example data shown.<br><br> configmap.yaml file is->

kind: ConfigMap
apiVersion: v1
metadata:
  name: example-configmap
  namespace: default
data1: {{ .File.Get "data1.yaml" | indent 2 }}
data2: {{ .File.Get "data2.yaml" | indent 2 }}

data1.yaml file is ->

data1:
  ui.type:2
  ui.color1:red
  ui.color2:green

data2.yaml file is ->

system.data.name: "app-name"
  system.data.url: {{ $.Values.appUrl }}   # variable
  system.data.type_one: "app-type-xxx"
  system.data.value: "3"
  system.interface.properties: |  

Values.yaml file is ->

appUrl: "https://app-name.com"

Output:

kind: ConfigMap
apiVersion: v1
metadata:
  name: example-configmap
  namespace: default
data1:
  ui.type:2
  ui.color1:red
  ui.color2:green
data2:
  system.data.name: "app-name"
  system.data.url: {{ $.Values.appUrl }}    # here should be "https://app-name.com" 
  system.data.type_one: "app-type-xxx"
  system.data.value: "3"
  system.interface.properties: |
-- md samual
helmfile
kubernetes
kubernetes-helm

2 Answers

2/22/2022
{{ (tpl (.Files.Glob "data2.yaml").AsConfig . ) | indent 2 }}

Using above syntax it's picking the actual value of variables. But it also printing the file name like below:

data2.yaml: |-

So I resolve the issue by using below syntax:

{{ (tpl (.Files.Get "data2.yaml") . ) | indent 2 }}
-- md samual
Source: StackOverflow

2/21/2022

You can use the tpl function to process the templated file like this:

configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-configmap
data:
{{ (tpl (.Files.Get "data2.yaml") . ) | indent 2 }}

Note that configmap has data key by default. I think you should capture your data under this key, instead of creating data1 and data2 but not sure.

values.yaml:

appUrl: "https://app-name.com"

data2.yaml: (indentations corrected, otherwise it fails)

system.data.name: "app-name"
system.data.url: {{ .Values.appUrl }}   # variable
system.data.type_one: "app-type-xxx"
system.data.value: "3"
system.interface.properties: |
-- murtiko
Source: StackOverflow