Skip to content

Commit 159dfa7

Browse files
committed
add --force flag to alpha update
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.
1 parent 79ca728 commit 159dfa7

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

docs/book/src/reference/commands/alpha_update.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ kubebuilder alpha update \
6060
--to-version=v4.6.0 \
6161
--from-branch=main
6262
```
63+
<aside class="note warning">
64+
<h1>Projects generated with </h1>
65+
66+
Projects generated with **Kubebuilder v4.6.0** or later include the `cliVersion` field in the `PROJECT` file.
67+
This field is used by `kubebuilder alpha update` to determine the correct CLI
68+
version for upgrading your project.
69+
</aside>
70+
71+
Force update even with merge conflicts:
72+
73+
```sh
74+
kubebuilder alpha update --force
75+
```
6376

6477
<aside class="note warning">
6578
<h1>You might need to upgrade your project first</h1>
@@ -81,17 +94,56 @@ Once updated, you can use `kubebuilder alpha update` for future upgrades.
8194
| `--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. |
8295
| `--to-version` | Version to upgrade to. Defaults to the latest version. |
8396
| `--from-branch` | Git branch that contains your current project code. Defaults to `main`. |
97+
| `--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). |
8498
| `-h, --help` | Show help for this command. |
8599

100+
## Merge Conflicts with `--force`
101+
102+
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:
103+
```
104+
<<<<<<< HEAD
105+
Your changes
106+
=======
107+
Incoming changes
108+
>>>>>>> branch-name
109+
```
110+
These conflicts will be committed in the
111+
`tmp-kb-update-merge` branch.
112+
86113
<aside class="note warning">
87-
<h1>Projects generated with </h1>
114+
<h1>You must manually resolve these conflicts before merging into your main branch.</h1>
88115

89-
Projects generated with **Kubebuilder v4.6.0** or later include the `cliVersion` field in the `PROJECT` file.
90-
This field is used by `kubebuilder alpha update` to determine the correct CLI
91-
version for upgrading your project.
116+
### Required Steps If Conflicts Are Present
92117

118+
If the merge introduces conflicts, follow these steps before committing any final changes:
119+
1. Resolve Conflicts;
120+
2. Open the conflicted files and manually edit them to resolve all merge markers;
121+
3. Run Required Make Targets;
122+
4. After resolving, run the following command to regenerate, format, vet, and lint the code:
123+
```
124+
make manifests generate fmt vet lint-fix
125+
```
126+
### Optional (but Recommended)
127+
128+
1. Run the full validation suite to ensure your project remains consistent and working:
129+
```
130+
make all
131+
```
132+
2. Create Pull Request
133+
After validation, commit your changes and push a pull request from `tmp-kb-update-merge` to your target branch.
93134
</aside>
94135

136+
## When to Use `--force`
137+
Use `--force` only in scenarios like:
138+
- Automated environments (e.g., CI pipelines or cron jobs)
139+
- When you need to create a PR even if conflicts are present
140+
- When a human will resolve the conflicts later
141+
`kubebuilder alpha update --force`
142+
143+
This ensures the update proceeds without manual blocking but shifts responsibility for conflict resolution to a follow-up manual step.
144+
145+
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.
146+
95147
## Requirements
96148

97149
- A valid [PROJECT][project-config] file at the root of your project

pkg/cli/alpha/internal/update/update.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Update struct {
3939
ToVersion string
4040
// FromBranch stores the branch to update from, e.g., "main".
4141
FromBranch string
42+
// Force commits the update changes even with merge conflicts
43+
Force bool
4244

4345
// UpdateBranches
4446
AncestorBranch string
@@ -335,8 +337,15 @@ func (opts *Update) mergeOriginalToUpgrade() error {
335337
var exitErr *exec.ExitError
336338
// If the merge has an error that is not a conflict, return an error 2
337339
if errors.As(err, &exitErr) && exitErr.ExitCode() == 1 {
338-
log.Warn("Merge completed with conflicts. Manual resolution required.")
339340
hasConflicts = true
341+
if !opts.Force {
342+
log.Warn("Merge stopped due to conflicts. Manual resolution is required.")
343+
log.Warn("After resolving the conflicts, run the following command:")
344+
log.Warn(" make manifests generate fmt vet lint-fix")
345+
log.Warn("This ensures manifests and generated files are up to date, and the project layout remains consistent.")
346+
return fmt.Errorf("merge stopped due to conflicts")
347+
}
348+
log.Warn("Merge completed with conflicts. Conflict markers will be committed.")
340349
} else {
341350
return fmt.Errorf("merge failed unexpectedly: %w", err)
342351
}

pkg/cli/alpha/update.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ The process uses Git branches:
5050
- upgrade: scaffold from the target version
5151
- merge: result of the 3-way merge
5252
53-
If conflicts occur during the merge, resolve them manually in the 'merge' branch.
54-
Once resolved, commit and push it as a pull request. This branch will contain the
55-
final upgraded project with the latest Kubebuilder layout and your custom code.
53+
If conflicts occur during the merge, the command will stop and leave the merge branch for manual resolution.
54+
Use --force to commit conflicts with markers instead.
5655
5756
Examples:
5857
# Update from the version specified in the PROJECT file to the latest release
@@ -64,6 +63,9 @@ Examples:
6463
# Update from a specific version to an specific release
6564
kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0
6665
66+
# Force update even with merge conflicts (commit conflict markers)
67+
kubebuilder alpha update --force
68+
6769
`,
6870

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

98+
updateCmd.Flags().BoolVar(&opts.Force, "force", false,
99+
"Force the update even if conflicts occur. Conflicted files will include conflict markers, and a "+
100+
"commit will be created automatically. Ideal for automation (e.g., cronjobs, CI).")
101+
96102
return updateCmd
97103
}

0 commit comments

Comments
 (0)