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

kubectl config drops all comments from kubeconfig file #1262

Closed
rm3l opened this issue Aug 3, 2022 · 6 comments
Closed

kubectl config drops all comments from kubeconfig file #1262

rm3l opened this issue Aug 3, 2022 · 6 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@rm3l
Copy link

rm3l commented Aug 3, 2022

What happened:
I was documenting a relatively big Kubeconfig file, and just noticed that all the comments I had written disappeared after executing a kubectl config command to set the namespace of a given context.

What you expected to happen:
Not sure if that is the intended behavior (I couldn't find any existing issues related to that, nor any documentation about this), but I would expect my "carefully-crafted" comments to still be there after writing the namespace information with kubectl config ....

I've provided minimal reproduction steps below. Am I missing something?

How to reproduce it (as minimally and precisely as possible):

  • Let's say you are adding a few comments to an existing kubeconfig:
❯ mkdir -p /tmp/test-kube-config/config && cd /tmp/test-kube-config/config

❯ cat <<EOF > /tmp/test-kube-config/config
apiVersion: v1

clusters:
# My development cluster
- cluster:
    certificate-authority: fake-ca-file
    server: https://1.2.3.4
  name: development
# My experimental cluster. We don't care about TLS validation
- cluster:
    insecure-skip-tls-verify: true
    server: https://5.6.7.8
  name: scratch

contexts:
- context:
    cluster: development
    user: developer
  name: dev-frontend
- context:
    cluster: development
    user: developer
  name: dev-storage
- context:
    cluster: scratch
    user: experimenter
  name: exp-scratch
current-context: scratch

kind: Config
preferences: {}

users:
- name: developer
  user:
    client-certificate: fake-cert-file
    client-key: fake-key-seefile
- name: experimenter
  user:
    password: some-password
    username: exp
EOF
  • Versioning the file to be able to see the diff
❯ git init && git add -A . && git commit -m "add kubeconfig with comments"
  • Now set the namespace for the dev-frontend context, using kubectl config:
❯ kubectl config --kubeconfig=/tmp/test-kube-config/config set-context dev-frontend --namespace dev-fe
  • Context namespace has been correctly set, but all comments have been removed
❯ git diff
diff --git a/config b/config
index 28a4446..fe9b393 100644
--- a/config
+++ b/config
@@ -1,20 +1,17 @@
 apiVersion: v1
-
 clusters:
-# My development cluster
 - cluster:
     certificate-authority: fake-ca-file
     server: https://1.2.3.4
   name: development
-# My experimental cluster. We don't care about TLS validation
 - cluster:
     insecure-skip-tls-verify: true
     server: https://5.6.7.8
   name: scratch
-
 contexts:
 - context:
     cluster: development
+    namespace: dev-fe
     user: developer
   name: dev-frontend
 - context:
@@ -26,10 +23,8 @@ contexts:
     user: experimenter
   name: exp-scratch
 current-context: scratch
-
 kind: Config
 preferences: {}
-
 users:
 - name: developer
   user:

Anything else we need to know?:
I guess this has something to do with the way this YAML document is marshaled/unmarshaled.

Environment:

  • Kubernetes client and server versions (use kubectl version):
Client Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.3", GitCommit:"aef86a93758dc3cb2c658dd9657ab4ad4afc21cb", GitTreeState:"clean", BuildDate:"2022-07-13T14:30:46Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.4
Server Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.2", GitCommit:"f66044f4361b9f1f96f0053dd46cb7dce5e990a8", GitTreeState:"clean", BuildDate:"2022-06-23T22:20:14Z", GoVersion:"go1.18.3", Compiler:"gc", Platform:"linux/amd64"}
  • Cloud provider or hardware configuration: KinD local cluster
  • OS (e.g: cat /etc/os-release): Fedora release 36 (Thirty Six)
@rm3l rm3l added the kind/bug Categorizes issue or PR as related to a bug. label Aug 3, 2022
@k8s-ci-robot
Copy link
Contributor

@rm3l: This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 3, 2022
@brianpursley
Copy link
Member

I don't think it is intended behavior, just unfortunate behavior. This happens because it deserializes the yaml, makes the changes, and then serializes it back out.

Comments are lost during deserialization.

I don't know offhand if there is some way to possibly preserve them, maybe do some kind of patch/merge of the yaml instead of completely overwriting it?

@eddiezane
Copy link
Member

This may potentially be addressed if we move to a go-yaml v3 which supports comments. Though some work would definitely need to be done to preserve them when serializing with kubectl config.

ref kubernetes-sigs/yaml#72

I understand it's annoying to have your comments dropped but right now I would say this is a "won't fix" for us.

@rm3l
Copy link
Author

rm3l commented Aug 31, 2022

I understand. Thanks, @brianpursley, and @eddiezane for your feedback.

@ardaguclu
Copy link
Member

As discussed this is an unfortunate behavior which can not be fixed easily. I'm closing this and feel free re-open it if you think otherwise.

/close

@k8s-ci-robot
Copy link
Contributor

@ardaguclu: Closing this issue.

In response to this:

As discussed this is an unfortunate behavior which can not be fixed easily. I'm closing this and feel free re-open it if you think otherwise.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

5 participants