Helm - Configmap - Replace only the contents of file and not File Names

8/27/2019

I have defined application.properties files in a config dir. Config dir is on the below structure.

config
  application.properties
  application-test.properties
  application-dev.properties
  application-prod.properties

I have created a helm chart to create the configmap. Helm chart is defined as below

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  {{- (.Files.Glob "config/*").AsConfig | nindent 2 }}

I see that the configmap is created.

We are consuming the ConfigMap via environment variables in a running container using the envFrom property. (This in my deployment yaml file)

spec:
   containers:
   - envFrom:
     - configMapRef:
         name: nginx-configmap

I see that the values are stored as environment variables. However the variables are in lower cases.

server.port=8080
server.name=localhost

Since these are env variables, they have to be uppercase and . should be replaced with _. So, I have modifed my chart as below

data:
  {{- (.Files.Glob "config/*").AsConfig | nindent 2 | upper | replace "." "_" }}

The generated configmap is as below

  APPLICATION_PROPERTIES: |
    SERVER_PORT = 8080
    SERVER_NAME = LOCALHOST

Below is the env variables inside container

APPLICATION_PROPERTIES=SERVER_PORT = 8080
SERVER_NAME = LOCALHOST

My requirement is that only the contents of the file should be upper case and . should be replaced with _. The filename should not be converted. The filename should be as is.

Can this be achieved?

-- Sunil Gajula
helmfile
kubernetes
kubernetes-helm

1 Answer

8/27/2019

Try this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configmap
data:
  {{ $root := . }}
  {{ range $path, $bytes := .Files.Glob "config/*" }}
  {{ base $path }}: '{{ $root.Files.Get $path | nindent 2 | upper | replace "." "_"  }}'
  {{ end }}
-- Emruz Hossain
Source: StackOverflow