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

Minikube kubectl autocompletion not working #14959

Closed
itay-grudev opened this issue Sep 15, 2022 · 8 comments · Fixed by #15850
Closed

Minikube kubectl autocompletion not working #14959

itay-grudev opened this issue Sep 15, 2022 · 8 comments · Fixed by #15850
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@itay-grudev
Copy link

itay-grudev commented Sep 15, 2022

What Happened?

As the documentation specified I tried using the kubectl provided in the project using:

sudo ln -s $(which minikube) /usr/local/bin/kubectl

I am on Ubuntu 22.04 and I tried both bash and zsh. In both cases the autocompletion doesn't work. I tried multiple things, sourcing the auto completion manually and putting them in the configuration file for each respective shell:

source <(kubectl completion zsh)
source <(kubectl completion bash)

I tried using the oh-my-zsh plugin. Nothing - it just doesn't work.

After installing a dedicated kubectl package via snapd everything works fine. There is just something wrong with the minikube setup.

Attach the log file

Not applicable.

Operating System

Ubuntu

Driver

Docker

@hypeitnow
Copy link

I can confirm

@RA489
Copy link

RA489 commented Oct 28, 2022

/kind support

@k8s-ci-robot k8s-ci-robot added the kind/support Categorizes issue or PR as a support question. label Oct 28, 2022
@fede843
Copy link

fede843 commented Jan 2, 2023

same here

@moto-ctrl
Copy link

moto-ctrl commented Feb 3, 2023

I have just spent half day trying to make it to work and failed. I'm on Debian 11 Bullseye with bash.
minikube completion works perfectly fine, built-in kubectl I cannot configure to do completion. tried both with link and with alias, user command, bashrc and system-wide file drop.
minikube version: v1.29.0

@moto-ctrl
Copy link

moto-ctrl commented Feb 3, 2023

It appears to have been reported before but never fixed
#12938
There's short code someone created to point bash directly to minikube cached version of kubectl that will do completions, but it's still a workaround as far as I can see - and not very pretty one especially 'improved' version with in-line perl

@ben-krieger
Copy link
Contributor

Using export BASH_COMP_DEBUG_FILE=$HOME/kubectl.completion and then attempting to complete kubectl ge, I see these logs:

========= starting completion logic ==========
cur is ge, words[*] is kubectl ge, #words[@] is 2, cword is 1
Truncated words[*]: kubectl ge,
lastParam ge, lastChar e
Calling kubectl __complete ge
The completion directive is: 0
The completions are: 
__kubectl_handle_completion_types: COMP_TYPE is 9

You can see that it's evaluating kubectl __complete ge in a subshell. Trying this myself, I get an error:

$ kubectl __complete ge; echo $?
Error: flags cannot be placed before plugin name: --cluster
1

So it appears that since minikube is always adding some flags, the completion script (minikube kubectl completion bash) now cannot be used with minikube kubectl either linked or aliased.

@ben-krieger
Copy link
Contributor

ben-krieger commented Feb 14, 2023

It seems like a pretty simple fix:

diff --git a/cmd/minikube/cmd/kubectl.go b/cmd/minikube/cmd/kubectl.go
index ace509340..26825b1c3 100644
--- a/cmd/minikube/cmd/kubectl.go
+++ b/cmd/minikube/cmd/kubectl.go
@@ -96,7 +96,7 @@ host. Please be aware that when using --ssh all paths will apply to the remote m
                        os.Exit(1)
                }
 
-               if len(args) > 1 && args[0] != "--help" {
+               if len(args) > 1 && args[0] != "--help" && args[0] != "__complete" {
                        cluster := []string{"--cluster", cname}
                        args = append(cluster, args...)

Before:

$ go run ./cmd/minikube kubectl __complete ge
Error: flags cannot be placed before plugin name: --cluster
exit status 1

After:

$ go run ./cmd/minikube kubectl __complete ge
get	Display one or many resources
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

I'll submit a PR.

@moto-ctrl
Copy link

great job @ben-krieger - I can see you got your PR merged! Thank you, looking forward to the new release to test it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
7 participants