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

Implement custom completions for some flags using Go #93714

Closed

Conversation

knight42
Copy link
Member

@knight42 knight42 commented Aug 5, 2020

What type of PR is this?

/kind feature

What this PR does / why we need it:

Implement custom completions using Go for flag --context, --cluster, --user and --namespace.

Which issue(s) this PR fixes:

Part of kubernetes/kubectl#882

Special notes for your reviewer:

Test the new completion code

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

# bash
source <(./_output/bin/kubectl completion bash)
  1. play with it

Please note that the new completion code requires the new kubectl.

./_output/bin/kubectl get --context [TAB]

# debug
./_output/bin/kubectl __complete get --context ''

Does this PR introduce a user-facing change?:

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. 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. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver area/cloudprovider area/code-generation area/dependency Issues or PRs related to dependency changes area/ipvs area/kubectl area/kubelet area/release-eng Issues or PRs related to the Release Engineering subproject area/test sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cli Categorizes an issue or PR as relevant to SIG CLI. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/network Categorizes an issue or PR as relevant to SIG Network. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/storage Categorizes an issue or PR as relevant to SIG Storage. sig/testing Categorizes an issue or PR as relevant to SIG Testing. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Aug 5, 2020
@k8s-ci-robot k8s-ci-robot requested review from freehan, wenjiaswe and a team August 5, 2020 11:42
@knight42
Copy link
Member Author

knight42 commented Aug 5, 2020

/cc @brianpursley @marckhouzam

PTAL. You might be only interested in this commit: 58e57ee

@marckhouzam
Copy link
Member

@knight42 how are things going for these Go completions?

@knight42
Copy link
Member Author

knight42 commented Sep 7, 2020

@marckhouzam Oh I think I have missed the comments from @brianpursley and just waiting for review. I am still working on it.

@knight42 knight42 force-pushed the refactor/custom-comp-in-go branch from 5f3790a to 7269c7d Compare September 7, 2020 12:49
@@ -0,0 +1,90 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

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

For reviewer: I move helper functions here as I think they might be reused for the completion of kubectl config get-{contexts,clusters}

@knight42 knight42 force-pushed the refactor/custom-comp-in-go branch 2 times, most recently from 36bf7fd to 3b29a23 Compare September 7, 2020 12:56
@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Sep 7, 2020

@knight42: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-kubernetes-e2e-gce-csi-serial 04c8a8c46dd9647532fa65321755c2d4b9807d84 link /test pull-kubernetes-e2e-gce-csi-serial
pull-kubernetes-e2e-gci-gce-ipvs 04c8a8c46dd9647532fa65321755c2d4b9807d84 link /test pull-kubernetes-e2e-gci-gce-ipvs
pull-kubernetes-e2e-gce-storage-slow 04c8a8c46dd9647532fa65321755c2d4b9807d84 link /test pull-kubernetes-e2e-gce-storage-slow

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

Only --context, --cluster, --user and --namespace are included.

Signed-off-by: knight42 <anonymousknight96@gmail.com>
@knight42 knight42 force-pushed the refactor/custom-comp-in-go branch from 3b29a23 to 3df31ca Compare September 7, 2020 13:12
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: knight42
To complete the pull request process, please assign soltysh
You can assign the PR to them by writing /assign @soltysh in a comment when ready.

The full list of commands accepted by this bot can be found 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

@brianpursley
Copy link
Member

@knight42 any update on this?

I think this is a good start, but what else would need to be implemented in order to get rid of the custom bash and zsh code in these two locations?

I think below is the key function. It looks like there are 7 completion cases that need to be implemented and then we could get rid of the above two blocks of code I linked to...

__kubectl_custom_func() {
    case ${last_command} in
        kubectl_get | kubectl_describe | kubectl_delete | kubectl_label | kubectl_edit | kubectl_patch |\
        kubectl_annotate | kubectl_expose | kubectl_scale | kubectl_autoscale | kubectl_taint | kubectl_rollout_* |\
        kubectl_apply_edit-last-applied | kubectl_apply_view-last-applied)
            __kubectl_get_resource
            return
            ;;
        kubectl_logs)
            __kubectl_require_pod_and_container
            return
            ;;
        kubectl_exec | kubectl_port-forward | kubectl_top_pod | kubectl_attach)
            __kubectl_get_resource_pod
            return
            ;;
        kubectl_cordon | kubectl_uncordon | kubectl_drain | kubectl_top_node)
            __kubectl_get_resource_node
            return
            ;;
        kubectl_config_use-context | kubectl_config_rename-context)
            __kubectl_config_get_contexts
            return
            ;;
        kubectl_config_delete-cluster)
            __kubectl_config_get_clusters
            return
            ;;
        kubectl_cp)
            __kubectl_cp
            return
            ;;
        *)
            ;;
    esac
}

Types of completions:

  • resources
  • pods & containers
  • pods
  • nodes
  • contexts <-- you already have the function for this one done
  • clusters <-- you already have the function for this one done
  • cp

@knight42
Copy link
Member Author

@brianpursley The original target of this PR is to only implement custom completions using Go for flag --context, --cluster, --user and --namespace. I think it is completed now.

but what else would need to be implemented in order to get rid of the custom bash and zsh code in these two locations?

We need to modify each command as described in https://github.com/spf13/cobra/blob/master/shell_completions.md#dynamic-completion-of-nouns. I plan to open separate PR for each command, perhaps we could open a tracking issue and call for community participation.

@marckhouzam
Copy link
Member

I feel this would have value to merge as is.

It will work for both bash and zsh and is a step in the right direction to get rid of the custom bash code.

And it will work automatically for fish completion if/when it is added (#92989)

@brianpursley
Copy link
Member

brianpursley commented Oct 9, 2020

I'm not trying to be difficult, but I am hesitant to merge this because it does not fully address the issue. I don't really like the idea of having some of the completions in go, but others still using custom scripts. I was hoping to see a PR that fully addresses the issue.

However, if someone with the ability to approve PRs is comfortable with doing it though, then I will go along with their opinion on this, as I don't want to make this a sticking point if everyone else sees things differently.

Either way, we need to make 100% sure this doesn't break existing completions for zsh and bash. This probably needs some manual confirmation as I don't think we have a way to test completions using integration tests. Also, when go completions are created, we should be able to have go unit tests for those now, so that might help too.

My number one concern is not to break existing completions which I know tool a long time and a lot of work to get working satisfactorily.

@marckhouzam
Copy link
Member

@brianpursley I agree with your concerns about making sure this doesn't break anything. That is why I personally like the gradual approach so as to make sure each change is safe.

That being said, you are also right that temporarily having half the completions in go and the other half in bash will still cause some confusion.

@knight42 are you hoping to continue this work for the rest of the custom completions? I would like to help but I worry that my total lack of knowledge of the kubectl code base will prevent me from suggesting the correct go code for the completions.

@marckhouzam
Copy link
Member

@brianpursley I've built upon @knight42 work and removed all bash scripting in #96087.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jan 30, 2021
@k8s-ci-robot
Copy link
Contributor

@knight42: PR needs rebase.

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-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 30, 2021
@knight42
Copy link
Member Author

knight42 commented Feb 5, 2021

/close
in favor of #96087

@knight42 knight42 closed this Feb 5, 2021
@knight42 knight42 deleted the refactor/custom-comp-in-go branch February 5, 2021 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. release-note-none Denotes a PR that doesn't merit a release note. 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.

6 participants