Using envFrom array or object

Hello,

when we use envFrom to inject comlete configmap to our pod, envFrom should come as an array or as an object? I check kubecnetes documentation, there they are using envFrom as object :

image

while when I was doing lab exercise there I got error as it expect envFrom as array and same I saw in solution video too.

image

Also what’s the difference in both approach? How to decide if we need to use envFrom as an array or as an object?

Thanks
Ashish

hi @06ashishrawat,

envFrom as array
EnvFrom is a 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. Immutable.

envFrom[] as object
EnvFromSource represents the source of a set of ConfigMaps

Hello @KodeKloud,

my appologies but I didn’t understand the above reply. Also when I was doing lab for secret there we used envFrom as object. So I am still confused how to decide if envFrom need to be use as an array or as on object.

Can you please explain the answer with examples?

Thanks
Ashish

Hello, @06ashishrawat
I think you’re getting confused because of their order.

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

Both are same meaning.

If you’re defining for multiple containers POD, then it would be like:

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

Thanks a lot @player001 … So it was the order that made me confused but now it’s clear. Thank You Very Much.

Regards
Ashish