Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: microk8s.kubectl completion #242

Closed
mattthym opened this issue Dec 13, 2018 · 10 comments
Closed

Feature Request: microk8s.kubectl completion #242

mattthym opened this issue Dec 13, 2018 · 10 comments

Comments

@mattthym
Copy link

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

@assafmendelson
Copy link
Contributor

assafmendelson commented Dec 18, 2018

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")

@mattthym
Copy link
Author

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

@chadnickbok
Copy link

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")

@drstoehr
Copy link

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")

@amozoss
Copy link

amozoss commented Jul 10, 2019

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)

@isapir
Copy link

isapir commented Dec 4, 2020

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.

@rba
Copy link

rba commented Jan 7, 2022

For me on MacOS (11.6.2) with zsh 5.8 I configured the following in .zshrc.
I use both, kubeclt and microk8s kubectl with aliases k and mk and they have the same version atm. That's why I load the completion from kubectl and use it for microk8s kubectl as well.

The shell function was necessary to make a single command microk8s.kubectl for the subcommand microk8s kubectl.

# use autocompletion
autoload -Uz compinit
compinit

# https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-zsh/
source <(kubectl completion zsh)
alias k=kubectl
compdef k=kubectl

# for microk8s kubectl
microk8s.kubectl() {
    microk8s kubectl "$@"
}
alias mk="microk8s.kubectl"
compdef microk8s.kubectl=kubectl
compdef mk=kubectl

@fxrcode
Copy link

fxrcode commented May 15, 2022

I used https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/microk8s/microk8s.plugin.zsh, along with @rba last 2 line:

plugins=(... microk8s)
compdef microk8s.kubectl=kubectl
compdef mk=kubectl

@phreed
Copy link

phreed commented Sep 26, 2023

Is the complete command new? e.g.

alias uk="microk8s kubectl"
source <(microk8s kubectl completion bash)
complete -o default -F __start_kubectl uk

@GagaMen
Copy link

GagaMen commented Apr 4, 2025

For me this workes in zsh shell without using snap alias:

# some alias for microk8s kubectl + autocompletion
alias kubectl="microk8s.kubectl"
alias k="kubectl"
source <(kubectl completion zsh | sed "s/kubectl/microk8s.kubectl/g")
source <(k completion zsh | sed "s/k/microk8s.kubectl/g")

# some alias for microk8s helm3 + autocompletion
alias helm="microk8s.helm3"
source <(helm completion zsh | sed "s/helm/microk8s.helm3/g")

# set KUBECONFIG environment variable for k9s + helm
export KUBECONFIG=/var/snap/microk8s/current/credentials/client.config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants