kubernetes / kubectl

Issue tracker and mirror of kubectl code
Apache License 2.0
2.82k stars 908 forks source link

kubectl plugin completion searches all of PATH, even when not needed #1398

Open rlipscombe opened 1 year ago

rlipscombe commented 1 year ago

(follow-up from #1336)

What happened:

kubectl 1.26.0 added completion for plugins. WSL2 (by default) adds the Windows %PATH% to the Linux $PATH. Since kubectl <TAB> searches $PATH for plugins (e.g. kubectl-foo), this is extremely slow.

Similarly, if the plugin exists, but doesn't provide a completion script (e.g. kubectl_complete-foo), kubectl f<TAB> is also extremely slow.

This is not that bug. That's explained (and a workaround is given) in #1336.

This issue is about when the plugin is specified (and found), and it does have a completion script. Even if the completion script is in the first entry in $PATH, kubectl searches every directory anyway.

And kubectl foo <TAB> is still extremely slow.

See the comment https://github.com/kubernetes/kubectl/issues/1336#issuecomment-1483911662, wherein:

I see that when it’s doing completion for a specific plugin, the code still looks for all plugins. That would explain why things remain slow when the WSL path contains all the windows paths.

What you expected to happen:

If I have a kubectl plugin, with a completion script, and both are early in $PATH, I expect kubectl completion to avoid searching all of $PATH and to be much quicker to respond.

How to reproduce it (as minimally and precisely as possible):

  1. Using WSL2, with defaults, observe that $PATH has a lot of /mnt/c/Program Files/Whatever entries in it.
  2. Write a simple kubectl plugin, put it somewhere early in $PATH. I had https://github.com/rlipscombe/kubectl-ssh in $HOME/bin.
  3. Observe that kubectl ssh <TAB> is really slow (it uses kubectl __complete port-forward to do its completion).
  4. Observe that kubectl __complete port-forward isn't slow.

Anything else we need to know?:

The workaround given in #1336 also addresses the problem here, which probably lowers the priority of this issue.

Environment:

eddiezane commented 1 year ago

/triage accepted /priority backlog

Jaswir commented 1 year ago

@rlipscombe @eddiezane Looking to get familiar with the code base, seems difficult but will do the job in getting me more familiar. /assign

Jaswir commented 1 year ago

@rlipscombe @eddiezane

What is <TAB> ?

I did echo $PATH on windows and noticed it was long and included a lot of entries as mentioned. Trying to figure out wat <TAB> is

rlipscombe commented 1 year ago

<TAB> is me pressing the Tab key to trigger the completion.

Jaswir commented 1 year ago

<TAB> is me pressing the Tab key to trigger the completion.

When I do it, it becomes kubectl.exe is that what you mean? Does it become kubectl.exe for you as well?

Without tab I get: image

rlipscombe commented 1 year ago

You're gonna need to install the Linux kubectl binary in WSL. I currently have kubectl v1.26.1 installed in /usr/local/bin. I suspect that I followed the instructions from here: https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

Jaswir commented 1 year ago

@rlipscombe @eddiezane

I installed Kubectl v1.26.1 and put it in /usr/local/bin

Still don't understand what <TAB> is supposed to complete to, when I press Tab I get this: 1398_TAB_

Now trying to get a Kubectl Plugin running and reproduce the bug

mpuckett159 commented 1 year ago

There needs to be a space between kubectl and the cursor otherwise the shell attempts to find the completion for kubectl, which would be the kubectl executable. When there is a space it will look for completions that are subcommands of kubectl.

Jaswir commented 1 year ago

@mpuckett159 @rlipscombe @eddiezane

I got kubectl-ssh plugin added to my $PATH and now trying to get the autocomplete working with <TAB> image

Currently it's giving me no autocompletion on tab, guess I have to run a plugin first and then it 1398_TAB_SPACE

I don't know how to get Kubectl Autocompletion doing something, help?

marckhouzam commented 1 year ago

This doc may help https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/2379-kubectl-plugins#shell-completion-support

Jaswir commented 1 year ago

This doc may help https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/2379-kubectl-plugins#shell-completion-support

@marckhouzam

Hi I read it and tried out writing a new plugin with a complete also installed bash-completion https://spacelift.io/blog/kubectl-auto-completion. But the autocomplete doesn't do what I expect it would do, help.

plugin I wrote (based on youtube vid): kubectl-colorize

#!/bin/bash

kubectl "$@" | lolcat

The autocomplete file: kubectl_complete-colorize

#!/usr/bin/env sh

exec kubectl __complete "$@" version

I expect kubectl colorize <TAB> to autocomplete to kubectl colorize version Because well $@ is empty since no parameters are given so it stays kubectl colorize and after that comes version.

Actual result, nothing happens just waits 1398_no_version_complete_question

marckhouzam commented 1 year ago

kubectl completion -h to learn how to setup shell completion first

marckhouzam commented 1 year ago

Once you have kubectl <TAB> working then you can try to get plugin completion to work.

This project provides completion scripts for half the kubectl plugins if you are interested: https://github.com/marckhouzam/kubectl-plugin_completion

Jaswir commented 1 year ago

kubectl completion -h to learn how to setup shell completion first

@marckhouzam @mpuckett159 @eddiezane @rlipscombe

I followed all the instruction in the manual page, learned source, <, <<, > , '~' and other things in the process, but Kubectl <Tab> still not working :(

1.#Installing bash completion on Linux

  1. Loaded the kubectl completion code for bash into the current shell

    • source <(kubectl completion bash)
    • kubectl completion bash > ~/.kube/completion.bash.inc

    printf "

    Kubectl shell completion

    source '$HOME/.kube/completion.bash.inc' " >> $HOME/.bash_profile

    
    -    `source $HOME/.bash_profile`

Still get this result: image

This answer is not clear, please provide more clear instructions. It would help if you send me a gif of what kubectl <TAB> does when you type it , you can use this: https://www.screentogif.com/

About to read this and see if I can get anything working with <TAB>: https://itnext.io/kubernetes-command-line-tools-acad11683794

Jaswir commented 1 year ago

@marckhouzam @mpuckett159 @eddiezane @rlipscombe

I did manage to get this working before, might be relevant to mention: learning_shell

It autocompletes kubectl-s to kubectl-ssh , is this kubectl autocompletion Bash or something else?

Jaswir commented 1 year ago

@marckhouzam @mpuckett159 @eddiezane @rlipscombe

kubectl g <TAB> autocompletes to kubectl get kubectl <TAB><TAB> autocompletes to image

kubectl <TAB> doesn't autocomplete to anything for me just get the notification sound it found nothing

brianpursley commented 1 year ago

@Jaswir are you typing a space after kubectl and before you press tab?

If not, you need to or else the shell is going to be trying to auto complete executable names matching "kubectl" which is what your image shows.

Jaswir commented 1 year ago

@brianpursley @marckhouzam @eddiezane @mpuckett159 @rlipscombe

The previous answers don't really answer my question. I am looking for a screenshot/snippet of what kubectl <TAB> results into for you. Thanks in advance

brianpursley commented 1 year ago

Ok. It seems this thread has deviated from the original issue topic.

If you are looking for help troubleshooting something, you might have better luck posting on Stack Overflow

If you think there is a bug in kubectl related to completions, then I recommend opening another issue so that it can be triaged and fixed.

Jaswir commented 1 year ago

@brianpursley @marckhouzam @eddiezane @mpuckett159

I feel like this project is not the right fit for me. I'll look for something else. /unassign

k8s-triage-robot commented 3 weeks ago

This issue has not been updated in over 1 year, and should be re-triaged.

You can:

For more details on the triage process, see https://www.kubernetes.dev/docs/guide/issue-triage/

/remove-triage accepted