Hardly readable data in config maps - Wed, Feb 16, 2022
Hardly readable data in config maps
Unreadable data in config maps
During development on my local Kubernetes cluster i often use the kubectl edit command for quick and dirty changes to resources. When editing config maps I sometimes found that the data was unreadable compared to the actual data of the original manifest. This blog post describes the cause for this and possible resolutions.
A look at the messed up data
Since I mostly used json as data in the config maps, the data was unreadable because of escaped new lines. Here is an example of a Grafana dashboard's json :
$ kubectl get configmaps grafana-dashboard -o yaml
apiVersion: v1
data:
new_dashboard.json: "{\n \"id\": null, \n \"uid\": \"cLV5GDCkz\",\n \"title\":
\"New dashboard\",\n \"tags\": [],\n \"style\": \"dark\",\n \"timezone\": \"browser\",\n
\ \"editable\": true,\n \"hideControls\": false,\n \"graphTooltip\": 1,\n \"panels\":
[],\n \"time\": { \"from\": \"now-6h\", \"to\": \"now\" },\n \"timepicker\":
{ \"time_options\": [], \"refresh_intervals\": [] },\n \"templating\": { \"list\":
[] },\n \"annotations\": { \"list\": [] },\n \"refresh\": \"5s\",\n \"schemaVersion\":
17,\n \"version\": 0,\n \"links\": [],\n}"
kind: ConfigMap
...
While the content in the original file looked like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: grafana-dashboard-no-space
data:
new_dashboard.json: |-
{
"id": null,
"uid": "cLV5GDCkz",
"title": "New dashboard",
"tags": [],
"style": "dark",
...
Which is perfectly readable. In addition not all config maps had this effect. So something must happen to particularly structured data during kubectl apply
.
The cause
After doing some searching, I found this post on stackoverflow
which contains the explanation:
Tabs and new spaces at the end of a line cause kubectl
to escape new lines. And indeed after taking a look at the original manifest in my editor I spotted the space at the end of the first line. Although my editor shows spaces and tabs I sometimes must have missed these. Often using copy and paste may have also contributed to the problem.
The solution
Now the solution is to remove these tabs and spaces and the end of the line. In order to do this automatically from the command line, two sed commands can be used:
sed -i -E 's/[[:space:]]+$//g' file.txt # trailing white spaces
sed -i 's/\t/ /g' file.txt # Replace tab with white spaces
Which where also part of the solution on stackoverflow.
Conclusion
Using an editor that shows spaces and tabs helps avoiding these kind of surprises. Some IDEs like VSCode offer shortcuts for doing exactly that:
press F1 and select/type "Trim Trailing Whitespace"
press F1 and select/type "Convert Identation to Spaces"
If you want to do this automatically sed can help you save some time.