Hello Guys, I have created a config map my-config from a file *configmapDataSou . . .

Hossam Hassan:
Hello Guys,

I have created a config map my-config from a file configmapDataSource

hoss@localhost ~]$ cat configmapDataSource
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
key6=value6

using the below command
kubectl create configmap my-config --from-file=/home/hoss/configmapDataSource

config map my config

hoss@localhost ~]$ kubectl describe configmaps my-config
Name: my-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
configmapDataSource:
----
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
key6=value6
BinaryData
====
Events: <none>

Then i have created a pod mypod which reference my-config key2

Mypod yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2023-02-20T00:42:21Z"
labels:
run: mypod
name: mypod
namespace: default
resourceVersion: "209591"
uid: cb9161da-887e-439d-9509-fb88c23178b5
spec:
containers:
- env:
- name: test
valueFrom:
configMapKeyRef:
key: key2
name: my-config

Now the pod is giving a config error that it cant find key2 in my config

Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 2s kubelet Pulling image “redis”
Normal Pulled 0s kubelet Successfully pulled image “redis” in 1.691770932s (1.691794944s including waiting)
Warning Failed 0s kubelet Error: couldn’t find key key2 in ConfigMap default/my-config

The question here why the pod can’t find the key while it shows in the configmap ?

Another question when i add new value in configmapDataSource its not added to the config map ?

Sergey Lozenkov:
Hi
couldn’t find key key2 in ConfigMap default/my-config
It is because this CM really does not have key with name key2.
For file, like You presented please use option “–from-env-file”
kubectl create configmap my-config --from-env-file=configmapDataSource
Or You can create configmap not from file, but from literals (like k create cm test --from-literal=key1=value1 --from-literal=key2=value2 --from-literal=key3=value3) and use key 2 as You want.

Hossam Hassan:
How it doesn’t have key2 while the output of the description of the configmap have key2 as follows

[hoss@localhost ~]$ kubectl describe configmaps my-config
Name: my-config
Namespace: default
Labels: <none>
Annotations: <none>

Data
====
configmapDataSource:
----
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
key6=value6

Sergey Lozenkov:
You can compare what You see in this CM with configmap that was created with --from-env-file option.

Hossam Hassan:
Thanks for reply I have created from-env file as suggested from your side and its working fine

The question here is why it didn’t work in the first time i mean how the -from-file flag is used

Thank you again

Alistair Mackay:
from-file created a file inside the config map under the single key configmapDataSource and the content being all six key-values.

from-env-file tells it to create individual keys for each line of the input file

Aneek Bera:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/

Aneek Bera:
https://matthewpalmer.net/kubernetes-app-developer/articles/configmap-example-yaml.html

Aneek Bera:
I’d say that the issue occurred because you are passing a ConfigMap yaml definition as parameter of --from-file.
You could simply create it using: kubectl create -f -----------.yaml

Hossam Hassan:
Thank you all for your support