Microk8s: Feature Request: microk8s.kubectl completion

Created on 13 Dec 2018  路  6Comments  路  Source: ubuntu/microk8s

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?

inspection-report-20181213_113531.tar.gz

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

All 6 comments

You have two options:

  1. change the command to be kubectl and use the default kubectl completion:
    alias kubectl=microk8s.kubectl source <(microk8s.kubectl completion bash)
  2. change the kubectl completion using sed:
    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:

  1. Add alias statically: sudo snap alias microk8s.kubectl mk
  2. Add to .zshrc: source <(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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xrpn picture xrpn  路  4Comments

cnadeau picture cnadeau  路  4Comments

devbharat picture devbharat  路  3Comments

carmine picture carmine  路  4Comments

singram picture singram  路  4Comments