• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 16933797156

13 Aug 2025 09:53AM UTC coverage: 70.864% (+0.1%) from 70.724%
16933797156

push

github

web-flow
Merge pull request #5006 from kubernetes-sigs/dependabot/github_actions/actions/checkout-5

🌱 Bump actions/checkout from 4 to 5

2960 of 4177 relevant lines covered (70.86%)

14.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.77
/pkg/cli/alpha/update.go
1
/*
2
Copyright 2025 The Kubernetes Authors.
3
Licensed under the Apache License, Version 2.0 (the "License");
4
you may not use this file except in compliance with the License.
5
You may obtain a copy of the License at
6
        http://www.apache.org/licenses/LICENSE-2.0
7
Unless required by applicable law or agreed to in writing, software
8
distributed under the License is distributed on an "AS IS" BASIS,
9
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
See the License for the specific language governing permissions and
11
limitations under the License.
12
*/
13

14
package alpha
15

16
import (
17
        "fmt"
18
        "log"
19

20
        "github.com/spf13/cobra"
21

22
        "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/update"
23
)
24

25
// NewUpdateCommand creates and returns a new Cobra command for updating Kubebuilder projects.
26
// This command helps users upgrade their projects to newer versions of Kubebuilder by performing
27
// a three-way merge between:
28
// - The original scaffolding (ancestor)
29
// - The user's current project state (current)
30
// - The new version's scaffolding (upgrade)
31
//
32
// The update process creates multiple Git branches to facilitate the merge and help users
33
// resolve any conflicts that may arise during the upgrade process.
34
func NewUpdateCommand() *cobra.Command {
2✔
35
        opts := update.Update{}
2✔
36
        updateCmd := &cobra.Command{
2✔
37
                Use:   "update",
2✔
38
                Short: "Update your project to a newer version (3-way merge; optional single-commit)",
2✔
39
                Long: `Upgrade your project scaffold using a 3-way merge strategy while preserving your code.
2✔
40

2✔
41
The command creates temporary branches to perform the update:
2✔
42
  - ancestor:  clean scaffold from the original version
2✔
43
  - current:   your existing project state
2✔
44
  - upgrade:   scaffold generated with the target version
2✔
45
  - merge:     result of the 3-way merge (committed; conflict markers kept with --force)
2✔
46

2✔
47
By default, the merge result is committed on the temporary 'merge' branch. 
2✔
48
Use --squash to snapshot that result into a SINGLE commit on a stable branch,
2✔
49
ready for a PR:
2✔
50

2✔
51
  kubebuilder-alpha-update-to-<to-version>
2✔
52

2✔
53
This keeps history tidy (one commit per update run) and enables idempotent PRs.
2✔
54

2✔
55
Notes:
2✔
56
  • --force commits even if conflicts occur (markers are kept).
2✔
57
  • --preserve-path lets you keep files from your base branch when squashing
2✔
58
    (useful for CI configs like .github/workflows).
2✔
59
  • --output-branch optionally overrides the default squashed branch name.
2✔
60

2✔
61
Examples:
2✔
62
  # Update from the version in PROJECT to the latest, stop on conflicts
2✔
63
  kubebuilder alpha update
2✔
64

2✔
65
  # Update from a specific version to latest
2✔
66
  kubebuilder alpha update --from-version v4.6.0
2✔
67

2✔
68
  # Update from v4.5.0 to v4.7.0 and keep conflict markers (automation-friendly)
2✔
69
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force
2✔
70

2✔
71
  # Same as above, but produce ONE squashed commit on a stable PR branch
2✔
72
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force --squash
2✔
73

2✔
74
  # Squash while preserving CI workflows from base (e.g., main) on the squashed branch
2✔
75
  kubebuilder alpha update --force --squash --preserve-path .github/workflows
2✔
76

2✔
77
  # Squash into a custom output branch name
2✔
78
  kubebuilder alpha update --force --squash --output-branch my-update-branch
2✔
79

2✔
80
Behavior summary:
2✔
81
  • Without --force:
2✔
82
      - If conflicts occur during the 3-way merge, the command stops on the 'merge' branch
2✔
83
        for manual resolution (no commit made).
2✔
84
  • With --force:
2✔
85
      - Conflicted files are committed on the 'merge' branch with conflict markers.
2✔
86
  • With --squash:
2✔
87
      - After the merge step, the exact 'merge' tree is copied to a new/updated branch
2✔
88
        (default: kubebuilder-alpha-update-to-<to-version>) and committed ONCE, keeping markers
2✔
89
        if present. This branch is intended for opening/refreshing a PR.`,
2✔
90
                PreRunE: func(_ *cobra.Command, _ []string) error {
2✔
91
                        err := opts.Prepare()
×
92
                        if err != nil {
×
93
                                return fmt.Errorf("failed to prepare update: %w", err)
×
94
                        }
×
95
                        return opts.Validate()
×
96
                },
97
                Run: func(_ *cobra.Command, _ []string) {
×
98
                        if err := opts.Update(); err != nil {
×
99
                                log.Fatalf("Update failed: %s", err)
×
100
                        }
×
101
                },
102
        }
103

104
        updateCmd.Flags().StringVar(&opts.FromVersion, "from-version", "",
2✔
105
                "binary release version to upgrade from. Should match the version used to init the project and be"+
2✔
106
                        "a valid release version, e.g., v4.6.0. If not set, "+
2✔
107
                        "it defaults to the version specified in the PROJECT file. ")
2✔
108
        updateCmd.Flags().StringVar(&opts.ToVersion, "to-version", "",
2✔
109
                "binary release version to upgrade to. Should be a valid release version, e.g., v4.7.0. "+
2✔
110
                        "If not set, it defaults to the latest release version available in the project repository.")
2✔
111
        updateCmd.Flags().StringVar(&opts.FromBranch, "from-branch", "",
2✔
112
                "Git branch to use as current state of the project for the update.")
2✔
113
        updateCmd.Flags().BoolVar(&opts.Force, "force", false,
2✔
114
                "Force the update even if conflicts occur. Conflicted files will include conflict markers, and a "+
2✔
115
                        "commit will be created automatically. Ideal for automation (e.g., cronjobs, CI).")
2✔
116
        updateCmd.Flags().BoolVar(&opts.Squash, "squash", false,
2✔
117
                "After merging, write a single squashed commit with the merge result to a fixed branch "+
2✔
118
                        "named kubebuilder-alpha-update-to-<to-version>.")
2✔
119
        updateCmd.Flags().StringArrayVar(&opts.PreservePath, "preserve-path", nil,
2✔
120
                "Paths to preserve from the base branch when squashing (repeatable). "+
2✔
121
                        "Example: --preserve-path .github/workflows")
2✔
122
        updateCmd.Flags().StringVar(&opts.OutputBranch, "output-branch", "",
2✔
123
                "Override the default kubebuilder-alpha-update-to-<to-version> branch name (used with --squash).")
2✔
124

2✔
125
        return updateCmd
2✔
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc