简体   繁体   中英

How to find out why Kubectl ZSH autocompletion script fails?

I'm running on Ubuntu 18.04 and use zsh instead of bash. To get kubectl completion it should be as simple as adding

source <(kubectl completion zsh)

to the ~/.zshrc config. I have done so, but the kubectl auto-completion is not working. I have reloaded the terminal several times.

When changing the command to

source <(kubectl completion zsh) && echo success || echo failure

I see "failure" on loading the terminal. So my conclusion would be that something goes wrong with the script. The script dismisses all the output, so I don't know what's going wrong exactly.

Does anyone know what might be wrong? Or maybe where the specific script is located, so I can change

_complete kubectl 2>/dev/null

to

_complete kubectl 2>~/logs.txt

Keep in mind that the following command:

source <(kubectl completion zsh)

simply sources the output of the command :

kubectl completion zsh

which generates kubectl autocompletion script and prints it to the standard output . Instead of using it that way you may simply redirect its output to the file:

kubectl completion zsh > kubectl-autocompletion-script

change the line:

_complete kubectl 2>/dev/null

to:

_complete kubectl 2>~/logs.txt

or any other part of the script you like.

It's difficult to guess what might be wrong in your particular case but you can easily debug the script . Just for debugging purposes you may try to run it as any other shell script. If there are any errors when sourcing the script (which is basically running it in the current shell ) you'll see those errors also when you run it in a new shell which of course will not affect the environment of your currently running shell but will tell you what might be the issue.

To turn on debugging mode you need to use -x flag (or -xv for verbose output):

zsh -x kubectl-autocompletion-script

This should tell you where the problem is located.

One more thing: As alternative to adding source <(kubectl completion zsh) to your ~/.zshrc you may want to place the script in /etc/zsh/zshrc.d/ which basically do the same but globally, for all users.

Try to add one line at the start of your.zshrc file

autoload -U +X compinit && compinit

Actually this problem is a known issue ( kubernetes/kubectl#1254 ). And there's this PR kubernetes/kubernetes#112362 in progress to fix this issue for future versions of kubectl (beyond 1.25.0).

In the meantime, you can run the command below to patch it, in case you are using kubectl as a plugin from Oh My Zsh :

sed -i '6i\    sed -i '\''s/tab=$(printf \x27\\t\x27)/tab="$(printf \x27\\t\x27)"/'\'' "$ZSH_CACHE_DIR/completions/_kubectl"\' ~/.oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh && \
rm ~/.oh-my-zsh/cache/completions/_kubectl && \
exec zsh

In case you are not using Oh My Zsh , you can replace the line where you source kubectl completion by this one:

<(kubectl completion zsh | sed 's/tab=$(printf \x27\\t\x27)/tab="$(printf \x27\\t\x27)"/')

Hope this will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM