Skip to content

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

@zentron

Description

@zentron

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/kubectlkind/featureCategorizes issue or PR as related to a new feature.priority/P3sig/cliCategorizes an issue or PR as relevant to SIG CLI.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions