Home > Software engineering >  Helm Variables inside ConfigMap File
Helm Variables inside ConfigMap File

Time:08-26

So I had a ConfigMap with a json configuration file in it, like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  config.json: | 
  {
    "some-url": "{{ .Values.myApp.someUrl }}"
  }

But I've moved to having my config files outside the ConfigMap's yaml, and just referencing them there, like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  config.json: |-
{{ .Files.Get .Values.myApp.configFile | indent 4 }}

But now I want my json to look like the following

{
  "some-url": "{{ .Values.myApp.someUrl }}"
}

The only thing I tried is what I just showed. I 'm not even sure how to look for this answer.

Is it even possible?

CodePudding user response:

At the time of reading the file, its content is a string. It's not evaluated as template, and therefore you cannot use variables like you do.

However, helm has a function for this purpose specifically called tpl:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  config.json: |- 
{{ tpl (.Files.Get .Values.myApp.configFile) $ | indent 4 }}

The tpl function takes a template string and renders it with some context. This is useful when you have template snippets in your values file or like in your case in some files content.

  • Related