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

🐛 fix(completion): Fix completion in deprecated projects #3474

Merged

Conversation

BronzeDeer
Copy link
Contributor

@BronzeDeer BronzeDeer commented Jun 21, 2023

Fixes #3473

Currently, when initializing in a project with deprecated plugins the kubebuilder cli will immediately output a deprecation notice to stdout before even parsing the command. While this is acceptable and even desirable for interactive usage of the cli, it will "randomly" (i.e. dependent on $PWD and not on the command) break automated usage of the kubebuilder output. This, again is fine if kubebuilder makes no guarantees of stable machine readable output, but this does not hold for the completion sub commands which are only ever parsed by machines: The output of "kubebuilder completion " must always only output the exact script generated by cobra and the cobra internal commands __complete and __completeNoDesc must only only return an exact list of completion options.

In deprecated projects this will break shell completion if the shell is started in the directory and if the completion script is correctly initialized, all completion options will be polluted by the massive deprecation notice.

This commit instead changes the deprecation notice to output to stderr (while maintaining the return code of the actual command). This fixes the completion usecase which only reads in stdout, and should not break existing integrations, unless there exist scripts which use the length of stderr to check for errors rather than the return code of the call.

Example broken completion output when deprecation notice is put out on stdout (ZSH)

source <(kubebuilder completion zsh)
# resulting error: /proc/self/fd/15:1: bad pattern: ^[[1

kubebuilder <tab>
# completion                                                                 
# create                                                                         
# edit                                                                           
# help                                                                           
# init                                                                           
# version                                                                        
# ^[[1;36m[Deprecation Notice] This version is deprecated.The `go/v3` cannot scaffold projects using kustomize versions v4x+ and cannot fully support Kubernetes 1.25+.It is recommended to upgrade your project to the latest versions available (go/v4).Please, check the migration guide to learn how to upgrade your project

@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jun 21, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @BronzeDeer!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubebuilder has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 21, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @BronzeDeer. Thanks for your PR.

I'm waiting for a kubernetes-sigs 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.

@BronzeDeer
Copy link
Contributor Author

Replaced commit with signed version

Currently, when initializing in a project with deprecated plugins
the kubebuilder cli will immediately output a deprecation notice to
stdout before even parsing the command. While this is acceptable and
even desirable for interactive usage of the cli, it will "randomly"
(i.e. dependent on $PWD and not on the command) break automated usage of
the kubebuilder output. This, again is fine if kubebuilder makes no
guarantees of stable machine readable output, but this does not hold for
the completion sub commands which are only ever parsed by machines: The
output of "kubebuilder completion <shell>" must always only output the
exact script generated by cobra and the cobra internal commands
__complete and __completeNoDesc must only only return an exact list of
completion options.

In deprecated projects this will break shell completion if the shell is
started in the directory and if the completion script is correctly
initialized, all completion options will be polluted by the massive
deprecation notice.

This commit instead changes the deprecation notice to output to stderr
(while maintaining the return code of the actual command). This fixes
the completion usecase which only reads in stdout, and should not break
existing integrations, unless there exist scripts which use the length
of stderr to check for errors rather than the return code of the call.

Signed-off-by: Joel Pepper <pepper@bronze-deer.de>
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jun 21, 2023
@BronzeDeer
Copy link
Contributor Author

Removed github style automatic reference to accompanying issue #3473 from commit message

@camilamacedo86
Copy link
Member

/ok-to-test

@BronzeDeer,

Thank you a lot for your contribution 🥇

Could you please just add in the description of the PR a print with the result of the change since it is hard to ensure with tests? So, that we have it tracked and we can easily review this one.

@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 Jun 23, 2023
@BronzeDeer
Copy link
Contributor Author

@camilamacedo86

I have copied the output error from the linked issue into the PR description.

As for tests, I could add a test that runs the three completion commands, captures stdout and Asserts that the deprecation notice is not in the captured output. This however only really tests the broken completion indirectly and is more of a regression test. If you consider such a test valuable, I'll add it

Copy link
Contributor

@Kavinjsir Kavinjsir left a comment

Choose a reason for hiding this comment

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

LGTM!
I think it is a good approach and the update to the test func here looks enough to me.
Thx @BronzeDeer !

Copy link
Member

@varshaprasad96 varshaprasad96 left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @BronzeDeer!
/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 Jul 24, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: BronzeDeer, Kavinjsir, varshaprasad96

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 Jul 24, 2023
@Kavinjsir
Copy link
Contributor

/test pull-kubebuilder-e2e-k8s-1-25-3

@k8s-ci-robot k8s-ci-robot merged commit d08f9d7 into kubernetes-sigs:master Aug 3, 2023
22 checks passed
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deprecation Notice breaks (atleast zsh) completion
5 participants