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

Move all bash custom completions to Go #96087

Merged
merged 8 commits into from Jun 26, 2021

Conversation

marckhouzam
Copy link
Member

@marckhouzam marckhouzam commented Nov 1, 2020

What type of PR is this?

/kind feature

What this PR does / why we need it:

Based on #93714, this PR finishes removing all bash completion scripting and replaces it fully with Go completions.

Advantages:

  • easier maintenance of custom completions
  • ability to write Go tests for custom completions
  • allow to eventually move to native zsh completion
  • allow Fish shell completion PR (add kubectl fish shell completion #92989) to fully support all of kubectl's custom completions
  • removes lack of portability of bash scripts
  • will allow to improve existing custom completions and add other ones

Which issue(s) this PR fixes:

Fixes kubernetes/kubectl#882

Special notes for your reviewer:

The PR is based on top of @knight42 great work of #93714. However, it has been mostly refactored when included with the rest of the PR.

The pattern that the PR follows is:

  • main completion functions are added in their matching files:
    • completion of api resource names: apiresources.go#CompGetResourceList(...)
    • completion of individual resources: get.go#CompGetResource(...)
  • a new completion.go file is added to the util package with the new completion code
  • the Cobra ValidArgsFunction field is added to every cobra command that was part of the bash __kubectl_custom_func. Each ValidArgsFunction calls the util package.
  • flag completion for --namespace, --context, --user, --cluster which are global was added in cmd.go
  • flag completion for --clusterrole which is limited to the create clusterrolebinding command was added in create_clusterrolebinding.go
  • go tests are added in the util package in completion_tests.go

Test the new completion code

  • build kubectl from source
make WHAT=cmd/kubectl
  • generate completion code
# zsh
source <(./_output/bin/kubectl completion zsh)

# bash
source <(./_output/bin/kubectl completion bash)
  • use it
    Please note that for the new completion code to work, one must use the new kubectl on the command-line.
./_output/bin/kubectl get [TAB]
# debug
./_output/bin/kubectl __complete get ""<ENTER>

./_output/bin/kubectl describe -n kube-system pod [TAB]

Does this PR introduce a user-facing change?:

Regression:

Custom completions don't work following concatenated short flags, e.g., kubectl exec -it <TAB>.
This will be fixed in the next Cobra release: spf13/cobra#1258

Only minor changes:

  • File completion is now disabled for the Go completions as it does not make sense for these cases
  • Completion for the cp command works slightly differently; instead of doing file completion all the time, which I found to be very verbose, I have used the same solution that helm implemented, which is to only show / and ./ to indicate file completion is valid, and then to trigger full file completion once the user starts typing something. You can try this by comparing: ./_output/bin/kubectl cp <TAB> and ./_output/bin/kubectl cp /tmp/<TAB>
  • Invalid command-lines will not lead to completions, for example:
    kubectl autoscale deployment --max NaN <TAB>
    will trigger file completion (that is what Cobra does when there is an error in the command-line) instead of accepting the invalid flag value and continuing with completion.
Shell completion has been migrated to Cobra's go solution.  `kubectl` is now smarter about disabling file completion when it does not apply.  Furthermore, completion for the `cp` command does not show all files unless the user has started typing something.

@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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Nov 1, 2020
@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 k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 1, 2020
@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-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Nov 1, 2020
@marckhouzam
Copy link
Member Author

I've added instructions on how to test, taken from #93714

You may be interested @brianpursley @eddiezane @knight42 @WLun001

@soltysh
Copy link
Contributor

soltysh commented Nov 2, 2020

/ok-to-test
/assign

@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 Nov 2, 2020
@marckhouzam marckhouzam force-pushed the feat/goComp branch 2 times, most recently from 5d701fb to 4bbc0b1 Compare November 7, 2020 11:30
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 9, 2020
This reverts commit 0e18f0380042b652996d795559bfb818698abec3.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 6, 2021
@marckhouzam
Copy link
Member Author

marckhouzam commented Jun 6, 2021

As for the tests, I'll wait for confirmation if testing individual commands for completion is or is not required, before cleaning up.

The tests should not keep delaying this PR, so I reverted the original commit which added the tests, and I wrote a few tests in the util package in completion_tests.go as suggested by @brianpursley. The original tests are still available in the commit history for reference.

Also, as a way to maybe to stir up some momentum, I moved to native zsh completion and removed all that extra hack-y shell code (see 77bb053).
With this, zsh gets completion descriptions:

$ source _output/bin/kubectl completion zsh
$ _output/bin/kubectl p<TAB>
patch         -- Update field(s) of a resource
plugin        -- Provides utilities for interacting with plugins.
port-forward  -- Forward one or more local ports to a pod
proxy         -- Run a proxy to the Kubernetes API server

Hopefully, we can get this merged.
/cc @brianpursley @rikatz @eddiezane

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

A few nits that can be addressed in a followup. Apologies for the delays in reviewing and thank you very much for this great effort to you @marckhouzam and @brianpursley and @phil9909 for reviews.
/lgtm
/approve

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
if len(args) == 0 {
if strings.IndexAny(toComplete, "/.~") == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

You'd need to support both linux and windows paths, so \\ would need to be included here as well.

Comment on lines 102 to 103
// FIXME
t.Skip("Should not be skipeed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, although I'd like to see this being continued in the near future.

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: marckhouzam, soltysh

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 Jun 25, 2021
@sttts
Copy link
Contributor

sttts commented Jun 25, 2021

😍

@marckhouzam
Copy link
Member Author

Thanks @soltysh! I will soon address your comment.

Should I revert the change that moves zsh completion to being native from this PR and leave it for a different PR?
I'm asking because I believe the zsh change will require an update to the documentation; specifically, aliases will be handled automatically by zsh, unlike what is mentioned here: https://kubernetes.io/docs/tasks/tools/included/optional-kubectl-configs-zsh/

@marckhouzam
Copy link
Member Author

Oh and there is still a regression with this change caused by spf13/cobra#1258

I'm still waiting for the next Cobra release: spf13/cobra#1388

@fejta-bot
Copy link

/retest
This bot automatically retries jobs that failed/flaked on approved PRs (send feedback to fejta).

Review the full test history for this PR.

Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@k8s-ci-robot k8s-ci-robot merged commit f7d2ecd into kubernetes:master Jun 26, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.22 milestone Jun 26, 2021
@marckhouzam marckhouzam deleted the feat/goComp branch June 26, 2021 14:50
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/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement kubectl custom command-line completions using go instead of bash