Kubectl: Provide the ability to clear the current kubectl context without explicitly setting a new one

Created on 10 May 2018  路  16Comments  路  Source: kubernetes/kubectl

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
arekubectl kinfeature prioritP3 sicli

Most helpful comment

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.

All 16 comments

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

/remove-lifecycle stale

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

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

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

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

/remove-lifecycle stale

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'

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

/remove-lifecycle stale

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.

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

/remove-lifecycle stale

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

@tdmalone Thanks for the explanation. Closing.

/close

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

Was this page helpful?
0 / 5 - 0 ratings