22

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.

1
  • 1
    Please fix the indentation of lines in provided snippet
    – Halis
    Aug 9, 2019 at 8:16

6 Answers 6

13

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.

1
  • 1
    yamllint.com this is magical just use it :D Feb 9, 2022 at 11:30
8

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
2
  • 3
    Removing 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
    – Andrew
    May 25, 2021 at 17:03
  • 1
    If you are using VSCode, simply open the command pallet (in Mac use Cmd+Shift+P) and then run the "Format Document" command. It will cleanup all whitespaces etc in the YAML file and then you should not see character escapes anymore.
    – Rakib
    Nov 17, 2022 at 8:40
2

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
1

Removing a blank line at the end of the data: section fixed the problem for me (I was using yaml)

0

In my case, removing these two characters fixed the issue: 🐞💬

Others (like ℹ️✓⚠️✘) didn't cause a problem.

0

In my case the issue was that I was using CRLF line endings. Switching to LF line endings and re-creating the configmap with kubectl resulted in a properly formatted configmap resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.