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

✨ Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins #4572

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sarthaksarthak9
Copy link
Contributor

@sarthaksarthak9 sarthaksarthak9 commented Feb 21, 2025

Fixes: #4433

  • Fixed issue where 'kubebuilder alpha generate' failed if PROJECT contained go.kubebuilder.io/v3.
  • Automatically updates outdated plugin references (v3 → v4) to support scaffolding.
  • Added a warning to notify users before modifying the PROJECT file.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 21, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: sarthaksarthak9
Once this PR has been reviewed and has the lgtm label, please assign camilamacedo86 for approval. For more information see the Code Review Process.

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

@k8s-ci-robot
Copy link
Contributor

Hi @sarthaksarthak9. 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-sigs/prow 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. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 21, 2025
@sarthaksarthak9 sarthaksarthak9 changed the title (:sparkles:) Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins (:sparkles:): Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins Feb 21, 2025
@sarthaksarthak9 sarthaksarthak9 changed the title (:sparkles:): Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins (:sparkles:)Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins Feb 21, 2025
@sarthaksarthak9 sarthaksarthak9 changed the title (:sparkles:)Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins ✨ Ensure 'kubebuilder alpha generate' rescaffolds with outdated plugins Feb 21, 2025

for i, plugin := range plugins {
if plugin == "go.kubebuilder.io/v3" {
plugins[i] = "go.kubebuilder.io/v4"
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 add a break here?
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there's a possibility that "go.kubebuilder.io/v3" appears multiple times in plugins, then we should not add a break, as we need to update all occurrences.

Copy link
Member

Choose a reason for hiding this comment

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

No, it can appears just once.
We can either check the layout, see: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4/PROJECT#L7

log.Warnf("Detected 'go.kubebuilder.io/v3' in PROJECT file.")
log.Warnf("Kubebuilder v4 no longer supports this. It will be replaced with 'go.kubebuilder.io/v4'.")

fmt.Print("Do you want to proceed? (y/N): ")
Copy link
Member

@camilamacedo86 camilamacedo86 Feb 21, 2025

Choose a reason for hiding this comment

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

That is very cool 🥇 🚀
When we scaffold the controllers/API (kubebuilder create API) we asked if we should scaffold both controller + API. So, we have some code utils for that already, Could we try to use those? See:

Copy link
Member

Choose a reason for hiding this comment

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

Also, to allow us to test out this option with scripts, could we create a flag so that we do not need to ask if the flag is set? --allow-best-effort OR we can use --force true OR any other term if you have a better idea/suggestion.

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

Really great job 🥇 🎉
I added some comments, and I think we need to add a test for this case.
Otherwise, it is all amazing !!!

if c.cmd.CalledAs() == "alpha generate" {
err := updateProjectFileForAlphaGenerate()
if err != nil {
return fmt.Errorf("failed to update PROJECT file: %w", err)
Copy link
Member

Choose a reason for hiding this comment

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

We have tests here: https://github.com/kubernetes-sigs/kubebuilder/tree/master/test/e2e/alphagenerate

We can create a new project and add 1 API
Then, update the PROJECT file to replace go.kubebuilder.io/v4 with go.kubebuilder.io/v2 or go.kubebuilder.io/v3 and then call the command with the flag that --force OR allow upgrade the PROJECT file without the need to say Y so we can ensure that works fine.

WDYT?

@sarthaksarthak9
Copy link
Contributor Author

sarthaksarthak9 commented Feb 22, 2025

@camilamacedo86 I noticed that updating the plugin list in getInitArgs() automatically updates the PROJECT file, even though I don’t explicitly see a save function. How is this happening? Could you help me confirm where the file update is triggered?. Also, is there still a need for updateProjectFileForAlphaGenerate() in this case?


// No need to update if v3 is not found
if !strings.Contains(projectStr, "go.kubebuilder.io/v3") {
return nil
Copy link
Member

Choose a reason for hiding this comment

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

You need fix the lint issues.
You can run locally make fix-lint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kubebuilder alpha generate fails to rescaffold if PROJECT contains go.kubebuilder.io/v3
3 participants