Skip to content

Commit 1eff876

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 1eff876

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ kubebuilder alpha update \
6161
--from-branch=main
6262
```
6363

64+
Force update even with merge conflicts:
65+
66+
```sh
67+
kubebuilder alpha update --force
68+
```
69+
6470
<aside class="note warning">
6571
<h1>You might need to upgrade your project first</h1>
6672

@@ -81,8 +87,46 @@ Once updated, you can use `kubebuilder alpha update` for future upgrades.
8187
| `--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. |
8288
| `--to-version` | Version to upgrade to. Defaults to the latest version. |
8389
| `--from-branch` | Git branch that contains your current project code. Defaults to `main`. |
90+
| `--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). |
8491
| `-h, --help` | Show help for this command. |
8592

93+
## Using the `--force` flag
94+
95+
When using the `--force` flag, merge conflicts will be committed with conflict markers in the `tmp-kb-update-merge` branch.
96+
You should carefully review and resolve these conflicts before merging into your main branch.
97+
98+
After resolving conflicts, run the following command to ensure manifests and generated files are up to date:
99+
```sh
100+
make manifests generate fmt vet lint-fix
101+
```
102+
103+
You might want to run `make all`, ensure that all tests are passing, and thoroughly validate the final result before committing and pushing a pull request to update your project from the `tmp-kb-update-merge` branch.
104+
105+
## Handling Merge Conflicts
106+
107+
If conflicts occur during a merge, Git will stop the process and leave the merge branch in a conflicted state for manual resolution.
108+
109+
To proceed with the merge despite conflicts, you can use the `--force` option. This is useful in automated environments, such as CI pipelines or cron jobs, where you want to create a pull request with the changes - even if conflicts are present.
110+
111+
```
112+
kubebuilder alpha update --force
113+
```
114+
The resulting commit will include conflict markers in the affected files:
115+
116+
```
117+
<<<<<<< HEAD
118+
Your changes
119+
=======
120+
Incoming changes
121+
>>>>>>> branch-name
122+
```
123+
124+
The commit message will indicate that conflicts are present and need to be resolved manually.
125+
126+
<aside>
127+
Note: 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.
128+
</aside>
129+
86130
<aside class="note warning">
87131
<h1>Projects generated with </h1>
88132

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)