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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions docs/book/src/reference/commands/alpha_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ kubebuilder alpha update \
--from-branch=main
```

Force update even with merge conflicts:

```sh
kubebuilder alpha update --force
```

<aside class="note warning">
<h1>You might need to upgrade your project first</h1>

Expand All @@ -81,17 +87,58 @@ Once updated, you can use `kubebuilder alpha update` for future upgrades.
| `--from-version` | **Required for projects initialized with versions earlier than v4.6.0.** Kubebuilder version your project was created with. If unset, uses the `PROJECT` file. |
| `--to-version` | Version to upgrade to. Defaults to the latest version. |
| `--from-branch` | Git branch that contains your current project code. Defaults to `main`. |
| `--force` | 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). |
| `-h, --help` | Show help for this command. |

<aside class="note warning">
<h1>Projects generated with </h1>

<aside class="note">
Projects generated with **Kubebuilder v4.6.0** or later include the `cliVersion` field in the `PROJECT` file.
This field is used by `kubebuilder alpha update` to determine the correct CLI
version for upgrading your project.
</aside>

## Merge Conflicts with `--force`

When you use the `--force` flag with `kubebuilder alpha update`, Git will complete the merge even if there are conflicts. The resulting commit will include conflict markers like:
```
<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> branch-name
```
These conflicts will be committed in the
`tmp-kb-update-merge` branch.

<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

<aside class="note warning">
<H1>If you face conflicts (using or not the --force flag) </H1>
If the merge introduces conflicts, you must resolve them and **ensure** you execute the following command to regenerate the manifests and organise the files properly:

```bash
make manifests generate fmt vet lint-fix
```

Alternatively, you may want to run:

```bash
make all
```
</aside>


## When to Use `--force`
Use `--force` only in scenarios like:
- Automated environments (e.g., CI pipelines or cron jobs)
- When you need to create a PR even if conflicts are present
- When a human will resolve the conflicts later
`kubebuilder alpha update --force`

This ensures the update proceeds without manual blocking but shifts responsibility for conflict resolution to a follow-up manual step.

This approach is typically used in automation workflows where conflict markers are later addressed by a human, or where preserving the conflicting changes is acceptable for follow-up processing.

## Requirements

- A valid [PROJECT][project-config] file at the root of your project
Expand Down
11 changes: 10 additions & 1 deletion pkg/cli/alpha/internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Update struct {
ToVersion string
// FromBranch stores the branch to update from, e.g., "main".
FromBranch string
// Force commits the update changes even with merge conflicts
Force bool

// UpdateBranches
AncestorBranch string
Expand Down Expand Up @@ -335,8 +337,15 @@ func (opts *Update) mergeOriginalToUpgrade() error {
var exitErr *exec.ExitError
// If the merge has an error that is not a conflict, return an error 2
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
log.Warn("Merge completed with conflicts. Manual resolution required.")
hasConflicts = true
if !opts.Force {
log.Warn("Merge stopped due to conflicts. Manual resolution is required.")
log.Warn("After resolving the conflicts, run the following command:")
log.Warn(" make manifests generate fmt vet lint-fix")
log.Warn("This ensures manifests and generated files are up to date, and the project layout remains consistent.")
return fmt.Errorf("merge stopped due to conflicts")
}
log.Warn("Merge completed with conflicts. Conflict markers will be committed.")
} else {
return fmt.Errorf("merge failed unexpectedly: %w", err)
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/cli/alpha/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ The process uses Git branches:
- upgrade: scaffold from the target version
- merge: result of the 3-way merge

If conflicts occur during the merge, resolve them manually in the 'merge' branch.
Once resolved, commit and push it as a pull request. This branch will contain the
final upgraded project with the latest Kubebuilder layout and your custom code.
If conflicts occur during the merge, the command will stop and leave the merge branch for manual resolution.
Use --force to commit conflicts with markers instead.

Examples:
# Update from the version specified in the PROJECT file to the latest release
Expand All @@ -64,6 +63,9 @@ Examples:
# Update from a specific version to an specific release
kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0

# Force update even with merge conflicts (commit conflict markers)
kubebuilder alpha update --force

`,

PreRunE: func(_ *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -93,5 +95,9 @@ Examples:
updateCmd.Flags().StringVar(&opts.FromBranch, "from-branch", "",
"Git branch to use as current state of the project for the update.")

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?

}