Skip to content

Commit 565f736

Browse files
Apply suggestions from code review
Co-authored-by: Camila Macedo <[email protected]>
1 parent 1340dc8 commit 565f736

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

pkg/cli/alpha/internal/update.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
log "github.com/sirupsen/logrus"
1313
"github.com/spf13/afero"
14+
"sigs.k8s.io/kubebuilder/v4/pkg/config/store"
1415
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
1516
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
1617
)
@@ -29,12 +30,8 @@ type Update struct {
2930
// - upgrade: New Kubebuilder version scaffolding
3031
// - merge: Attempts to merge upgrade changes into current state
3132
func (opts *Update) Update() error {
32-
// Load the PROJECT configuration file to get the current CLI version
33-
projectConfigFile := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
34-
if err := projectConfigFile.LoadFrom(yaml.DefaultPath); err != nil { // TODO: assess if DefaultPath could be renamed to a more self-descriptive name
35-
return fmt.Errorf("fail to run command: %w", err)
36-
}
3733

34+
projectConfigFile, _ := opts.loadConfigFile()
3835
// Determine which Kubebuilder version to use for the update
3936
cliVersion := projectConfigFile.Config().GetCliVersion()
4037

@@ -95,6 +92,15 @@ func (opts *Update) Update() error {
9592
return nil
9693
}
9794

95+
// Load the PROJECT configuration file to get the current CLI version
96+
func (opts *Update) loadConfigFile() (store.Store, error) {
97+
projectConfigFile := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
98+
if err := projectConfigFile.LoadFrom(yaml.DefaultPath); err != nil { // TODO: assess if DefaultPath could be renamed to a more self-descriptive name
99+
return projectConfigFile, fmt.Errorf("fail to run command: %w", err)
100+
}
101+
return projectConfigFile, nil
102+
}
103+
98104
// downloadKubebuilderBinary downloads the specified version of Kubebuilder binary
99105
// from GitHub releases and saves it to a temporary directory with executable permissions.
100106
// Returns the temporary directory path containing the binary.

pkg/cli/alpha/update.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,30 @@ func NewUpdateCommand() *cobra.Command {
2020
updateCmd := &cobra.Command{
2121
Use: "update",
2222
Short: "Update a Kubebuilder project to a newer version",
23-
Long: `Update a Kubebuilder project to a newer version using a three-way merge strategy.
23+
Long: `This command upgrades your Kubebuilder project to the latest scaffold layout using a 3-way merge strategy.
2424
25-
This command helps you upgrade your Kubebuilder project by:
26-
1. Creating a clean ancestor branch with the old version's scaffolding
27-
2. Creating a current branch with your project's current state
28-
3. Creating an upgrade branch with the new version's scaffolding
29-
4. Attempting to merge the changes automatically
25+
It performs the following steps:
26+
1. Creates an 'ancestor' branch from the version originally used to scaffold the project
27+
2. Creates a 'current' branch with your project's current state
28+
3. Creates an 'upgrade' branch using the new version's scaffolding
29+
4. Attempts a 3-way merge into a 'merge' branch
3030
31-
The process creates several Git branches to help you manage the upgrade:
32-
- ancestor: Clean scaffolding from the original version
33-
- current: Your project's current state
34-
- upgrade: Clean scaffolding from the new version
35-
- merge: Attempted automatic merge of upgrade into current
31+
The process uses Git branches:
32+
- ancestor: clean scaffold from the original version
33+
- current: your existing project state
34+
- upgrade: scaffold from the target version
35+
- merge: result of the 3-way merge
3636
37-
If conflicts occur during the merge, you'll need to resolve them manually.
37+
If conflicts occur during the merge, resolve them manually in the 'merge' branch.
38+
Once resolved, commit and push it as a pull request. This branch will contain the
39+
final upgraded project with the latest Kubebuilder layout and your custom code.
3840
3941
Examples:
4042
# Update using the version specified in PROJECT file
4143
kubebuilder alpha update
4244
4345
# Update from a specific version
44-
kubebuilder alpha update --from-version v3.0.0
45-
46-
Requirements:
47-
- Must be run from the root of a Kubebuilder project
48-
- Git repository must be clean (no uncommitted changes)
49-
- PROJECT file must exist and contain a valid layout version`,
46+
kubebuilder alpha update --from-version v3.0.0`,
5047

5148
// TODO: Add validation to ensure we're in a Kubebuilder project and Git repo is clean
5249
// PreRunE: func(_ *cobra.Command, _ []string) error {
@@ -62,7 +59,7 @@ Requirements:
6259

6360
// Flag to override the version specified in the PROJECT file
6461
updateCmd.Flags().StringVar(&opts.FromVersion, "from-version", "",
65-
"Override the CLI version from PROJECT file. Specify the Kubebuilder version to upgrade from (e.g., 'v3.0.0' or '3.0.0')")
62+
"Kubebuilder binary release version to upgrade from (e.g., 'v3.0.0'). Defaults to cliVersion in PROJECT file. Should match the version used to init the project."
6663

6764
return updateCmd
6865
}

0 commit comments

Comments
 (0)