Bug 1405961 - Completion has some problems with multiple flags
Summary: Completion has some problems with multiple flags
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: OpenShift Container Platform
Classification: Red Hat
Component: oc
Version: 3.4.0
Hardware: Unspecified
OS: Unspecified
low
low
Target Milestone: ---
: 3.9.0
Assignee: Maciej Szulik
QA Contact: zhou ying
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-12-19 10:16 UTC by Xingxing Xia
Modified: 2019-04-09 14:20 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: No Doc Update
Doc Text:
Clone Of:
Environment:
Last Closed: 2019-04-09 14:20:17 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Product Errata RHBA-2019:0619 0 None None None 2019-04-09 14:20:25 UTC

Description Xingxing Xia 2016-12-19 10:16:35 UTC
Description of problem:
Both under bash(MacOS) and zsh(no matter Fedora or MacOS), completion has some problems with multiple flags

Version-Release number of selected component (if applicable):
oc v3.4.0.37

How reproducible:
Always

Steps to Reproduce:
1. In MacOS bash shell, given bash completion package is installed [*1], try:
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
...

$ bash

$ source $(brew --prefix)/etc/bash_completion

$ oc completion bash > out_bash.sh

$ source out_bash.sh

Then try completion when typing multiple flags (note: '=' or space after first flag --namespace will have DIFFERENT errors):
1> oc get pod --namespace=xxia-proj --select<Press Tab>
2> oc get pod --namespace  xxia-proj --select<Press Tab>
3> oc get pod --request-timeout=10s   --namesp<Press Tab>

[*1] in MacOS bash, install bash completion package by:
$ sudo brew install bash-completion

2. In zsh(no matter Fedora or MacOS):
[tester@pc_f25 oc]$ zsh --version
zsh 5.2 (x86_64-redhat-linux-gnu)

[tester@pc_f25 oc]$ zsh

[tester@dhcp-128-14]~/oc% oc completion zsh > out_zsh.sh

[tester@dhcp-128-14]~/oc% source out_zsh.sh

Then try completion when typing multiple flags:
1> [tester@dhcp-128-14]~/oc% oc get pod --namespace  xxia-proj --select<Press Tab>

2> [tester@dhcp-128-14]~/oc% oc get pod --namespace=xxia-proj --select<Press Tab>



Actual results:
1. Each sub step failed to press Tab to complete the additional flag, and error message follows each command line:
1> oc get pod --namespace=xxia-proj --select-bash: --namespace=: attempted assignment to non-variable (error token is "=")
2> oc get pod --namespace  xxia-proj --select-bash: flaghash[${flagname}]: bad array subscript
3> oc get pod --request-timeout=10s   --select-bash: --request-timeout=: syntax error: operand expected (error token is "=")

2. Also '=' or space after the first flag --namespace will have DIFFERENT results:
1> It can complete additional flag --select(or)
2> Failed to press Tab to complete the additional flag, error message follows command line:
oc get pod --namespace=xxia-proj --select__handle_flag:25: bad math expression: operand expected at end of string

Expected results:
1. In MacOS bash, each sub step should successfully complete as:
oc get pod --namespace ... --selector=

2. No matter '--namespace=xxia-proj' or '--namespace xxia-proj', completion for the additional flag --select(or) should work.

As comparsion, in Fedora bash, no matter previous flags are followed by '=' or spaces, following flags can always complete by Tab:
$ oc get pod --namespace=xxia-proj --config ~/.kube/config --select<press Tab>     will complete as:
$ oc get pod --namespace=xxia-proj --config ~/.kube/config --selector=

Additional info:

Comment 1 Juan Vallejo 2016-12-19 22:16:52 UTC
Related upstream PR: https://github.com/kubernetes/kubernetes/issues/29322

Comment 2 Juan Vallejo 2016-12-19 22:20:05 UTC
Could you verify that this bug also exists in bash -version >3 on mac?

Comment 3 Xingxing Xia 2016-12-20 06:31:11 UTC
(In reply to Juan Vallejo from comment #2)
> Could you verify that this bug also exists in bash -version >3 on mac?

In MacOS, after update bash, all 3 examples of step 1 ARE NOT PROBLEM.
They can complete multiple flags successfully, no matter '=' or space is used between flag and flag value, no matter flag is long format '--flagname' or short format '-f':
dhcp-140-31:~ xxia$ bash --version
GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin16.1.0)
...

