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

Shell completion of multiple resource names #105711

Merged
merged 1 commit into from Oct 27, 2021

Conversation

marckhouzam
Copy link
Member

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR teaches the completion functions to repeat resource names when applicable and supported. The logic checks if a resource name has already been specified by the user and does not include it again when repeating the completion.

For example, the get command can receive multiple pods names, therefore with this commit:
kubectl get pod pod1 [tab]
will provide completion of pod names again, but not show pod1 a second time since it is already part of the command-line.

The improvement affects the following commands:

  • annotate
  • apply edit-last-applied
  • apply view-last-applied
  • autoscale
  • delete
  • describe
  • edit
  • expose
  • get
  • label
  • patch
  • rollout history
  • rollout pause
  • rollout restart
  • rollout resume
  • rollout undo
  • scale
  • taint

Note that "rollout status" only accepts a single resource name, unlike the other "rollout ..." commands; this required the creation of a special completion function that did not repeat just for that case.

Which issue(s) this PR fixes:

Replaces #101427

Special notes for your reviewer:

The PR adds a utility function to remove a list of values from an array. It is used to prevent the completion from suggesting choices that are already on the command-line (as explained above). I don't know if such a utility already existed in the code base or if I put it in the right place (cmd/util/helpers.go#Unique(..))

Does this PR introduce a user-facing change?

Shell completion now knows to continue suggesting resource names when the command supports it.  For example "kubectl get pod pod1 <TAB>" will suggest more pod names.

/cc @phil9909

@k8s-ci-robot
Copy link
Contributor

@marckhouzam: GitHub didn't allow me to request PR reviews from the following users: phil9909.

Note that only kubernetes members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR teaches the completion functions to repeat resource names when applicable and supported. The logic checks if a resource name has already been specified by the user and does not include it again when repeating the completion.

For example, the get command can receive multiple pods names, therefore with this commit:
kubectl get pod pod1 [tab]
will provide completion of pod names again, but not show pod1 a second time since it is already part of the command-line.

The improvement affects the following commands:

  • annotate
  • apply edit-last-applied
  • apply view-last-applied
  • autoscale
  • delete
  • describe
  • edit
  • expose
  • get
  • label
  • patch
  • rollout history
  • rollout pause
  • rollout restart
  • rollout resume
  • rollout undo
  • scale
  • taint

Note that "rollout status" only accepts a single resource name, unlike the other "rollout ..." commands; this required the creation of a special completion function that did not repeat just for that case.

Which issue(s) this PR fixes:

Replaces #101427

Special notes for your reviewer:

The PR adds a utility function to remove a list of values from an array. It is used to prevent the completion from suggesting choices that are already on the command-line (as explained above). I don't know if such a utility already existed in the code base or if I put it in the right place (cmd/util/helpers.go#Unique(..))

Does this PR introduce a user-facing change?

Shell completion now knows to continue suggesting resource names when the command supports it.  For example "kubectl get pod pod1 <TAB>" will suggest more pod names.

/cc @phil9909

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 release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 16, 2021
@k8s-ci-robot
Copy link
Contributor

@marckhouzam: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

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
Copy link
Contributor

Hi @marckhouzam. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 16, 2021
@marckhouzam
Copy link
Member Author

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. area/kubectl and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 16, 2021
@lauchokyip
Copy link
Member

/ok-to-test
/cc @brianpursley

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 20, 2021
@brianpursley
Copy link
Member

/retest

Copy link
Member

@brianpursley brianpursley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I left a few comments, but this looks like a good improvement to completions.

@@ -733,3 +733,21 @@ func Warning(cmdErr io.Writer, newGeneratorName, oldGeneratorName string) {
oldGeneratorName,
)
}

// Unique removes any elements of subArray from fullArray and returns the result
func Unique(fullArray []string, subArray []string) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a couple of looks to realize what this function is doing.

It is called Unique but it does not de-duplicate items in the fullArray, it only removes them if their exist in subArray. That's fine, but I think the name could be more accurate for its purpose.

Might I suggest Exclude() as a name? Or Difference() (which I think would be the term for this in set theory)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Unique() is a very poor name in this case. Let's go with Difference() since it is used in Set theory.

var result []string
for _, elem := range fullArray {
found := false
for _, subElem := range subArray {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know subArray is usually going to be pretty small in your particular use case, but I think this could be a little simpler and more efficient by using a map instead of nested loops.

For example, something like this is what I'm thinking:

exclude := make(map[string]bool)
for _, elem := range subArray {
	exclude[elem] = true
}
var result []string
for _, elem := range fullArray {
	if _, found := exclude[elem]; !found {
		result = append(result, elem)
	}
}
return result

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good optimization. I will use it. Thanks.

@@ -733,3 +733,21 @@ func Warning(cmdErr io.Writer, newGeneratorName, oldGeneratorName string) {
oldGeneratorName,
)
}

// Unique removes any elements of subArray from fullArray and returns the result
func Unique(fullArray []string, subArray []string) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a unit test for this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
I needed a function to check if two arrays were equal so I created checkArrayEqual(), but if you know of an existing function that does that, please let me know.

@marckhouzam
Copy link
Member Author

Thanks for the review @brianpursley, I believe the latest version addresses your comments.

Copy link
Member

@brianpursley brianpursley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Tried it out locally and seems to work well. Nice work.

@marckhouzam
Copy link
Member Author

/assign @eddiezane

Copy link
Member

@eddiezane eddiezane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! Thanks for adding. A few nits to address and then lgtm.

@@ -321,3 +322,43 @@ func TestDumpReaderToFile(t *testing.T) {
t.Fatalf("Wrong file content %s != %s", testString, stringData)
}
}

func TestDifferenceFunc(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Let's use table driven tests here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better. Thanks for pointing it out.

}
}

func checkArrayEqual(arr1, arr2 []string) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already use go-cmp in kubectl and can use it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

defer tf.Cleanup()

pods, _, _ := cmdtesting.TestData()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use

tf, cmd := prepareCompletionTest()
addPodsToFactory(tf)

like above for these three tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what happened there. I created the common functions when moving to Go completions but forgot to use them for the other tests 😮

I fixed all of them. Thanks for catching that!

This commit teaches the completion function to repeat resource names
when supported by the command. The logic checks if a resource name
has already been specified by the user and does not include it again
when repeating the completion.

For example, the get command can receive multiple pods names, therefore
with this commit we have:
  kubectl get pod pod1 [tab]
will provide completion of pod names again, but not show 'pod1' since
it is already part of the command-line.

The improvement affects the following commands:
- annotate
- apply edit-last-applied
- apply view-last-applied
- autoscale
- delete
- describe
- edit
- expose
- get
- label
- patch
- rollout history
- rollout pause
- rollout restart
- rollout resume
- rollout undo
- scale
- taint

Note that "rollout status" only accepts a single resource name, unlike
the other "rollout ..." commands; this required the creation of a
special completion function that did not repeat just for that case.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@marckhouzam
Copy link
Member Author

Ready again @eddiezane

Copy link
Member

@eddiezane eddiezane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 27, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brianpursley, eddiezane, marckhouzam

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 27, 2021
@marckhouzam
Copy link
Member Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit aa7c633 into kubernetes:master Oct 27, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.23 milestone Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants