kubernetes configmap prints \n instead of a newline

hashmim picture hashmim · Jul 11, 2018 · Viewed 7.9k times · Source

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.

Answer

VonC picture VonC · Jul 12, 2018

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.