dhcp-140-31:~ xxia$ bash

bash-4.3$ oc get pod --namespace=xxia-proj --se<Tab>
--selector=  --server=    
bash-4.3$ oc get pod --namespace=xxia-proj --selector name -<Tab>
--all-namespaces            --insecure-skip-tls-verify
...
--template=                 -o
...

bash-4.3$ oc get pod --namespace=xxia-proj --selector name -o yaml

Comment 4 Xingxing Xia 2016-12-20 06:54:23 UTC
(In reply to Juan Vallejo from comment #1)
> Related upstream PR: https://github.com/kubernetes/kubernetes/issues/29322

This issue in fact includes 2 aspects:

One is to complete resources in the specified --namespace, as said in the issue's 1st comment: "With namespace, ignores it and completes as in the first case". I tried even in Fedora bash shell:

[tester@pc_f25 ~]$ oc get project
NAME            DISPLAY NAME   STATUS
xxia-pipeline                  Active
xxia-proj                      Active

[tester@pc_f25 ~]$ oc project 
Using project "xxia-pipeline" on server "...".

[tester@pc_f25 ~]$ oc get pod
NAME                             READY     STATUS      RESTARTS   AGE
jenkins-1-oj83m                  1/1       Running     1          21h
...

[tester@pc_f25 ~]$ oc get pod --namespace=xxia-proj
NAME                        READY     STATUS      RESTARTS   AGE
database-5-b7q37            1/1       Running     1          22h
...

[tester@pc_f25 ~]$ oc --namespace=xxia-xxia-proj get pod database<Tab>      Just complete local files
database_hooks_US.yaml  database_rolling.yaml   database.yaml           
         
[tester@pc_f25 ~]$ oc --namespace=xxia-pipeline get pod jenkins<Tab>   Can complete jenkins-1-oj83m

That is:
when --namespace is given current context project, it can complete resources, as --namespace is not given
when --namespace is given other project, it just complete local files, not resources! Not sure if this would be improved.



The other aspect is zsh completion if completing flags or resources when previous flag uses '=' before flag value. It is not limited to flag --namespace:

dhcp-140-31% oc get pod -o yaml --se<Tab>      Can prompt completion --selector  --server

dhcp-140-31% oc get pod -o yaml --selector=name --sh<Tab>     like comment 0 step 2.1:

dhcp-140-31% oc get pod -o yaml --selector=name --sh__handle_flag:25: bad math expression: operand expected at end of string

Comment 6 Juan Vallejo 2017-01-23 23:23:22 UTC
Related PR: https://github.com/openshift/origin/pull/12630

Linked PR adds support for `--namespace` flag tab completion. Instead of suggesting local file names, it will now suggest all available namespaces, and any resources available in the chosen namespace.

The other issue specified in https://bugzilla.redhat.com/show_bug.cgi?id=1405961#c0 (bad math expression error when adding a "=" to a flag) is not likely to be addressable as it seems to be an issue with zsh and older versions of bash. The same problem occurs in kubectl.

Also, although the `--selector` flag was used in the examples above, we are not able to suggest any values for it at the moment in bash completion, as there is no clean way of obtaining all available labels (A way of doing this would be to iterate in the bash script through all resources in the namespace and list label keys for each one, but this will not guarantee duplicates, and seems rather expensive).

Comment 7 Juan Vallejo 2017-01-30 14:44:29 UTC
[PR](https://github.com/openshift/origin/pull/12630) linked in [previous comment](https://bugzilla.redhat.com/show_bug.cgi?id=1405961#c6) has merged. Adds support for the `--namespace` flag in both bash and zsh.

Comment 8 XiaochuanWang 2017-02-03 06:18:59 UTC
OC 3.5.0.14 reproduced by the original steps "Steps to Reproduce"

1. For MAC bash 4.3.42(1)
$ oc get pod --namespace<tab> will print:
$ oc get pod --namespace=--namespace=   then add one more <tab> will output local files

multiple flags (eg. --namespace=xiaocwan-t --select<tab>) also reproduced the same:
$ oc get pod --namespace=xiaocwan-t --selector=--selector=<tab>  =>will print the local files

So `--namespace` flag tab completion is good, but seems namespaces are not suggested instead of local files. 

2. For zsh 5.1.1 
Full flag name can not be suggested when input "--namesp" or "--select" and there are not any local files output.

3. For compare, fedora23 will only output the suggest flag "--namespace=" and "--selector=" one time and will output local files when TAB more

There are not errors like "Actual results" anyway.

Comment 9 Juan Vallejo 2017-02-03 15:43:39 UTC
Are you testing on latest master?

> 1. For MAC bash 4.3.42(1)
$ oc get pod --namespace<tab> will print:
$ oc get pod --namespace=--namespace=   then add one more <tab> will output local files

I tested on bash 4.3.42 on Fedora 23, and a tab after both "--namespace" and "--namespace=" work as expected, you seem to have two "--namespace" in your command, was this intended?

> multiple flags (eg. --namespace=xiaocwan-t --select<tab>) also reproduced the same:

Only the "--namespace" flag was updated for autocompletions. The "--selector" flag is trickier since there is no clean way of getting a list of all selectors being used in a namespace.

> 2. For zsh 5.1.1 
Full flag name can not be suggested when input "--namesp" or "--select" and there are not any local files output.

Tested this on a mac with both zsh and bash, and was able to confirm that completions for the --namespace flag suggest local files instead. Will look into this, thanks

Comment 10 Juan Vallejo 2017-02-03 20:28:03 UTC
From #c8
> 3. For compare, fedora23 will only output the suggest flag "--namespace=" and "--selector=" one time and will output local files when TAB more

After testing on fedora 23, `--names<TAB>` is able to autocomplete to `--namespace `. Doing <TAB> again, suggests all of the namespace that I have access to. Selecting a suggested namespace and pressing <TAB> (after specifying the resource I want) suggests resources of the specified type for the that namespace. 
A few flags, such as `--selector` and `--output` will not be fixed in the linked PR as there is currently no clear way of getting the information to suggest (for example, the reason why the --namespace flag is able to work, is because the bash completion function performs an "oc get namespace" cmd in the background and presents the output of that as suggestions when <TAB> is pressed).

Lowering the severity of this for now, the fix for mac should come from upstream as the `oc completion` command is just a wrapper for the upstream command, and all of the the bash completion functions being used in the linked PR [1] are entirely taken from upstream's bash completion functions [2]

[1] https://github.com/openshift/origin/pull/12630
[2] https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/cmd.go#L37

Related upstream issues:

https://github.com/kubernetes/kubernetes/issues/32676
https://github.com/kubernetes/kubernetes/issues/29322

Comment 12 Maciej Szulik 2019-02-26 15:35:38 UTC
This is working properly since 3.9. Moving to qa.

Comment 13 zhou ying 2019-02-28 08:54:36 UTC
Confirmed with ocp 3.9 version on Macos:

[openshift@Mac-Slave-1 39]$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)

bash-4.4$ ./oc version
oc v3.9.71
kubernetes v1.9.1+a0ce1bc657
features: Basic-Auth

oc get pod -o yaml --se<Tab>      Can prompt completion --selector  --server


bash-4.4$ oc get pod
NAME              READY   STATUS      RESTARTS   AGE
ruby-ex-1-build   0/1     Completed   0          9m44s
ruby-ex-1-j2645   1/1     Running     0          8m55s

./oc --namespace=zhouy get pod ruby-<Tab>  can show the pod names: ruby-ex-1-build, ruby-ex-1-j2645


With ocp 3.9 latest version on fedora:
[bob@fedora29 3.9]$ zsh --version
zsh 5.6.2 (x86_64-redhat-linux-gnu)

[bob@fedora29 3.9]$ ./oc version
oc v3.9.71
kubernetes v1.9.1+a0ce1bc657
features: Basic-Auth GSSAPI Kerberos SPNEGO

./oc get po --namespace=zhouy --sel<Tab>   can prompt completion --selector
./oc get po --namespace zhouy --sel<Tab>   can prompt completion --selector
./oc get pod --request-timeout=10s   --names<Tab>  can prompt completion --namespace=

Comment 15 errata-xmlrpc 2019-04-09 14:20:17 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2019:0619


Note You need to log in before you can comment on or make changes to this bug.