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)
5条答案
按热度按时间2nc8po8w1#
Both
bash
andzsh
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.wz1wpwve2#
I add this function to my $HOME/.zshrc.
It will lazy load complete function of
kubectl
The oneline version:
pkwftd7m3#
对于
oh-my-zsh
,启用kubectl自动完成的最简单方法是在~/.zshrc
中添加kubectl
插件:ds97pgxw4#
I want to suggest more clean setup, for all of you that are using
Oh My Zsh
. The current accepted answer uses the commandkubectl completion zsh
and sources them to the environment. Instead of doing that we would place them in theplugins
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
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! :) )
ar7v8xwq5#
尝试在.zshrc文件的开头添加一行
然后在下面添加另一行