@@ -11,6 +11,7 @@ import (
11
11
12
12
log "github.com/sirupsen/logrus"
13
13
"github.com/spf13/afero"
14
+ "sigs.k8s.io/kubebuilder/v4/pkg/config/store"
14
15
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
15
16
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
16
17
)
@@ -29,12 +30,8 @@ type Update struct {
29
30
// - upgrade: New Kubebuilder version scaffolding
30
31
// - merge: Attempts to merge upgrade changes into current state
31
32
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
- }
37
33
34
+ projectConfigFile , _ := opts .loadConfigFile ()
38
35
// Determine which Kubebuilder version to use for the update
39
36
cliVersion := projectConfigFile .Config ().GetCliVersion ()
40
37
@@ -95,6 +92,15 @@ func (opts *Update) Update() error {
95
92
return nil
96
93
}
97
94
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
+
98
104
// downloadKubebuilderBinary downloads the specified version of Kubebuilder binary
99
105
// from GitHub releases and saves it to a temporary directory with executable permissions.
100
106
// Returns the temporary directory path containing the binary.
@@ -195,15 +201,24 @@ func (opts *Update) cleanUpAncestorBranch() error {
195
201
// to create clean scaffolding in the ancestor branch. This uses the downloaded
196
202
// binary with the original PROJECT file to recreate the project's initial state.
197
203
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" )
199
212
200
213
// Temporarily modify PATH to use the downloaded Kubebuilder binary
214
+ tempBinaryPath := tempDir + "/kubebuilder"
201
215
originalPath := os .Getenv ("PATH" )
202
216
tempEnvPath := tempDir + ":" + originalPath
203
217
204
218
if err := os .Setenv ("PATH" , tempEnvPath ); err != nil {
205
219
return fmt .Errorf ("failed to set temporary PATH: %w" , err )
206
220
}
221
+
207
222
// Restore original PATH when function completes
208
223
defer func () {
209
224
if err := os .Setenv ("PATH" , originalPath ); err != nil {
@@ -219,7 +234,7 @@ func (opts *Update) runAlphaGenerate(tempDir, version string) error {
219
234
220
235
// Restore the original PROJECT file from master branch to ensure
221
236
// 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" )
223
238
if err := gitCmd .Run (); err != nil {
224
239
return fmt .Errorf ("failed to checkout PROJECT from master" )
225
240
}
0 commit comments