kubernetes: Kubectl bash completion doesn't properly handle namespace flag

The kubectl bash completion doesn’t take into account the namespace flag, and instead completes as if the flag wasn’t passed.

Without namespace works correctly:

$ kubectl get service k
kibana      kubernetes

With namespace, ignores it and completes as in the first case:

$ kubectl get service --namespace=kube-system k
kibana      kubernetes

It should instead complete using the services I have in that namespace:

$ kubectl get service --namespace=kube-system
NAME                   CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
kube-dns               10.0.0.10    <none>        53/UDP,53/TCP   12d
kubernetes-dashboard   10.0.0.36    nodes         80/TCP          12d

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (5 by maintainers)

Commits related to this issue

Most upvoted comments

The above was with 1.2.x kubectl and completions. I upgraded to 1.3.0 and now it errors out on completions with namespace:

$ kubectl get service --namespace=kube-system k-bash: --namespace=: attempted assignment to non-variable (error token is "=")

@superbrothers Thanks, I can confirm 1.9.0-alpha.2 of kubectl works fine 👍

I might be wrong, but this issue is still valid when using the shorthand flag -n, such as:

kubectl -n <namespace> get <tab tab>

returns pod suggestions from the default namespace

I can confirm that the issue I reported is fixed in the release version 1.8.1. Thanks a lot!

The above was with 1.2.x kubectl and completions. I upgraded to 1.3.0 and now it errors out on completions with namespace:

$ kubectl get service --namespace=kube-system k-bash: --namespace=: attempted assignment to non-variable (error token is "=")

https://github.com/kubernetes/kubernetes/issues/29322#issuecomment-234102427

@yissachar The above error does not occurs on v1.8.

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.3.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ . /usr/local/etc/bash_completion
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.1", GitCommit:"f38e43b221d08850172a9a4ea785a86a3ffa3b3a", GitTreeState:"clean", BuildDate:"2017-10-11T23:27:35Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.5", GitCommit:"17d7182a7ccbb167074be7a87f0a68bd00d58d97", GitTreeState:"clean", BuildDate:"2017-10-06T20:53:14Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
$ source <(kubectl completion bash)

$ kubectl get service --namespace=kube-system kube<tab>
kube-dns              kubernetes-dashboard

This is true of the zsh completions (on 1.3.x) as well.

$ kubectl --namespace=staging get pod bal<TAB>
__handle_flag:20: bad math expression: operand expected at end of string
__handle_flag:20: bad math expression: operand expected at end of string
__handle_flag:20: bad math expression: operand expected at end of string

https://github.com/kubernetes/kubernetes/issues/29322#issuecomment-236282692

I’m seeing this in zsh 5.2, as well. This input:

kubectl get service --namespace=kube-system <TAB>

results in this error being printed to my shell:

__handle_flag:25: bad math expression: operand expected at end of string

https://github.com/kubernetes/kubernetes/issues/29322#issuecomment-283444545

@philk @n-rook The above error has been fixed by https://github.com/kubernetes/kubernetes/pull/48553.

$ zsh --version
zsh 5.3.1 (x86_64-apple-darwin16.6.0)
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.1", GitCommit:"f38e43b221d08850172a9a4ea785a86a3ffa3b3a", GitTreeState:"clean", BuildDate:"2017-10-11T23:27:35Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.5", GitCommit:"17d7182a7ccbb167074be7a87f0a68bd00d58d97", GitTreeState:"clean", BuildDate:"2017-10-06T20:53:14Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"linux/amd64"}
$ source <(kubectl completion zsh)

$ kubectl --namespace=kube-system get po kube<tab>
kube-addon-manager-minikube  kube-dns-1326421443-16lh9    kubernetes-dashboard-hfxln

Also seems to not work as you’d expect with 1.4 … --namespace <ns> works, but with =, and -n <ns> does not.

Only tested with zsh.

https://github.com/kubernetes/kubernetes/issues/29322#issuecomment-249818999

@davidreuss The above error will be fixed by https://github.com/kubernetes/kubernetes/pull/54406.

$ kubectl -n kube-system get po kube<tab>
kube-addon-manager-minikube  kube-dns-1326421443-16lh9    kubernetes-dashboard-hfxln

For me (with zsh):

kubectl create -f <TAB>

gives:

__handle_flag:29: bad math expression: operand expected at '/Users/joh...'

https://github.com/kubernetes/kubernetes/issues/29322#issuecomment-288563190

@lqueryvg I could not reproduce the above error on v1.8. Could you give me more information?

FYI, I have my own rather horrible workaround which is as follows:

Instead of: source <(kubectl completion zsh) I run: source <(kubectl completion zsh | sed -e '/flaghash.*true/s/# pad/2>\/dev\/null # pad/')

Basically, this line: flaghash[${flagname}]="true" # pad "true" for bool flag Becomes: flaghash[${flagname}]="true" 2>/dev/null # pad "true" for bool flag

The actual completion behaviour still seems to work correctly, so I’m just throwing away the annoying message.

Needs a proper solution though.