The Problem
Have you seen this before? When you create a ConfigMap with a multiline data block!
Here is an example: When you create the following ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: poc data: application.yaml: | foo: bar baz: qux
It shows like that after it's applied (you can see it kubectl get cm poc -o yaml):
apiVersion: v1 kind: ConfigMap metadata: name: poc data: application.yaml: "foo: bar\n\nbaz: qux \n"
In fact, that issue occurs in any data under certain circumstances, not only in YAML data.
The Root Cause
This isn't a bug per se, but it's how Kubernetes handles whitespace and interpretation of multiline strings in the ConfigMaps.
Main resons for that:
- An empy line.
- An empy space at the end of the line.
- Tabs intendation instead of spaces.
Just ensure you don't have extra spaces in your data. It happens even more in Helm templates because some times some values are empty or missing!
Bonus
OK, what if you don't have the source file to fix the ConfigMap, don't want to edit the ConfigMap, or just want to view that messy ConfigMap? You can still do that easily using kubectl with jsonpath:
kubectl get configmap poc -o jsonpath="{.data.application\.yaml}"
That will print the data properly:
foo: bar baz: qux
That's it! Happy DevOpsing! :-)