Can I use autocompletion for kubectl in zsh?
Asked Answered
L

5

44

I daily find myself doing...

$ kubectl --context=foo get pods
  < copy text manually >
$ kubectl --context=foo logs dep1-12345678-10101

I would like to cycle through matching resources with

$ kubectl --context=foo logs dep1<TAB>

but this doesn't seem to do anything with my stock setup. Any ideas?

osx 10.12.3 kubectl v1.4.5 zsh zsh 5.2 (x86_64-apple-darwin16.0)

Liegeman answered 21/2, 2017 at 0:58 Comment(0)
V
91

Both bash and zsh supports scripts that completes printed command when you press <TAB>. The feature is called Programmable completion, and you can find more details about that here: zsh completion.

Fortunately, you don't need to write your own script - kubectl provides it for zsh > 5.2. Try running this command: source <(kubectl completion zsh).

Another option is to use this tool: https://github.com/mkokho/kubemrr (disclaimer: I'm the author). The reason it exists is because standard completion script is too slow - it might take seconds before Kubernetes cluster replies will all pod names. But kubemrr keeps the names locally, so the response comes back almost immediately.

Vernalize answered 21/2, 2017 at 9:39 Comment(3)
To automatically load in future shells, you can do echo "source <(kubectl completion zsh)" >> ~/.zshrcStertor
This is amazing. Spent 30 minutes trying to get bash completion working in Centos until I found this.Tropopause
If you're running this on OSX and getting command not found: compdef, check this answer: unix.stackexchange.com/a/477909/12737Viquelia
C
19

For oh-my-zsh, the easiest way to enable kubectl auto-completion is to add kubectl plugin in ~/.zshrc:

# somewhere in your .zshrc
# kubectl: The kubectl completion script for Zsh can be generated with the command kubectl completion zsh. Sourcing the completion script in your shell enables kubectl autocompletion.
# kube-ps1: A script that lets you add the current Kubernetes context and namespace configured on kubectl to your Bash/Zsh prompt strings
plugins=(git kubectl Kube-ps1)
Chumash answered 16/9, 2020 at 6:25 Comment(2)
I had to add kube-ps1 to the list to get it to work plugins=(git kubectl kube-ps1) + another line source <(kubectl completion zsh)Hogle
I didn't need kube-ps1, but I did need plugins=(git kubectl) and source <(kubectl completion zsh).Jeanniejeannine
D
15

I add this function to my $HOME/.zshrc.

It will lazy load complete function of kubectl

kubectl () {
    command kubectl $*
    if [[ -z $KUBECTL_COMPLETE ]]
    then
        source <(command kubectl completion zsh)
        KUBECTL_COMPLETE=1 
    fi
}

The oneline version:

(( ${+commands[kubectl]} )) && alias kubectl='test -z $C_KUBE && C_KUBE=1 && source <(command kubectl completion zsh); command kubectl'
Dunant answered 11/1, 2018 at 8:47 Comment(3)
(( ${+commands[kubectl]} )) && alias kubectl='test -z $C_KUBE && C_KUBE=1 && source <(command kubectl completion zsh); command kubectl'Dunant
can you please explain what does the command do which you left in the previous comment?Trenatrenail
@Trenatrenail 1. if the executable binary file kubectl exist, make the alias; 2. check if the environment variable C_KUBE exist. if not exist, source completion script and set the environment variable; 3. let the aliased command 'kubectl' works like the real command 'kubectl'Dunant
I
8

I want to suggest more clean setup, for all of you that are using Oh My Zsh. The current accepted answer uses the command kubectl completion zsh and sources them to the environment. Instead of doing that we would place them in the plugins folder, using the same command but modifying it to:

kubectl completion zsh > ~/.oh-my-zsh/custom/plugins/kubectl.plugin.zsh

Now we have to define the plugins we want to load on ZSH start inside of our ~/.zshrc config

...

plugins=(
  git
  ...
  kubectl
)

...

This solution has the benefit of keeping your config clean. (Yes I know it is one line, but if you have to do this for 10 plugins it adds up! :) )

Indigestion answered 14/7, 2022 at 7:27 Comment(0)
C
7

Try to add one line at the begining of you .zshrc file

autoload -U +X compinit && compinit

then add another line below

source <(kubectl completion zsh)
Cavetto answered 28/10, 2021 at 16:36 Comment(1)
this is what helped meIronbark

© 2022 - 2024 — McMap. All rights reserved.