Is it possible to expose the contents of application.properties as key/value pairs in env inside the container?

8/28/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 defined configmap as below

{{ range $path, $bytes := .Files.Glob "config/*" }}
  {{ base $path }}: '{{- $.Files.Get $path | nindent 2 | upper | replace "." "_" }}'
  {{- end }}

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

Post running helm install, I see that configmap is generated.

kubectl describe cm sample-configmap
Data
====
----
 SERVER_PORT = 8080 SERVER_NAME = LOCALHOST SERVER_GREETING = GREETING-SERVICE 
----

In configmap, the contents are not generated as single string instead of key/value pair.

Even, inside the container, the values are not stored as key/value pair. they are stored as string.

kubectl exec -it <pod> sh
/data # env | grep application.properties
application.properties= SERVER_PORT = 8080 SERVER_NAME = LOCALHOST SERVER_GREETING = GREETING-SERVICE SAMPLE_GROUPA_PARAM1 = VALUE1BASE SAMPLE_GROUPA_PARAM2 = VALUE2BASE SAMPLE_HIGHERPARAM = VALUEHIGHERBASE
/data # echo $application.properties
.properties
/data # echo $SERVER_PORT

I have also tried

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

observe the same issue.

Is it possible to expose the contents of application.properties as key/value pairs in env inside the container?

-- Sunil Gajula
configmap
kubernetes
kubernetes-helm

1 Answer

8/28/2019

The issue is the way you are reading your files and configmap configuration. Assuming your file from config folder looks like this (please note that integers should be included in as quoted strings):

config.ini

SERVER_PORT = "8080" 
SERVER_NAME = LOCALHOST 
SERVER_GREETING = GREETING-SERVICE 
SAMPLE_GROUPA_PARAM1 = VALUE1BASE 
SAMPLE_GROUPA_PARAM2 = VALUE2BASE 
SAMPLE_HIGHERPARAM = VALUEHIGHERBASE

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
data:
{{ range $path, $bytes := .Files.Glob "config/*" }}
  {{- $.Files.Get $path | indent 2 | upper | replace "." "_" | replace " = " ": " }}
{{- end }}

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config

output:

$ kubectl logs test-pod
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
SAMPLE_GROUPA_PARAM1=VALUE1BASE
SAMPLE_GROUPA_PARAM2=VALUE2BASE
HOSTNAME=test-pod
...
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
SERVER_GREETING=GREETING-SERVICE
SERVER_PORT=8080
SERVER_NAME=LOCALHOST

See Configure all key-value pairs in a ConfigMap as container environment variables

-- edbighead
Source: StackOverflow