Skip to content

✨ (alpha update command): add --force flag #4936

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

Merged
merged 1 commit into from
Jul 21, 2025

Conversation

vitorfloriano
Copy link
Contributor

@vitorfloriano vitorfloriano commented Jul 17, 2025

This PR adds the --force flag to the alpha update command, as well as e2e tests for the flag.

The --force flag makes it possible to run the alpha update command in CI workflows.

If the --force flag is not passed to the command, the update operation will abort if there are conflicts, informing the user that manual resolution is necessary:

vitor@pc:~/go/src/github.com/vitorfloriano/multiversion$ kubebuilder alpha update --from-version v4.5.0 --to-version v4.
6.0 --from-branch main
INFO Checking if is a git repository
INFO Checking if branch has uncommitted changes
INFO Binary version v4.5.0 is available
INFO Binary version v4.6.0 is available
INFO Checking out base branch: main
INFO Using branch names:
INFO   Ancestor: tmp-ancestor-16-07-25-23-25
INFO   Original:  tmp-original-16-07-25-23-25
INFO   Upgrade:  tmp-upgrade-16-07-25-23-25
INFO   Merge:    tmp-merge-16-07-25-23-25
...
...
...
...
WARN Merge stopped due to conflicts. Manual resolution is required. 
WARN After resolving the conflicts, run the following command: 
WARN     make manifests generate fmt vet lint-fix 
WARN This ensures manifests and generated files are up to date, and the project layout remains consistent. 
FATA Update failed: failed to merge upgrade into merge branch: merge stopped due to conflicts 

If the --force flag is passed, the update will proceed, despite the conflicts, and commit the markers as is:

vitor@pc:~/go/src/github.com/vitorfloriano/multiversion$ kubebuilder alpha update --from-version v4.5.0 --to-version v4.6.0 --from-branch main --force
INFO Checking if is a git repository
INFO Checking if branch has uncommitted changes
INFO Binary version v4.5.0 is available
INFO Binary version v4.6.0 is available
INFO Checking out base branch: main
INFO Using branch names:
INFO   Ancestor: tmp-ancestor-16-07-25-23-39
INFO   Original:  tmp-original-16-07-25-23-39
INFO   Upgrade:  tmp-upgrade-16-07-25-23-39
INFO   Merge:    tmp-merge-16-07-25-23-39
...
...
...
...
...
...
INFO Preparing Merge branch with name tmp-merge-16-07-25-23-39 and performing merge
WARN Merge completed with conflicts. Conflict markers will be committed.

As a follow-up, we could find a way to not run the make targets if there are conflicts, to avoid the stream of warnings about failing operations caused by the presence of the markers in the code. Ending the operation with so many warnings could be confusing to users:

WARN Merge completed with conflicts. Conflict markers will be committed.
...
...
WARN make manifests failed: error running "make": exit status 2
...
...
WARN make generate failed: error running "make": exit status 2
...
...
WARN make fmt failed: error running "make": exit status 2
...
...
WARN make vet failed: error running "make": exit status 2
...
...
ERRO Running error: context loading failed: failed to load packages: failed to load packages: failed to load with go/pac
...
...
WARN make lint-fix failed: error running "make": exit status 2

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 17, 2025
@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 Jul 17, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @vitorfloriano. 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 size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 17, 2025
@@ -105,6 +105,45 @@ var _ = Describe("kubebuilder", func() {
By("validating custom code preservation")
validateCustomCodePreservation(mockProjectDir)
})

