Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configmap pretty display #36222

Closed
tsn77130 opened this issue Nov 4, 2016 · 68 comments
Closed

Configmap pretty display #36222

tsn77130 opened this issue Nov 4, 2016 · 68 comments
Assignees
Labels
area/kubectl priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release.

Comments

@tsn77130
Copy link

tsn77130 commented Nov 4, 2016

Kubernetes version :

Client Version: version.Info{Major:"1", Minor:"3", GitVersion:"v1.3.6", GitCommit:"ae4550cc9c89a593bcda6678df201db1b208133b", GitTreeState:"clean", BuildDate:"2016-08-26T18:13:23Z", GoVersion:"go1.6.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"3", GitVersion:"v1.3.6+coreos.0", GitCommit:"f6f0055b8e503cbe5fb7b6f1a2ee37d0f160c1cd", GitTreeState:"clean", BuildDate:"2016-08-29T17:01:01Z", GoVersion:"go1.6.2", Compiler:"gc", Platform:"linux/amd64"}

What happened:

When I try to display configmap content with yaml output (kubectl get configmap webapp-inifile -o yaml), the output is sometimes pretty, and sometimes not -depending newlines are effectively displayed or not - just \n char-

I can't find what's the difference between some file and some others. (limit on numer of lines ?)

Configmap is created with --from-file option

Ex :

apiVersion: v1
data:
  int.ini: "###\n# app configuration\n# http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/environment.html\n###\n\n[global]\nenv
    = INT\n\n[app:main]\nuse = egg:ihm\n\npyramid.reload_templates
    = true\npyramid.debug_authorization = false\npyramid.debug_notfound = false\n
...

What you expected to happen:

More human readable display, in order to be able to easily change parameter when kubectl edit congifmap

###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/environment.html
###

[global]
env = INT

[app:main]
use = egg:ihm

pyramid.reload_templates = true
pyramid.debug_authorization = false

How to reproduce it :

kubectl create configmap webapp-inifile --from-file=/apps/webapp/int.ini
kubectl get configmap webapp-inifile -o yaml / kubectl edit configmap webapp-inifile
@pwittrock pwittrock added the priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. label Nov 17, 2016
@brendandburns brendandburns self-assigned this Nov 21, 2016
@brendandburns
Copy link
Contributor

Hrm, this seems to me like the right answer is to special case 'kubectl edit' for configmaps, rather than try to hack around with the YAML decoder.

I suppose we could use block quotes (e.g. |) if we see newlines but that seems error prone...

k8s-github-robot pushed a commit that referenced this issue Dec 9, 2016
Automatic merge from submit-queue (batch tested with PRs 38284, 38403, 38265)

Display config map data, not byte count.

Begins to address #36222

Next step is:
`kubectl edit configmap <name> --key=<file-name>`

@kubernetes/sig-cli
k8s-github-robot pushed a commit that referenced this issue Jan 23, 2017
Automatic merge from submit-queue (batch tested with PRs 38445, 40292)

Add the ability to edit fields within a config map.

Addresses part of #36222

Example command:

```console
$ kubectl edit configmap foo --config-map-data=bar
```

Will open the data element named `bar` in the `ConfigMap` named `foo` in `$EDITOR`, the edited contents are then updated back to the config map.

@kubernetes/sig-cli 

```release-note
Add a special purpose tool for editing individual fields in a ConfigMap with kubectl
```
@brendandburns
Copy link
Contributor

Closing this, pretty print is there, kubectl configMap edit support is there.

@jeremyeder
Copy link

This still happens on kube 1.7 :-(

I'm not 100% sure how to reproduce, but what I did was:

  • have an existing configmap
  • kubectl edit configmap ... and edit a few lines
  • exit the editor
  • kubectl edit configmap ... now I see the \n characters instead of actual newlines.
  • even kubectl get configmap ... -o yaml shows the \n characters

@thedch
Copy link

thedch commented Sep 26, 2017

I'm also seeing this issue in kube 1.7. During the past few days I have edited a configmap ~110 times, and seen the formatting get destroyed (spaces messed up, \n characters visible) twice. I can't determine anything specific that I did in those two edits that would have triggered the formatting messup.

@tsn77130
Copy link
Author

I'm still seeing this issue too.
Pushed a lot of identical configmaps, some ones are displaying good, and some other bad.

For example, I have a configmap with 3 files in ; they all are identical except one variable ; 2 of them display normally, and the last one displays with \n ...

@rileytg
Copy link

rileytg commented Oct 18, 2017

also seeing this issue on 1.7

@rileytg
Copy link

rileytg commented Oct 18, 2017

can we reopen this issue? or open a new one?

@transient1
Copy link

I created a brand new configmap using
kubectl create configmap my-map --from-file file1 --from-file file2

and when I viewed the yaml with kubectl get configmap my-map -oyaml it's full of \n characters and escape characters. This makes it very difficult to edit.

@Haybu
Copy link

Haybu commented Mar 12, 2018

I am experiencing the same. \n character is messing up a configmap I'm creating with --from-file option. Anyone found a reason why this is happening ?

@lpzdvd
Copy link

lpzdvd commented Mar 22, 2018

Also failing in 1.8.4

@tutaidalal
Copy link

I am also facing the same issue of formatting .. I have a application configuration of 400-500 lines. Tryied many times. For fist 40-50 lines the format is good ans easily readable.. However, when i try to create config for entire configuration file its very difficult to edit the config map..

@huguesalary
Copy link

Same issue here. No idea what makes one configmap show better than another.

@arafatmohammed
Copy link

arafatmohammed commented Sep 14, 2018

This issue corrupted my aws-auth configMap that authenticates my user to access the EKS cluster and while trying to add other users to the configMap the formatting corrupted the configMap file and now I am locked out of my EKS cluster.

Version: 1.10.3

@lleontop
Copy link

I don't know if this will help somebody else but I was facing the same issue with escaped data in a config map edit and get (the config map had a single .conf file), and it seems that I had a unneeded extra space at the end of a single line in my data. When I removed that extra space the pretty printing was maintained in edit and get.

@pastukhov
Copy link

1.11
Still same

@vitalyevodin
Copy link

still same ( there is an example if install helm install stable/prometheus , you will find cm/prometheus-server )

@jimmythedog
Copy link

I agree with @lleontop - I can recreate it every time
If you were to execute the following (ensuring no trailing spaces), the configmap remains "pretty"

cat <<-EOF | kubectl apply -f -
apiVersion: v1
data:
  test.properties: |-
    abc=def
    xyz=123
kind: ConfigMap
metadata:
  name: test-for-formatting
EOF

However, repeat the above (or edit the existing one with kubectl), and put a trailing space after one of the key/value pairs, and that then messes up the configmap contents

@dotw
Copy link

dotw commented Oct 12, 2018

Can we reopen this issue? I need this feature to change the yaml in cm, and I have been struggling with the intent and \n for several days ...

@86label
Copy link

86label commented Jul 30, 2020

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

@akunaatrium

@brylor
Copy link

brylor commented Aug 27, 2020

Edited a cm, came back in, and had \n all over.

Something about white space in my previous edit?

Not great overall :/

On

kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-07-04T15:01:15Z", GoVersion:"go1.14.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.6", GitCommit:"7015f71e75f670eb9e7ebd4b5749639d42e20079", GitTreeState:"clean", BuildDate:"2019-11-13T11:11:50Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}

@alexjuchina
Copy link

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

Awesome, very good website~

@sriharshakx
Copy link

I agree with @lleontop - I can recreate it every time
If you were to execute the following (ensuring no trailing spaces), the configmap remains "pretty"

cat <<-EOF | kubectl apply -f -
apiVersion: v1
data:
  test.properties: |-
    abc=def
    xyz=123
kind: ConfigMap
metadata:
  name: test-for-formatting
EOF

However, repeat the above (or edit the existing one with kubectl), and put a trailing space after one of the key/value pairs, and that then messes up the configmap contents

Thank You very much

@stepanselyuk
Copy link

stepanselyuk commented Nov 17, 2020

I was able to fix this behavior by:

  1. Don't use tabs, convert to spaces
  2. To remove spaces before a newline character, use this:
 sed -i -E 's/([[:space:]]|\r)+$//g' File.ext

It will also convert CRLF to LF only, by removing \r.

@wcarss
Copy link

wcarss commented Feb 9, 2021

Something small that exacerbates this issue is that in python's standard lib module json, up to some version (I think python 3.4, see https://bugs.python.org/issue16333), pretty printing adds a trailing space after every comma by default, unless you supply a list of separators yourself that omit the spaces (e.g. like json.dumps(data, True, indent=4, separators=(",",":"))

So if you have old-ish glue (e.g. python 2.7) that parses a k8s configmap into a file for archival purposes, and you made that file readable by pretty-printing it, using that file to regenerate a new configmap results in this gross escape behaviour.

reimannf added a commit to sapcc/helm-charts that referenced this issue Mar 3, 2021
Based on this: kubernetes/kubernetes#36222
Spaces before Newlines causes issues rendering confgMaps.
Very anoying when `kubectl edit` is involved
natw added a commit to natw/nats-k8s that referenced this issue Aug 17, 2021
Depending on your configuration, the configmap can be rendered with
lines having trailing whitespace. This causes k8s to display the
configmap as just a big string containing literal "\n" strings, rather
than actual linebreaks.

This is most notably a problem when using `kubectl edit`, as it is
somewhat more difficult to edit the config when it's all on one line.

kubernetes/kubernetes#36222 has a whole bunch
of discussion on the issue, in case you wanted to read about a bunch of
people yelling past each other.

Regardless of whether it's a bug or not, seems best to just avoid it in
the first place.
wallyqs pushed a commit to nats-io/k8s that referenced this issue Aug 17, 2021
Depending on your configuration, the configmap can be rendered with
lines having trailing whitespace. This causes k8s to display the
configmap as just a big string containing literal "\n" strings, rather
than actual linebreaks.

This is most notably a problem when using `kubectl edit`, as it is
somewhat more difficult to edit the config when it's all on one line.

kubernetes/kubernetes#36222 has a whole bunch
of discussion on the issue, in case you wanted to read about a bunch of
people yelling past each other.

Regardless of whether it's a bug or not, seems best to just avoid it in
the first place.
@somaninga-pujari
Copy link

Hello There,
One of our colleague has edited configmap with wrong data, now whole cluster inaccessible with any of role, need help on immediate basis and much appreciated.
Error:
through eksctl - Error: getting auth ConfigMap: Unauthorized
through kubectl error: You must be logged in to the server (Unauthorized)

@saiskee
Copy link

saiskee commented Mar 8, 2022

yaml marshallers will write a string to a multiline string e.g.

multiline_string: |
  some string!
  with new lines and \ no need to escape things

UNLESS

  1. The string contains a \t character
  2. The string terminates in a space character
  3. The string contains a substring which matches the regex \n[ ]+\n

@YamilDiaz
Copy link

YamilDiaz commented Apr 20, 2022

Hello There, One of our colleague has edited configmap with wrong data, now whole cluster inaccessible with any of role, need help on immediate basis and much appreciated. Error: through eksctl - Error: getting auth ConfigMap: Unauthorized through kubectl error: You must be logged in to the server (Unauthorized)

I am in the same situation. Were you able to solve this?

@CC11001100
Copy link

Also reproduce in v1.22.3 :(

@Auggie321
Copy link

I occasionally encountered this problem in v1.15.6 or v1.21.2,
but eventually I was able to deal with it by using edit and then using vim's own set list to back-propagate
the original static configmap code configuration under gitlab to see
if there were extra spaces, and then modifying the local configmap file based on the online configmap's set list display under edit.

The state of the online configmap under edit, to modify the local configmap file, is ultimately able to deal with this problem,
but if there is a better official way to do it would be good.

@MartinKosicky
Copy link

ession was perfect. Once you know this is your doing -- and you know how to fix it -- you're not bothered by this behaviour anymore.

yes if you are working on linux

@samermassoud
Copy link

i would like to share an observation that helped me sort out the line merging and \n.
it appears to be related to the last line of the file you are using (--from-file=file.conf)
if the last line ends with empty space or tab. the config map is merged up in one line with \n as line feed
if you remove the empty space from the last line, the ConfigMap will import file as is with |- or |+

@piyumisudusinghe
Copy link

piyumisudusinghe commented Jul 26, 2022

I got the same error after editing the ConfigMap file. I used an online tool to remove the trailing white space that we cannot see in the text. After copying the content which removed trailing whitespaces I was able to resolve this issue

FYI - https://miniwebtool.com/remove-leading-trailing-spaces/ (online tool which I used)

dlipovetsky added a commit to dlipovetsky/calico that referenced this issue Aug 23, 2022
When this trailing whitespace is present, and the Tigera Operator
manifest is embedded as YAML within a Kubernetes ConfigMap or Secret,
all whitespace in the embedded YAML is escaped. This is discussed in
kubernetes/kubernetes#36222.
@jainpratik163
Copy link

in v1.24 also i can see same issue.
how we can edit cm with many lines.

@chaoluo-git
Copy link

chaoluo-git commented Nov 9, 2023

Finally, I found that the absence of a value defined or the value is empty string in valus.yaml will cause the \n issue.

data: {{ .Values.application.data}}

application.data is not defined in my yaml

just use quote to the value resolved

data: "{{ .Values.application.data}}"

@GentleSong
Copy link

delete space before \n or after \n
it works for me

@jainpratik163
Copy link

did anyone find solution when we edit cm how we can see in proper format as for me really difficult to edit cm as we have many \n char and I can still see this issue in k8s version 1.28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release.
Projects
None yet
Development

No branches or pull requests