Use Helm to Loop thru lines of dotenv file and render as key-value pair in ConfigMap

1.7k views Asked by At

I have this .env file :

REACT_APP_API_ENDPOINT=http://api.app:8080
REACT_APP_GOOGLE_ANALY=xyz1234ezyz

I want to build a configmap out of this .env file which looks like :

apiVersion: v1
kind: ConfigMap
metadata:
  name: frontend-config

data:
 REACT_APP_API_ENDPOINT: 'http://api.app:8080'
 REACT_APP_GOOGLE_ANALY: 'xyz1234ezyz'
  

The algorithm is straightforward :

 forEach Line of `.env` content
    > Split by "="
    > key <- first part , value <- second part
    > render key, value in 

My helm chart default values is :

# chart/values.yaml

# using --set-file
frontendEnv: |
 REACT_APP_API_ENDPOINT=http://api.app:8080
 REACT_APP_GOOGLE_ANALY=xyz1234ezyz

My configmap template :

apiVersion: v1
kind: ConfigMap
metadata:
 #...

data:

# MY QUESTION is what to put Here 

I've tried this loop :

# ...
# ..
data:
{{- range $line := splitList "\n" .Values.frontendEnv -}}
{{/* Break the line into words */}}
{{- $kv := split "=" $line -}}
  $kv._0: {{ $kv._1 | quote }}
{{- end -}}

but it does not work

1

There are 1 answers

0
Abdennour TOUMI On BEST ANSWER

Fixed .

  • use {{- range ... }} not {{- range... -}} to keep new line for each iteration.

  • use splitList not split

  • check if line is not empty

{{- range $line := splitList "\n" .Values.frontendEnv }}
  {{/* Break the line into words */}}
  {{- $kv := splitList "=" $line -}}
  {{- $k := first $kv -}}
  {{- if $k }}
    {{ $k }}: {{ last $kv | quote }}
  {{- end }}

{{- end }}