I was curious if it was already somehow possible to use kubectl's completion feature in microk8s. I really like the idea of testing my services locally on a single node cluster but working with kubectl without its completion feature is quite tedious.
I tried sourcing microk8s.kubectl completion zsh/bash but the scripts appear to be the original ones of kubectl.
Any idea how the completion feature can be achieved easily or any plans to make it possible in the future?
You have two options:
alias kubectl=microk8s.kubectl
source <(microk8s.kubectl completion bash)
source <(microk8s.kubectl completion bash | sed "s/complete -o default -F __start_kubectl kubectl/complete -o default -F __start_kubectl microk8s.kubectl/g" | sed "s/complete -o default -o nospace -F __start_kubectl kubectl/complete -o default -o nospace -F __start_kubectl microk8s.kubectl/g")
Thanks for your response!
Your second suggestion worked like a charm for bash as well as for zsh.
I made it permanent by adding this to .zshrc:
if [ $commands[microk8s.kubectl] ]; then source <(microk8s.kubectl completion zsh | sed "s/complete -o default -F __start_kubectl kubectl/complete -o default -F __start_kubectl microk8s.kubectl/g" | sed "s/complete -o default -o nospace -F __start_kubectl kubectl/complete -o default -o nospace -F __start_kubectl microk8s.kubectl/g"); fi
Quick tip for folks stumbling across this like I did - if you don't want to alias kubectl (if you want to run kubectl and microk8s.kubectl side-by-side, for instance), you're going to have a bad time with that sed command.
Here's my approach:
# Kubectl completion
alias microkubectl=microk8s.kubectl
source <(microkubectl completion bash | sed "s/kubectl/microkubectl/g")
My working approach for Ubuntu with Snap and ZSH:
sudo snap alias microk8s.kubectl mksource <(mk completion zsh | sed "s/kubectl/mk/g")For me to get kubectl (with a production cluster) and microk8s to work at the same time. I had to do
sudo snap alias microk8s.kubectl mk
# added to .bashrc
source <(mk completion bash | sed "s/kubectl/mk/g" | sed "s/__custom_func/__mk_custom_func/g")
source <(kubectl completion bash)
Adding the alias and sed works for me in bash but not in zsh. What am I doing wrong? Thanks!
Update: using a snap alias like @drstoehr suggested does work.
Most helpful comment
Thanks for your response!
Your second suggestion worked like a charm for bash as well as for zsh.
I made it permanent by adding this to .zshrc:
if [ $commands[microk8s.kubectl] ]; then source <(microk8s.kubectl completion zsh | sed "s/complete -o default -F __start_kubectl kubectl/complete -o default -F __start_kubectl microk8s.kubectl/g" | sed "s/complete -o default -o nospace -F __start_kubectl kubectl/complete -o default -o nospace -F __start_kubectl microk8s.kubectl/g"); fi