Search code examples
kuberneteskubernetes-helmkubernetes-pod

How to read env property file in the config map


I have a property file in chart/properties folder. For example chart/properties/dev is the file and the contents of it looks like the below

var1=somevalue1
var2=somevalue2


var3=somepwd=

var4=http://someurl.company.com

some of the value strings in property file have an =. There are also some empty lines in the property file.

and chart/configmap.yaml looks like below

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-configmap
  namespace: {{ .Release.Namespace }}
data:
{{ range .Files.Lines "properties"/.Values.env.required.environment }}
  {{ . | replace "=" ": " }}
{{ end }}

Generated yaml file:

---
# Source: app/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-configmap
  namespace: default
data:

  var1: somevalue1

  var2: somevalue2
  
  var3: somepwd:

  var4: http://someurl.company.com

The generated output property entries are missing double quote in the value, as a result deployment complains of it when the value strings contain special characters.

I'm expecting the configmap.yaml data block to be a proper yaml (Indent 2) like file with the above changes. With above changes, there are extra lines after each property entry in yaml file. I got this to work partially when there are no blank lines and no value strings with =. Need help to get this working correctly.

Expected yaml file:

---
# Source: app/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-configmap
  namespace: default
data:
  var1: "somevalue1"
  var2: "somevalue2"
  var3: "somepwd="
  var4: "http://someurl.company.com"

Solution

  • You can follow go template syntax to do that. I update config.yaml like following works

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: env-configmap
      namespace: {{ .Release.Namespace }}
    data:
      {{ range .Files.Lines "properties"/.Values.env.required.environment }}
        {{- if ne . "" -}}
        {{- $parts := splitn "=" 2 . -}} # details about split function http://masterminds.github.io/sprig/string_slice.html
        {{ $parts._0 }}: {{ $parts._1 | quote }}
        {{end}}
      {{ end }}