kubernetes configmap prints \n instead of a newline kubernetes configmap prints \n instead of a newline kubernetes kubernetes

kubernetes configmap prints \n instead of a newline


This seems to be similar to kubernetes/kubernetes issue 36222 when creating configMap from files.

In your case, that happens when created from a data block.

The recent kubernetes/kubernetes issue 63503 references all printed issues.

A comment mentions:

I added a new line in a configMap using Tab for identation. After changing to Spaces instead of Tab, I was able to see the configmap as expected...

August 202: The issue 36222 now includes:

If you just want the raw output as it was read in when created --from-file, you can use jq to get the raw string (without escaped newlines etc)

If you created a configmap from a file like this:

kubectl create configmap myconfigmap --from-file mydata.txt

Get the data:

kubectl get cm myconfigmap -o json | jq '.data."mydata.txt""' -r

Also:

If the formatting of cm goes wierd a simple hack to get it back to normal is :

kubectl get cm configmap_name -o yaml > cm.yaml

Now copy the contents of cm.yaml file and past it on yamllint.com. Yamllint.com is powerful tool to check the linting of yaml files.
This will provide you with the configmap as expected with correct formatting.

Paste the output in another yaml file (for e.g - cm_ready.yaml)

 kubectl apply -f cm_ready.yaml

Update Nov. 2020, the same issue includes:

I was able to fix this behavior by:

  • Don't use tabs, convert to spaces

  • To remove spaces before a newline character, use this:

      sed -i -E 's/[[:space:]]+$//g' File.ext

It seems also will convert CRLF to LF only.


Using Kubernetes 1.20.2, the issue was fixed by:

  • removing trailing whitespaces using: sed -i -E 's/[[:space:]]+$//g' file.txt
  • replacing tabs with spaces using sed -i 's/\t/ /g' file.txt (WARN: manage manually number of spaces!)
  • removing blank lines at the end of the file


As stated in the Github issue, you need to remove all whitespace from the end of each line, and make sure you don't have any special characters as well.

If you are doing this programatically, you'll have better luck with single-line strings, rather than multiline. e.g. in go use "" + "\n" rather than backticks.

The correct result should use a pipe |

data: | some = foo foo = some