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

kubectl autocompletion does not work when using --context flag with a context that contains a colon #1157

Closed
frederik-b opened this issue Dec 22, 2021 · 10 comments · Fixed by kubernetes/kubernetes#107439
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@frederik-b
Copy link

What happened:

tab autocompletion does not work when explicitly declaring the context

What you expected to happen:

tab autocompletion does work when explicitly declaring the context

How to reproduce it (as minimally and precisely as possible):
does work for me:

kubectl --namespace=test get pods <tab> <tab>
kubectl --namespace test get pods <tab> <tab>

does not work for me:

kubectl --context=arn:aws:eks:eu-central-1:1234567890:cluster/foo-bar --namespace=test get pods 
kubectl --context arn:aws:eks:eu-central-1:1234567890:cluster/foo-bar --namespace=test get pods 

Anything else we need to know?:
I found: #1119
and thought it might fix it, but if I understand correctly this is included in v1.23.0 (not sure though)
and this is still not working

Environment:

  • Kubernetes client and server versions (use kubectl version): client: v1.23.0, server: 1.20.7-eks-d88609 (and others)
  • Cloud provider or hardware configuration: tested with aws, gcp, rancher2
  • OS (e.g: cat /etc/os-release): Fedora Linux 35
  • Shell: bash 5.1.8(1)-release
@frederik-b frederik-b added the kind/bug Categorizes issue or PR as related to a bug. label Dec 22, 2021
@k8s-ci-robot
Copy link
Contributor

@frederik-b: This issue is currently awaiting triage.

SIG CLI takes a lead on issue triage for this repo, but any Kubernetes member can accept issues by applying the triage/accepted label.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Dec 22, 2021
@marckhouzam
Copy link
Member

marckhouzam commented Dec 23, 2021

This is a regression with v1.22 when kubectl moved to using Go completions

The problem is with the : that are part of your context @frederik-b. The bash completion script provided by Cobra that kubectl uses does not handle the : properly in every case (spf13/cobra#1355 (comment)).

However, I believe that if you quote your context it will work. You could try something like this:

kubectl --context 'arn:aws:eks:eu-central-1:1234567890:cluster/foo-bar' --namespace=test get pods <tab> <tab>

As for a permanent fix, we would either need to fix Cobra, or kubectl would need to move to using Cobra's bash completion V2 which does not have this problem.

@marckhouzam
Copy link
Member

marckhouzam commented Dec 23, 2021

This is a regression with v1.22 when kubectl moved to using Go completions

Actually, I was wrong, the bug is present, but it is not a regression. The same problem happens with kubectl 1.21.8 which had not yet moved to Go completions.

@frederik-b
Copy link
Author

After a little bit more testing it does only work when using equal signs and not when using spaces.
I rewrote my daily use alias which now looks as follows.

alias kubectl='kubectl ${KUBECTL_CONTEXT:+--context=$KUBECTL_CONTEXT} ${KUBECTL_NAMESPACE:+--namespace=$KUBECTL_NAMESPACE}'

and today it worked even with that aws arn:aws:eks stuff with all the colons

I believe my first test case was wrong (only namespace without equal sign).
Still on 1.23.0
So it's probably fixed already?

I would like to understand why the alias only works with the = while it does otherwise work with the space syntax.

also a function apparently works the same as an alias

@eddiezane
Copy link
Member

/assign @brianpursley

@brianpursley
Copy link
Member

brianpursley commented Jan 6, 2022

As for a permanent fix, we would either need to fix Cobra, or kubectl would need to move to using Cobra's bash completion V2 which does not have this problem.

You're right about what is happening regarding cobra's completion function considering : to be a word delimiter.

I confirmed that GenBashCompletionV2 as you mentioned appears to fix the problem and completions seem to still work fine.

@marckhouzam Is there any reason you know of that it would not be a good idea to move to V2?

It should be a very simple fix.

In the meantime, anyone who needs to use a context name that has a colon can either quote it, like --context="foo:bar" or can escape the colon like --context=foo\:bar.

@brianpursley
Copy link
Member

brianpursley commented Jan 6, 2022

I would like to understand why the alias only works with the = while it does otherwise work with the space syntax.

@frederik-b Your observation is correct, that completions work when an alias contains -n=foo, but do not work when an alias contains -n foo

This is a known issue an also affects plugin invocations as well.

The cause is in this code, where it interprets the first word not starting with a - to be a command name, not a value for a flag. -n=foo works because it is considered a single word. -n foo is considered two words by this code.

https://github.com/kubernetes/kubernetes/blob/c4fbd35cf4bdc76b86dd74e468b69328f5aa20f9/staging/src/k8s.io/kubectl/pkg/cmd/cmd.go#L121-L127

I know it may not be immediately clear what this has to do with completions, but here is what is happening to cause this:

When a completion is requested for kubectl -n foo get pod <TAB>, under the hood, the bash completion script invokes:

kubectl __completeNoDesc -n foo get pod  ""

In this case, __completeNoDesc is the first non-flag word, and everything works as expected.

However if you have an alias like:

alias k=`kubectl -n foo`
complete -F __start_kubectl k

And then if you do k get pod <TAB>, under the hood, the bash completion script invokes:

k __completeNoDesc get pod  ""

But the alias expands to:

kubectl -n foo __completeNoDesc get pod  ""

Now kubectl sees foo as the first non flag and thinks it is a plugin.

In fact, if you invoke this command you can see the error that occurs, which is hidden when the completion call is made:

~ $ kubectl -n foo __completeNoDesc get pod  ""
Error: flags cannot be placed before plugin name: -n

So, while I realize this is not ideal, it is an explanation of what is happening and why you have to use = when specifying flag values inside an alias.

@marckhouzam
Copy link
Member

marckhouzam commented Jan 6, 2022

@marckhouzam Is there any reason you know of that it would not be a good idea to move to V2?

@brianpursley Bash completion v2 has a couple of benefits beyond this bug fix, but you should be aware of them to know exactly what the change implies:

  1. Support for descriptions like zsh and fish (and PowerShell). You must opt-in to this feature and could allow your users to disable it (as helm does)
  2. It only recommends --flag instead of both --flag= and --flag which is better for the user
  3. The completion script has a fixed and small size, less than 300 lines, compared to kubectl's current 14K lines script.
  4. Aligned behaviour with zsh and fish.
  5. Support for completions which includes a = or :

If you wish to enable descriptions of completion choices kubectl should first move to Cobra 1.3 to get this fix: spf13/cobra#1509

Helm has been using the bash completion v2 in the wild for 4 months (helm 3.7.0) and the only bug report was for this fix that's part of Cobra 1.3. Podman has been using it for even longer than that.

@brianpursley
Copy link
Member

/retitle kubectl autocompletion does not work when using --context flag with a context that contains a colon

@k8s-ci-robot k8s-ci-robot changed the title kubectl autocompletion stops working when using --context kubectl autocompletion does not work when using --context flag with a context that contains a colon Jan 6, 2022
@brianpursley
Copy link
Member

@marckhouzam Thanks for the info! I'll take a look at upgrading Cobra to 1.3 and turning on descriptions too. We might as well take advantage of the descriptions if we can, since it would be available with V2 completions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
5 participants