Skip to content

Commit d6414e9

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

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

pkg/cli/alpha/internal/update.go

Lines changed: 22 additions & 7 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.
@@ -195,15 +201,24 @@ func (opts *Update) cleanUpAncestorBranch() error {
195201
// to create clean scaffolding in the ancestor branch. This uses the downloaded
196202
// binary with the original PROJECT file to recreate the project's initial state.
197203
func (opts *Update) runAlphaGenerate(tempDir, version string) error {
198-
tempBinaryPath := tempDir + "/kubebuilder"
204+
205+
// Restore the original PROJECT file from master branch to ensure
206+
// we're using the correct project configuration for scaffolding
207+
gitCmd := exec.Command("git", "checkout", "master", "--", "PROJECT")
208+
if err := gitCmd.Run(); err != nil {
209+
return fmt.Errorf("failed to checkout PROJECT from master")
210+
}
211+
log.Info("Succesfully checked out the PROJECT file from master branch")
199212

200213
// Temporarily modify PATH to use the downloaded Kubebuilder binary
214+
tempBinaryPath := tempDir + "/kubebuilder"
201215
originalPath := os.Getenv("PATH")
202216
tempEnvPath := tempDir + ":" + originalPath
203217

204218
if err := os.Setenv("PATH", tempEnvPath); err != nil {
205219
return fmt.Errorf("failed to set temporary PATH: %w", err)
206220
}
221+
207222
// Restore original PATH when function completes
208223
defer func() {
209224
if err := os.Setenv("PATH", originalPath); err != nil {
@@ -219,7 +234,7 @@ func (opts *Update) runAlphaGenerate(tempDir, version string) error {
219234

220235
// Restore the original PROJECT file from master branch to ensure
221236
// we're using the correct project configuration for scaffolding
222-
gitCmd := exec.Command("git", "checkout", "master", "--", "PROJECT")
237+
gitCmd = exec.Command("git", "checkout", "master", "--", "PROJECT")
223238
if err := gitCmd.Run(); err != nil {
224239
return fmt.Errorf("failed to checkout PROJECT from master")
225240
}

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)