Can I use autocompletion for kubectl in zsh?

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)

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)

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'

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.