Is it possible to create a configmap with an empty/blank key? If yes, what should be the expected behaviour with respect to the pods?

1/18/2019

Want to understand if it's possible to create a configmap with a blank or empty key. The value isn't empty though.

-- Vinodh Nagarajaiah
configmap
kubernetes

1 Answer

1/18/2019

No, it is not possible. While the YAML syntax allows for an empty string to be specified as the key, Kubernetes validation will not accept it:

$ cat test-cm.yaml
apiVersion: v1
data:
  key1: value1
  key2: value2
  "": value3
kind: ConfigMap
metadata:
  name: test-cm
$ kubectl apply -f test-cm.yaml
The ConfigMap "test-cm" is invalid: data[]: Invalid value: "": a valid config key must consist of alphanumeric characters, '-', '_' or '.' (e.g. 'key.name',  or 'KEY_NAME',  or 'key-name', regex used for validation is '[-._a-zA-Z0-9]+')
$

The validation regexp printed in the error message [-._a-zA-Z0-9]+ clearly states that the key length may not be zero.

Using the null key is also unacceptable to Kubernetes:

$ cat test-cm.yaml 
apiVersion: v1
data:
  key1: value1
  key2: value2
  ?
  : value3
kind: ConfigMap
metadata:
  name: test-cm
$ kubectl apply -f test-cm.yaml 
error: error converting YAML to JSON: Unsupported map key of type: %!s(<nil>), key: <nil>, value: "value3"
$
-- Laszlo Valko
Source: StackOverflow