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

Provide the ability to clear the current kubectl context without explicitly setting a new one #483

Closed
zentron opened this issue May 10, 2018 · 17 comments
Labels
area/kubectl kind/feature Categorizes issue or PR as related to a new feature. priority/P3 sig/cli Categorizes an issue or PR as relevant to SIG CLI.

Comments

@zentron
Copy link

zentron commented May 10, 2018

Feature Request

When attempting to run scripts we sometimes want to create and use a "temporary" context that can be easily cleaned up afterward. (as opposed to appending the --server etc arguments to every command since they may be externally loaded)

e.g

...
$originalContext = $(kubectl config current-context)
kubectl config set-context tempcontext --user=tempuser --cluster=tempcluster
kubectl config use-context tempcontext 
...

do some stuff
...
kubectl config use-context $originalContext
kubectl config delete-context tempcontext 
kubectl config delete-cluster tempcluster
...etc

However the delete-context result may end up with warning: this removed your active context, use "kubectl config use-context" to select a different one if there was no original context set (lets say on a fresh machine). Although the context can start as "" in the config, there is no way to just set it to "" since kubectl config use-context with no context name results in an error due to "unexpected args"

It would be nice if we could either

  1. Use the use-context command and provide a null or empty context (or some --clear arg)
  2. Or provide a kubectl config clear-context
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 8, 2018
@zentron
Copy link
Author

zentron commented Aug 8, 2018

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Aug 8, 2018
@jeremyjh
Copy link

jeremyjh commented Sep 1, 2018

@zentron do you have a workaround for this? When I'm not actively working with Kubernetes I'd like to clear my context so it doesn't take up room in my prompt.

@zentron
Copy link
Author

zentron commented Sep 3, 2018

@jeremyjh for my purposes I ended up just setting the KUBECONFIG environment variable (https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable) so that i could create a temporary context and then delete it for clean up once my operation is complete. Given this work-around I'm happy to consider this task as closed for my purposes.

@seans3
Copy link
Contributor

seans3 commented Sep 25, 2018

/kind feature
/sig cli
/area kubectl
/priority P3

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. sig/cli Categorizes an issue or PR as relevant to SIG CLI. area/kubectl priority/P3 labels Sep 25, 2018
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 25, 2018
@Bessonov
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 15, 2019
@grugnog
Copy link

grugnog commented Mar 18, 2019

Another use case for this - if you use a combination of minikube (or other "dev" clusters) and production clusters I find it helpful to set the context at the beginning of a session and explicitly clear it afterwards. This helps avoid accidentally running commands against the wrong cluster.

I have the current context on my prompt using the fantastic Go Bullet Train (GBT) to remind me to what the context is (and to clear it afterwards!).

Currently I have the following alias to do this:
alias kubeclr='sed -i"" -e"s/^current-context:.*$/current-context:/" ~/.kube/config'

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 16, 2019
@tdmalone
Copy link

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jun 16, 2019
@madAndroid
Copy link

It looks like this might already have been implemented? I was looking for a way of doing this, and was able to achieve this using:

$ kubectl config unset current-context
Property "current-context" unset.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 17, 2019
@seans3
Copy link
Contributor

seans3 commented Sep 17, 2019

/remove-lifecycle stale

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 17, 2019
@tdmalone
Copy link

tdmalone commented Sep 17, 2019

@madAndroid is correct - this has already been implemented, and thus this ticket should be allowed to close.

Is possible like this:

$ kubectl config unset current-context
Property "current-context" unset.

and therefore @grugnog's workaround is no longer needed.

@seans3
Copy link
Contributor

seans3 commented Sep 17, 2019

@tdmalone Thanks for the explanation. Closing.

/close

@k8s-ci-robot
Copy link
Contributor

@seans3: Closing this issue.

In response to this:

@tdmalone Thanks for the explanation. Closing.

/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.

zivv added a commit to zivv/conf that referenced this issue Dec 9, 2022
@safaci2000
Copy link

For those stumbling on this ticket: kubectl config unset current-context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubectl kind/feature Categorizes issue or PR as related to a new feature. priority/P3 sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
Development

No branches or pull requests

10 participants