From 9ef48b941eb168f88b89767ab4f083a507c9cff5 Mon Sep 17 00:00:00 2001 From: Eileen Yu Date: Mon, 12 Feb 2024 23:47:00 -0500 Subject: [PATCH] change cfg from string to struct --- .../sampleexternalplugin/v1/scaffolds/api.go | 35 ----------- .../sampleexternalplugin/v1/scaffolds/init.go | 59 +++++++------------ .../v1/scaffolds/interface.go | 29 --------- pkg/plugin/external/types.go | 9 ++- pkg/plugins/external/api.go | 4 ++ pkg/plugins/external/helpers.go | 16 +---- pkg/plugins/external/init.go | 15 ++--- 7 files changed, 36 insertions(+), 131 deletions(-) delete mode 100644 docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/interface.go diff --git a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/api.go b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/api.go index 5b315585142..ab7956e2337 100644 --- a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/api.go +++ b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/api.go @@ -21,7 +21,6 @@ import ( "github.com/spf13/pflag" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external" - "sigs.k8s.io/yaml" ) var ApiFlags = []external.Flag{ @@ -58,40 +57,6 @@ func ApiCmd(pr *external.PluginRequest) external.PluginResponse { flags.Parse(pr.Args) number, _ := flags.GetInt("number") - // Update the project config with GVK - cfg := PluginConfig{} - err := yaml.Unmarshal([]byte(pr.Config), &cfg) - if err != nil { - return external.PluginResponse{ - Error: true, - ErrorMsgs: []string{ - err.Error(), - }, - } - } - - // Create and append the new config info - newResource := ResourceData{ - Group: "group", - Domain: "my.domain", - Version: "v1", - Kind: "Externalpluginsample", - } - cfg.Resources = append(cfg.Resources, newResource) - - updatedConfig, err := yaml.Marshal(cfg) - if err != nil { - return external.PluginResponse{ - Error: true, - ErrorMsgs: []string{ - err.Error(), - }, - } - } - - // Update the PluginResponse with the modified config string - pluginResponse.Config = string(updatedConfig) - apiFile := api.NewApiFile(api.WithNumber(number)) // Phase 2 Plugins uses the concept of a "universe" to represent the filesystem for a plugin. diff --git a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/init.go b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/init.go index c715768c403..50bc17146a6 100644 --- a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/init.go +++ b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/init.go @@ -16,13 +16,9 @@ limitations under the License. package scaffolds import ( - "os" - "path/filepath" - "github.com/spf13/pflag" + "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" - "sigs.k8s.io/yaml" - "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external" "v1/scaffolds/internal/templates" ) @@ -61,45 +57,30 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse { flags.Parse(pr.Args) domain, _ := flags.GetString("domain") - // Update the project config with ProjectName - cfg := PluginConfig{} - err := yaml.Unmarshal([]byte(pr.Config), &cfg) - if err != nil { - return external.PluginResponse{ - Error: true, - ErrorMsgs: []string{ - err.Error(), - }, - } - } + // Update the project config + cfg := pr.Config - // Get current directory as the project name - cwd, err := os.Getwd() - if err != nil { - return external.PluginResponse{ - Error: true, - ErrorMsgs: []string{ - err.Error(), - }, - } - } + pluginChain := cfg.GetPluginChain() + pluginChain = append(pluginChain, pflag.Arg(0)) + cfg.SetPluginChain(pluginChain) - dirName := filepath.Base(cwd) - - cfg.ProjectName = dirName + if cfg.GetProjectName() == "" { + cfg.SetProjectName("externalplugin") + } - updatedConfig, err := yaml.Marshal(cfg) - if err != nil { - return external.PluginResponse{ - Error: true, - ErrorMsgs: []string{ - err.Error(), - }, - } + rsc := resource.Resource{ + GVK: resource.GVK{ + Group: "group", + Version: "v1", + Kind: "Externalpluginsample", + Domain: "my.domain", + }, } - // Update the PluginResponse with the modified config string - pluginResponse.Config = string(updatedConfig) + cfg.UpdateResource(rsc) + + // Update the PluginResponse with the modified updated config + pluginResponse.Config = cfg initFile := templates.NewInitFile(templates.WithDomain(domain)) diff --git a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/interface.go b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/interface.go deleted file mode 100644 index 28f92a6cb39..00000000000 --- a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/scaffolds/interface.go +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2023 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package scaffolds - -type PluginConfig struct { - ProjectName string `json:"projectname,omitempty"` - Resources []ResourceData `json:"resources,omitempty"` -} - -type ResourceData struct { - Group string `json:"group,omitempty"` - Domain string `json:"domain,omitempty"` - Version string `json:"version"` - Kind string `json:"kind"` -} diff --git a/pkg/plugin/external/types.go b/pkg/plugin/external/types.go index 6c506c35776..35200eb10f9 100644 --- a/pkg/plugin/external/types.go +++ b/pkg/plugin/external/types.go @@ -16,7 +16,10 @@ limitations under the License. package external -import "sigs.k8s.io/kubebuilder/v3/pkg/plugin" +import ( + v3cfg "sigs.k8s.io/kubebuilder/v3/pkg/config/v3" + "sigs.k8s.io/kubebuilder/v3/pkg/plugin" +) // PluginRequest contains all information kubebuilder received from the CLI // and plugins executed before it. @@ -37,7 +40,7 @@ type PluginRequest struct { Universe map[string]string `json:"universe"` // Config stores the project configuration file. - Config string `json:"config"` + Config v3cfg.Cfg `json:"config"` } // PluginResponse is returned to kubebuilder by the plugin and contains all files @@ -68,7 +71,7 @@ type PluginResponse struct { Flags []Flag `json:"flags,omitempty"` // Config stores the project configuration file. - Config string `json:"config"` + Config v3cfg.Cfg `json:"config"` } // Flag is meant to represent a CLI flag that is used by Kubebuilder to define flags that are parsed diff --git a/pkg/plugins/external/api.go b/pkg/plugins/external/api.go index 002cab3b104..6ea00fa7fdf 100644 --- a/pkg/plugins/external/api.go +++ b/pkg/plugins/external/api.go @@ -20,6 +20,7 @@ import ( "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/machinery" "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" @@ -52,10 +53,13 @@ func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) { } func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error { + cfg := p.config.(*v3cfg.Cfg) + req := external.PluginRequest{ APIVersion: defaultAPIVersion, Command: "create api", Args: p.Args, + Config: *cfg, } err := handlePluginResponse(fs, req, p.Path, p) diff --git a/pkg/plugins/external/helpers.go b/pkg/plugins/external/helpers.go index 66f33380bc3..15b5147c9ad 100644 --- a/pkg/plugins/external/helpers.go +++ b/pkg/plugins/external/helpers.go @@ -34,7 +34,6 @@ import ( "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{} @@ -176,20 +175,9 @@ func handlePluginResponse(fs machinery.Filesystem, req external.PluginRequest, p return fmt.Errorf("error getting current directory: %v", err) } - // 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) - } - - if err := p.InjectConfig(updatedConfig); err != nil { - return fmt.Errorf("error injecting the updated config from PluginResponse: %w", err) - } + if err := p.InjectConfig(&res.Config); err != nil { + return fmt.Errorf("error injecting the updated config from PluginResponse: %w", err) } for filename, data := range res.Universe { diff --git a/pkg/plugins/external/init.go b/pkg/plugins/external/init.go index c47131308ee..6a925c11291 100644 --- a/pkg/plugins/external/init.go +++ b/pkg/plugins/external/init.go @@ -17,15 +17,13 @@ limitations under the License. package external import ( - "fmt" - "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/machinery" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external" - "sigs.k8s.io/yaml" ) var _ plugin.InitSubcommand = &initSubcommand{} @@ -45,21 +43,16 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) { } func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error { - configBytes, err := yaml.Marshal(p.config) - if err != nil { - return err - } - - fmt.Println("Scaffolding with config:", string(configBytes)) + cfg := p.config.(*v3cfg.Cfg) req := external.PluginRequest{ APIVersion: defaultAPIVersion, Command: "init", Args: p.Args, - Config: string(configBytes), + Config: *cfg, } - err = handlePluginResponse(fs, req, p.Path, p) + err := handlePluginResponse(fs, req, p.Path, p) if err != nil { return err }