Inject env var for all ConfigMap values

5/4/2019

I am searching for a way where i can inject env variable value to configmap so by defining LOG_SEVERITY as part of my deployment as env variable the value will be automatilcy injected to the configmap.

kind: ConfigMap
apiVersion: v1
data:
  log.properties: |
    timeout=10
    severity=${LOG_SEVERITY}
-- George Armand
kubernetes

2 Answers

5/5/2019

You would have to use some kind of templating tool to process the YAML. Helm is popular (though that doesn't directly handle environment variables). For your case I would strongly recommend envsubst, it will handle this case nicely.

-- coderanger
Source: StackOverflow

5/6/2019

Like @coderanger mentioned you can use envsubst and Helm. I'll just provide tad more details and a another way ;)

envsubst

In your config_map.yml you would have:

severity= $LOG_SEVERITY

Then just create new environmental variable and execute kubectl in a following way:

export LOG_SEVERITY="9"
envsubst < config_map.yml | kubectl apply -f -

Helm

Create a template and calling it.

kind: ConfigMap
apiVersion: v1
data:
  log.properties: |
    timeout=10
    severity= {{ .Values.logSeverity }}

You would need to define a default value of severity inside mychart/values.yaml

logSeverity: 1

And setting a different value by adding --set flag to the helm call.

helm install --set logSeverity=9 ./mychart

This is nicely explained inside Helm documentation The Chart Template Developer’s Guide. Keep in mind this is way more complicated to just use helm as a wrapper for Kubernetes files.

sed

sed -i 's/$LOG_SEVERITY/9/g' input.txt

I recommend reading How to use sed to find and replace text in files in Linux / Unix shell.

-- Crou
Source: StackOverflow