Reference Values in Helm method

6/22/2020

I'm trying to reference a value from my Values.yaml file in Helm in a Files.Glob method.

Here is my Configmap template:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm
data:
  {{- tpl ((.Files.Glob "qa.ns1/*").AsConfig) . | nindent 2 }}

The above works as expected.

The folder structure is "environment.namespace". ns1 is the name of my namespace. How do I reference a value from my Values.yaml file instead of ns1?

I have tried:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm
data:
  {{- tpl ((.Files.Glob "qa.{{ .Values.namespace }}/*").AsConfig) . | nindent 2 }}

Where my Values.yaml file is:

namespace: ns1

I get no data in my configmap when I reference it with above. Any help would be appreciated!

-- firebolt
configmap
kubernetes
kubernetes-helm

1 Answer

6/22/2020

I'd use the Go text/template printf function. Split out into variables this would look like:

data:
  {{- $glob := printf "qa.%s/*" .Values.Namespace }}
  {{- $data := (.Files.Glob $glob).AsConfig }}
  {{- tpl $data . | nindent 2 }}

The Sprig cat function would also work

{{- $glob := cat "qa." .Values.Namespace "/*" }}

Once you're inside the {{ ... }} template block, you can't nest template blocks, but you can just directly refer to variables and Helm values.

-- David Maze
Source: StackOverflow