Skip to content

Commit 22cae49

Browse files
authored
Merge pull request #4871 from vitorfloriano/feat-alpha-update
✨ (alpha update) initial code for the `alpha update` command
2 parents 8ac30b1 + 0844168 commit 22cae49

File tree

9 files changed

+1317
-31
lines changed

9 files changed

+1317
-31
lines changed

pkg/cli/alpha.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const (
3131
var alphaCommands = []*cobra.Command{
3232
newAlphaCommand(),
3333
alpha.NewScaffoldCommand(),
34+
alpha.NewUpdateCommand(),
3435
}
3536

3637
func newAlphaCommand() *cobra.Command {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/afero"
24+
25+
"sigs.k8s.io/kubebuilder/v4/pkg/config/store"
26+
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
27+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
28+
)
29+
30+
// LoadProjectConfig load the project config.
31+
func LoadProjectConfig(inputDir string) (store.Store, error) {
32+
projectConfig := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
33+
if err := projectConfig.LoadFrom(fmt.Sprintf("%s/%s", inputDir, yaml.DefaultPath)); err != nil {
34+
return nil, fmt.Errorf("failed to load PROJECT file: %w", err)
35+
}
36+
return projectConfig, nil
37+
}
38+
39+
// GetInputPath will return the input path for the project.
40+
func GetInputPath(inputPath string) (string, error) {
41+
if inputPath == "" {
42+
cwd, err := os.Getwd()
43+
if err != nil {
44+
return "", fmt.Errorf("failed to get working directory: %w", err)
45+
}
46+
inputPath = cwd
47+
}
48+
projectPath := fmt.Sprintf("%s/%s", inputPath, yaml.DefaultPath)
49+
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
50+
return "", fmt.Errorf("project path %q does not exist: %w", projectPath, err)
51+
}
52+
return inputPath, nil
53+
}

pkg/cli/alpha/internal/generate.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ import (
2323
"os/exec"
2424
"strings"
2525

26+
"sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/common"
27+
2628
log "github.com/sirupsen/logrus"
2729

28-
"github.com/spf13/afero"
2930
"sigs.k8s.io/kubebuilder/v4/pkg/config"
3031
"sigs.k8s.io/kubebuilder/v4/pkg/config/store"
31-
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
32-
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
3332
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
3433
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3534
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
@@ -46,9 +45,9 @@ type Generate struct {
4645

4746
// Generate handles the migration and scaffolding process.
4847
func (opts *Generate) Generate() error {
49-
projectConfig, err := loadProjectConfig(opts.InputDir)
48+
projectConfig, err := common.LoadProjectConfig(opts.InputDir)
5049
if err != nil {
51-
return err
50+
return fmt.Errorf("error loading project config: %v", err)
5251
}
5352

5453
if opts.OutputDir == "" {
@@ -138,7 +137,7 @@ func (opts *Generate) Generate() error {
138137
// Validate ensures the options are valid and kubebuilder is installed.
139138
func (opts *Generate) Validate() error {
140139
var err error
141-
opts.InputDir, err = getInputPath(opts.InputDir)
140+
opts.InputDir, err = common.GetInputPath(opts.InputDir)
142141
if err != nil {
143142
return fmt.Errorf("error getting input path %q: %w", opts.InputDir, err)
144143
}
@@ -151,15 +150,6 @@ func (opts *Generate) Validate() error {
151150
return nil
152151
}
153152

154-
// Helper function to load the project configuration.
155-
func loadProjectConfig(inputDir string) (store.Store, error) {
156-
projectConfig := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
157-
if err := projectConfig.LoadFrom(fmt.Sprintf("%s/%s", inputDir, yaml.DefaultPath)); err != nil {
158-
return nil, fmt.Errorf("failed to load PROJECT file: %w", err)
159-
}
160-
return projectConfig, nil
161-
}
162-
163153
// Helper function to create the output directory.
164154
func createDirectory(outputDir string) error {
165155
if err := os.MkdirAll(outputDir, 0o755); err != nil {
@@ -276,22 +266,6 @@ func createAPIWithDeployImage(resourceData v1alpha1.ResourceData) error {
276266
return nil
277267
}
278268

279-
// Helper function to get input path.
280-
func getInputPath(inputPath string) (string, error) {
281-
if inputPath == "" {
282-
cwd, err := os.Getwd()
283-
if err != nil {
284-
return "", fmt.Errorf("failed to get working directory: %w", err)
285-
}
286-
inputPath = cwd
287-
}
288-
projectPath := fmt.Sprintf("%s/%s", inputPath, yaml.DefaultPath)
289-
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
290-
return "", fmt.Errorf("project path %q does not exist: %w", projectPath, err)
291-
}
292-
return inputPath, nil
293-
}
294-
295269
// Helper function to get Init arguments for Kubebuilder.
296270
func getInitArgs(s store.Store) []string {
297271
var args []string

0 commit comments

Comments
 (0)