From d433c9ea767c460df3564357d866c56bd22bde71 Mon Sep 17 00:00:00 2001 From: Eileen Date: Fri, 11 Aug 2023 18:26:42 -0400 Subject: [PATCH] feat: update config on KB side --- pkg/plugins/external/api.go | 17 ++++++++++++++--- pkg/plugins/external/edit.go | 17 ++++++++++++++--- pkg/plugins/external/helpers.go | 25 ++++++++++++++++++++++--- pkg/plugins/external/init.go | 6 +++++- pkg/plugins/external/webhook.go | 17 ++++++++++++++--- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/pkg/plugins/external/api.go b/pkg/plugins/external/api.go index bff39f67411..002cab3b104 100644 --- a/pkg/plugins/external/api.go +++ b/pkg/plugins/external/api.go @@ -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" @@ -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 { @@ -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 +} diff --git a/pkg/plugins/external/edit.go b/pkg/plugins/external/edit.go index b048bbd5504..c1c8bdde1a5 100644 --- a/pkg/plugins/external/edit.go +++ b/pkg/plugins/external/edit.go @@ -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" @@ -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) { @@ -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 +} diff --git a/pkg/plugins/external/helpers.go b/pkg/plugins/external/helpers.go index cf4bd718414..95e1b9b26a8 100644 --- a/pkg/plugins/external/helpers.go +++ b/pkg/plugins/external/helpers.go @@ -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{} @@ -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) @@ -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 { var err error req.Universe, err = getUniverseMap(fs) @@ -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) + } for filename, data := range res.Universe { path := filepath.Join(currentDir, filename) diff --git a/pkg/plugins/external/init.go b/pkg/plugins/external/init.go index 5292361cad5..12af58ec695 100644 --- a/pkg/plugins/external/init.go +++ b/pkg/plugins/external/init.go @@ -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 } @@ -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 +} diff --git a/pkg/plugins/external/webhook.go b/pkg/plugins/external/webhook.go index af49ee06649..f89d95fc382 100644 --- a/pkg/plugins/external/webhook.go +++ b/pkg/plugins/external/webhook.go @@ -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" @@ -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 { @@ -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 +}