Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override env values defined in container spec

Tags:

I have a configmap where I have defined the following key-value mapping in the data section:

apiVersion: v1 kind: ConfigMap metadata:   namespace: test   name: test-config data:   TEST: "CONFIGMAP_VALUE" 

then in the definition of my container (in the deployment / statefulset manifest) I have the following:

        env:         - name: TEST           value: "ANOTHER_VALUE"         envFrom:         - configMapRef:             name: test-config 

When doing this I was expecting that the value from the configmap (TEST="CONFIGMAP_VALUE") will override the (default) value specified in the container spec (TEST="ANOTHER_VALUE"), but this is not the case (TEST always gets the value from the container spec). I couldn't find any relevant documentation about this - is it possible to achieve such env variable value overriding?

like image 535
asenovm Avatar asked Jan 28 '19 08:01

asenovm


People also ask

How do I override Docker-compose environment variables?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.

When defining an environment variable What is the part of the pod spec where you set the value?

When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file.

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a DockerfileDockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.


1 Answers

From Kubernetes API reference:

envFrom : List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.

So above clearly states the env will take precedence than envFrom.

When a key exists in multiple sources, the value associated with the last source will take precedence.

So, for overriding, don't use envFrom, but define the value twice within env, see below:

apiVersion: v1 kind: ConfigMap metadata:   namespace: default   name: test-config data:   TEST: "CONFIGMAP_VALUE" --- apiVersion: v1 kind: Pod metadata:   name: busy   namespace: default spec:   containers:   - name: busybox     image: busybox     env:     - name: TEST       value: "DEFAULT_VAULT"     - name: TEST       valueFrom:         configMapKeyRef:           name: test-config           key: TEST     command:     - "sh"     - "-c"     - >       while true; do         echo "$(TEST)";         sleep 3600;       done 

Check:

kubectl logs busy -n default CONFIGMAP_VALUE 
like image 110
nightfury1204 Avatar answered Oct 01 '22 01:10

nightfury1204