Skip to content

Commit

Permalink
feat: update config on KB side
Browse files Browse the repository at this point in the history
  • Loading branch information
Eileen-Yu committed Aug 11, 2023
1 parent 91afc94 commit d433c9e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
17 changes: 14 additions & 3 deletions pkg/plugins/external/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package external
import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand All @@ -32,8 +33,9 @@ const (
)

type createAPISubcommand struct {
Path string
Args []string
Path string
Args []string
config config.Config
}

func (p *createAPISubcommand) InjectResource(*resource.Resource) error {
Expand All @@ -56,10 +58,19 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
Args: p.Args,
}

err := handlePluginResponse(fs, req, p.Path)
err := handlePluginResponse(fs, req, p.Path, p)
if err != nil {
return err
}

return nil
}

func (p *createAPISubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

func (p *createAPISubcommand) GetConfig() config.Config {
return p.config
}
17 changes: 14 additions & 3 deletions pkg/plugins/external/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package external
import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
Expand All @@ -27,8 +28,9 @@ import (
var _ plugin.EditSubcommand = &editSubcommand{}

type editSubcommand struct {
Path string
Args []string
Path string
Args []string
config config.Config
}

func (p *editSubcommand) UpdateMetadata(_ plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
Expand All @@ -46,10 +48,19 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
Args: p.Args,
}

err := handlePluginResponse(fs, req, p.Path)
err := handlePluginResponse(fs, req, p.Path, p)
if err != nil {
return err
}

return nil
}

func (p *editSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

func (p *editSubcommand) GetConfig() config.Config {
return p.config
}
25 changes: 22 additions & 3 deletions pkg/plugins/external/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import (

"github.com/spf13/afero"
"github.com/spf13/pflag"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/external"
"sigs.k8s.io/yaml"
)

var outputGetter ExecOutputGetter = &execOutputGetter{}
Expand All @@ -51,6 +53,12 @@ type ExecOutputGetter interface {

type execOutputGetter struct{}

// PluginConfigHandler is an interface to update the config modified by external plugin.
type PluginConfigHandler interface {
GetConfig() config.Config
InjectConfig(config.Config) error
}

func (e *execOutputGetter) GetExecOutput(request []byte, path string) ([]byte, error) {
cmd := exec.Command(path) //nolint:gosec
cmd.Stdin = bytes.NewBuffer(request)
Expand Down Expand Up @@ -149,7 +157,7 @@ func getUniverseMap(fs machinery.Filesystem) (map[string]string, error) {
return universe, nil
}

func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string) error {
func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, path string, p PluginConfigHandler) error {

Check failure on line 160 in pkg/plugins/external/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 122 characters (lll)
var err error

req.Universe, err = getUniverseMap(fs)
Expand All @@ -167,8 +175,19 @@ func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, p
return fmt.Errorf("error getting current directory: %v", err)
}

// TODO: should handle updated PluginResponse.Config shown as below
fmt.Println(res.Config)
// TODO: for debug only, would delete it
fmt.Println("This is the received config from plugin response!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", res.Config)

// update the config
if res.Config != "" {
updatedConfig := p.GetConfig()

if err := yaml.Unmarshal([]byte(res.Config), updatedConfig); err != nil {
return fmt.Errorf("error unmarshalling the updated config from PluginResponse: %w", err)
}

p.InjectConfig(updatedConfig)

Check failure on line 189 in pkg/plugins/external/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `p.InjectConfig` is not checked (errcheck)
}

for filename, data := range res.Universe {
path := filepath.Join(currentDir, filename)
Expand Down
6 changes: 5 additions & 1 deletion pkg/plugins/external/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error {
Config: string(configBytes),
}

err = handlePluginResponse(fs, req, p.Path)
err = handlePluginResponse(fs, req, p.Path, p)
if err != nil {
return err
}
Expand All @@ -70,3 +70,7 @@ func (p *initSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

func (p *initSubcommand) GetConfig() config.Config {
return p.config
}
17 changes: 14 additions & 3 deletions pkg/plugins/external/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package external
import (
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand All @@ -28,8 +29,9 @@ import (
var _ plugin.CreateWebhookSubcommand = &createWebhookSubcommand{}

type createWebhookSubcommand struct {
Path string
Args []string
Path string
Args []string
config config.Config
}

func (p *createWebhookSubcommand) InjectResource(*resource.Resource) error {
Expand All @@ -52,10 +54,19 @@ func (p *createWebhookSubcommand) Scaffold(fs machinery.Filesystem) error {
Args: p.Args,
}

err := handlePluginResponse(fs, req, p.Path)
err := handlePluginResponse(fs, req, p.Path, p)
if err != nil {
return err
}

return nil
}

func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
p.config = c
return nil
}

func (p *createWebhookSubcommand) GetConfig() config.Config {
return p.config
}

0 comments on commit d433c9e

Please sign in to comment.