Skip to content

Commit

Permalink
load config
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Feb 14, 2024
1 parent 9ef48b9 commit a0ba688
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/plugin/external/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package external

import (
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand All @@ -40,7 +40,7 @@ type PluginRequest struct {
Universe map[string]string `json:"universe"`

// Config stores the project configuration file.
Config v3cfg.Cfg `json:"config"`
Config config.Config `json:"config"`
}

// PluginResponse is returned to kubebuilder by the plugin and contains all files
Expand Down Expand Up @@ -71,7 +71,7 @@ type PluginResponse struct {
Flags []Flag `json:"flags,omitempty"`

// Config stores the project configuration file.
Config v3cfg.Cfg `json:"config"`
Config config.Config `json:"config"`
}

// Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed
Expand Down
10 changes: 7 additions & 3 deletions pkg/plugins/external/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package external

import (
"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand Down Expand Up @@ -53,13 +54,16 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
}

func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
cfg := p.config.(*v3cfg.Cfg)
cfg := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
if err := cfg.Load(); err != nil {
return err
}

req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Command: "create api",
Args: p.Args,
Config: *cfg,
Config: cfg.Config(),
}

err := handlePluginResponse(fs, req, p.Path, p)
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/external/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, p
}

// update the config
if err := p.InjectConfig(&res.Config); err != nil {
if err := p.InjectConfig(res.Config); err != nil {
return fmt.Errorf("error injecting the updated config from PluginResponse: %w", err)
}

Expand Down
24 changes: 21 additions & 3 deletions pkg/plugins/external/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ limitations under the License.
package external

import (
"os"

"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
Expand All @@ -43,13 +46,28 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
}

func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
cfg := p.config.(*v3cfg.Cfg)
fileName := "PROJECT"

if _, err := os.Stat(fileName); os.IsNotExist(err) {
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()

Check failure on line 56 in pkg/plugins/external/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `file.Close` is not checked (errcheck)
}

cfg := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
// cfg.Config().SetVersion(3)

if err := cfg.Load(); err != nil {
return err
}

req := external.PluginRequest{
APIVersion: defaultAPIVersion,
Command: "init",
Args: p.Args,
Config: *cfg,
Config: cfg.Config(),
}

err := handlePluginResponse(fs, req, p.Path, p)
Expand Down

0 comments on commit a0ba688

Please sign in to comment.