It("should stop on merge conflicts when --force is not used", func() {
By("creating mock project with kubebuilder v4.5.2")
Copy link
Member

@camilamacedo86 camilamacedo86 Jul 17, 2025

Choose a reason for hiding this comment

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

So, with conflict it is 4.5.0 to 4.6.0
Without conflict it is 4.5.2 to 4.6.0
Then, we can reuse the mock
and we can pass the version as arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@camilamacedo86 I created a mock using v4.5.2 (init, create api, etc) for testing and got the conflicts when updating to v4.6.0:

~/go/src/github.com/vitorfloriano/operator-project/mock-operator$ kubebuilder alpha update --from-version v4.5.2 --to-version v4.6.0 --from-branch main
INFO Checking if is a git repository
INFO Checking if branch has uncommitted changes
INFO Binary version v4.5.2 is available
INFO Binary version v4.6.0 is available
INFO Checking out base branch: main
INFO Using branch names:
INFO   Ancestor: tmp-ancestor-17-07-25-11-13
INFO   Original:  tmp-original-17-07-25-11-13
INFO   Upgrade:  tmp-upgrade-17-07-25-11-13
INFO   Merge:    tmp-merge-17-07-25-11-13
INFO Preparing Ancestor branch with name tmp-ancestor-17-07-25-11-13
INFO Generating project with version v4.5.2
WARN Using current working directory to re-scaffold the project
WARN This directory will be cleaned up and all files removed before the re-generation
INFO Cleaning directory:/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator
INFO Running cleanup:
$ sh -c rm -rf /home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/ *
INFO kubebuilder init:
$ kubebuilder init --plugins go.kubebuilder.io/v4 --domain example.com --repo github.com/example/mock-operator
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO Get controller runtime:
$ go get sigs.k8s.io/controller-runtime@v0.20.4
INFO Update dependencies:
$ go mod tidy
Next: define a resource with:
$ kubebuilder create api
INFO kubebuilder create api:
$ kubebuilder create api --plural webapps --group apps --version v1 --kind WebApp --resource --namespaced --controller
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO api/v1/webapp_types.go
INFO api/v1/groupversion_info.go
INFO internal/controller/suite_test.go
INFO internal/controller/webapp_controller.go
INFO internal/controller/webapp_controller_test.go
INFO Update dependencies:
$ go mod tidy
INFO Running make:
$ make generate
mkdir -p /home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin
Downloading sigs.k8s.io/controller-tools/cmd/controller-gen@v0.17.2
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests
INFO Grafana plugin not found, skipping migration
INFO Deploy-image plugin not found, skipping migration
INFO Successfully ran alpha generatev4.5.2
INFO Running make manifests:
$ make manifests
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
INFO Running make generate:
$ make generate
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
INFO Running make fmt:
$ make fmt
go fmt ./...
INFO Running make vet:
$ make vet
go vet ./...
INFO Running make lint-fix:
$ make lint-fix
Downloading github.com/golangci/golangci-lint/cmd/golangci-lint@v1.63.4
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/golangci-lint run --fix
INFO Preparing Original branch with name tmp-original-17-07-25-11-13
INFO Preparing Upgrade branch with name tmp-upgrade-17-07-25-11-13
INFO Generating project with version v4.6.0
WARN Using current working directory to re-scaffold the project
WARN This directory will be cleaned up and all files removed before the re-generation
INFO Cleaning directory:/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator
INFO Running cleanup:
$ sh -c rm -rf /home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/ *
INFO kubebuilder init:
$ kubebuilder init --plugins go.kubebuilder.io/v4 --domain example.com --repo github.com/example/mock-operator
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO Get controller runtime:
$ go get sigs.k8s.io/controller-runtime@v0.21.0
INFO Update dependencies:
$ go mod tidy
Next: define a resource with:
$ kubebuilder create api
INFO kubebuilder create api:
$ kubebuilder create api --plural webapps --group apps --version v1 --kind WebApp --resource --namespaced --controller
INFO Writing kustomize manifests for you to edit...
INFO Writing scaffold for you to edit...
INFO api/v1/webapp_types.go
INFO api/v1/groupversion_info.go
INFO internal/controller/suite_test.go
INFO internal/controller/webapp_controller.go
INFO internal/controller/webapp_controller_test.go
INFO Update dependencies:
$ go mod tidy
INFO Running make:
$ make generate
mkdir -p /home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin
Downloading sigs.k8s.io/controller-tools/cmd/controller-gen@v0.18.0
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:
$ make manifests
INFO Grafana plugin not found, skipping migration
INFO Deploy-image plugin not found, skipping migration
INFO Successfully ran alpha generatev4.6.0
INFO Running make manifests:
$ make manifests
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
INFO Running make generate:
$ make generate
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
INFO Running make fmt:
$ make fmt
go fmt ./...
INFO Running make vet:
$ make vet
go vet ./...
INFO Running make lint-fix:
$ make lint-fix
Downloading github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.1.0
/home/vitor/go/src/github.com/vitorfloriano/operator-project/mock-operator/bin/golangci-lint run --fix
0 issues.
INFO Preparing Merge branch with name tmp-merge-17-07-25-11-13 and performing merge
WARN Merge stopped due to conflicts. Manual resolution is required.
WARN After resolving the conflicts, run the following command:
WARN     make manifests generate fmt vet lint-fix
WARN This ensures manifests and generated files are up to date, and the project layout remains consistent.
FATA Update failed: failed to merge upgrade into merge branch: merge stopped due to conflicts

So I believe we can use v4.5.2 for all test cases.

Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

I created a mock using v4.5.2 (init, create api, etc) for testing and got the conflicts when updating to v4.6.0:

We should not face conflict
When I test the PR: https://github.com/camilamacedo86/wordpress-operator/pull/4/commits
It has none conflicts se we will need to review it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@camilamacedo86 Alright, I updated the tests. For that, I removed the constants with hardcoded versions and added variable so that each test can set its fromVersion value. Let me know what you think.

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 19, 2025

Choose a reason for hiding this comment

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

If we have a conflict from 4.5.2 to 4.6.0, then we need to check the implementation.
Are we calling makeTargets after we call re-scaffold with 4.5.2 ?
PS. That test was done with the review PR

Also, check the mock.
See if it is == the example used

By last way that you can run those tests locally as well now ;-)

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 20, 2025
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 20, 2025
@vitorfloriano vitorfloriano force-pushed the force-flag branch 6 times, most recently from 159dfa7 to a292c8f Compare July 20, 2025 22:44
updateCmd.Flags().BoolVar(&opts.Force, "force", false,
"Force the update even if conflicts occur. Conflicted files will include conflict markers, and a "+
"commit will be created automatically. Ideal for automation (e.g., cronjobs, CI).")

return updateCmd
Copy link
Member

Choose a reason for hiding this comment

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

Also, could we add the e2e tests as before?
We need supplement the tests.
I fixed the mock sample in https://github.com/kubernetes-sigs/kubebuilder/pull/4938/files for it be as it was done in our POCs.

Therefore, from 4.5.2 to 4.6.0, we should not have conflicts, as from 4.5.0 or 4.4.0 to 4.6.0, we will likely encounter them.

Copy link
Member

@camilamacedo86 camilamacedo86 Jul 21, 2025

Choose a reason for hiding this comment

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

Lets do one thing
We move forward with now, we have the flag + docs
And then, we add the e2e tests in a follow up 🙂
Is that OK?

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jul 21, 2025
This commit adds the --force flag to the alpha update command.
It also adds documentation for the flag.
The --force flag makes it possible to run the alpha update command in CI workflows.
@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 Jul 21, 2025
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 21, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: camilamacedo86, vitorfloriano

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 21, 2025
<aside class="note warning">
You must manually resolve these conflicts before merging into your main branch.

```suggestion
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
```suggestion

The suggestion should not be added

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/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.

3 participants