kubernetes configmap prints \n instead of a newline
Solution 1
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.
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 usejq
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 onyamllint.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.
Solution 2
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
Solution 3
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
Solution 4
Removing a blank line at the end of the data:
section fixed the problem for me (I was using yaml)
Admin
Updated on July 17, 2022Comments
-
Admin almost 2 years
I am trying to deploy a configmap onto a cluster
- name: Make/Update all configmaps on the cluster kubernetes: api_endpoint: blah url_username: blah url_password: blah inline_data: apiVersion: v1 kind: ConfigMap metadata: name: blah namespace: blah data: my-data.txt: "{{ data }}" state: present data: | some = foo foo = some (using spinnaker to attach it to pods)
When I go into the pod and open my-data.txt it displays:
some = foo\n foo = some\n
I want it to look exactly like the text and print newline rather than \n
Weird thing if I put ' ' single quotes somewhere in the text it prints the text as is but with the single quotes so :
data: | some = foo foo = some ' '
prints exactly the same.
I have tried to research but I couldn't find anything and I have been stuck on this for a while now.
-
Andrew almost 3 yearsRemoving the trailing whitespace was all I needed, although I was already using spaces. The blank line at the end didn't matter for my case
-
Tara Prasad Gurung about 2 years
yamllint.com
this is magical just use it :D