Kubernetes ConfigMap: import as many values ββfrom file as one?
Creating a new ConfigMap from a file: kubernetes create configmap foo --from-file=foo
This is what ConfigMap looks like inside:
kubernetes get configmaps foo -o yaml
apiVersion: v1
data:
foo: |
VAR1=value1
VAR2=value2
Then I use this ConfigMap to create a set of environment variables in the container:
apiVersion: v1
kind: Pod
metadata:
labels:
name: app
name: app
spec:
containers:
- name: app-server
image: app:latest
ports:
- containerPort: 3000
envFrom:
- configMapRef:
name: foo
command: ["/bin/bash", "-c", "printenv"]
When the container command runs, I see the following output for printenv
:
foo=VAR1=value1
VAR2=value2
So the command echo $foo
in the return of the pod is:VAR1=value1 VAR2=value2
According to the documentation for ConfigMap, --from-file
this is the expected behavior.
What will be a creative way (and proper place), to somehow get the value of this file for module as separate variables the env VAR1
, VAR2
, VAR3
etc?
source to share
This is not possible in the current version (1.6.x) of Kubernetes. As written in the official documentation for kubectl create configmap
:
- from-file: The key file can be specified using its file path, in which case the base filename will be used as the configuration key, or optionally with the key and file path, in which case this key will be used. Specifying a directory will iterate over every named file in the directory whose base name is a valid config key.
If you want to create a configmap that is used like this as the input for the container configuration envFrom
, you can create it with a parameter --from-literal
like this:
kubectl create configmap foo --from-literal=var1=value1 --from-literal=var2=value2
To save the file, you can convert your file to something, which then runs this command like this:
eval "kubectl create configmap foo $(cat foo.txt | sed -e 's/^/--from-literal=/' | tr "\n" ' ')"
Along with that, it might be worth your time checking out outstanding proposals like the --flatten
flag proposal on Github .
Also watch out for variable names. iirc VAR1
and VAR2
are not valid property names - they must be in lower case, which can cause some problems when passing them.
source to share