kubernetes: kubectl config current-context is slow

What would you like to be added: If possible, it would be great to have kubectl config current-context performance be improved.

Why is this needed: I sometimes do commands thinking my context is one thing, but end up modifying a cluster I didn’t intend to. To help avoid this, I changed my shell prompt to print out my kubectl context. But since the command is slow, every command I run in my shell causes a few hundred ms delay before I see the prompt again.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Hopefully this helps anyone still running into this: I managed to speed it up by deleting old entries (clusters, contexts, and users) in ~/.kube/config. I had a bunch of configs for old AWS clusters I was no longer using, as well as a local kind cluster. I removed everything except GKE clusters which are currently up and running, and now my kubectl commands are very fast again.

Maybe the CLI was trying to communicate with these defunct clusters and failing after some timeout? If that is the case, seems like a bug. It seemed to be happening on every command – even my kubectl completion bash command was greatly slowed down, causing my shell startup to take 5 seconds or so.


Side note, if you want an alternative to kubectl config current-context that is fast enough to be used for a shell prompt, try this:

awk '/^current-context:/{print $2;exit;}' <~/.kube/config

This command runs in about 2ms, vs kubectl config get-context which takes 32ms on my setup.

I’ve just somehow gotten the same issue(?). Not sure what changed actually, haven’t added any new clusters or changed config.

༶ kubectl version                                                                                                                                                                                                     
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.9", GitCommit:"a17149e1a189050796ced469dbd78d380f2ed5ef", GitTreeState:"clean", BuildDate:"2020-04-16T11:44:51Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"darwin/amd64"}

A few runs of:

༶ time kubectl config current-context                                                                                                                                                                                 
xxx
kubectl config current-context  0.09s user 0.02s system 18% cpu 0.620 total
༶ time kubectl config current-context                                                                                                                                                                                 
xxx
kubectl config current-context  0.10s user 0.02s system 18% cpu 0.629 total
༶ time kubectl config current-context                                                                                                                                                                                 
xxx
kubectl config current-context  0.09s user 0.02s system 18% cpu 0.634 total

I’m also using oh-my-zsh kubectl plugin which leverages the command to show current context and thats what got me digging as shell got so much slower than usual

EDIT:

figured out my local issue. When using the gcloud-sdk provided kubectl it is indeed slow. When using the docker-engine provided its back to normal:

Client Version: version.Info{Major:"1", Minor:"16+", GitVersion:"v1.16.6-beta.0", GitCommit:"e7f962ba86f4ce7033828210ca3556393c377bcc", GitTreeState:"clean", BuildDate:"2020-01-15T08:26:26Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"darwin/amd64"}
༶ time /Applications/Docker.app/Contents/Resources/bin/kubectl config current-context                                                                                                                               
/Applications/Docker.app/Contents/Resources/bin/kubectl config current-contex  0.05s user 0.01s system 136% cpu 0.040 total