diff --git a/pkg/plugin/external/types.go b/pkg/plugin/external/types.go index e9ac27d746c..6c506c35776 100644 --- a/pkg/plugin/external/types.go +++ b/pkg/plugin/external/types.go @@ -35,6 +35,9 @@ type PluginRequest struct { // Universe represents the modified file contents that gets updated over a series of plugin runs // across the plugin chain. Initially, it starts out as empty. Universe map[string]string `json:"universe"` + + // Config stores the project configuration file. + Config string `json:"config"` } // PluginResponse is returned to kubebuilder by the plugin and contains all files @@ -63,6 +66,9 @@ type PluginResponse struct { // Flags contains the plugin specific flags that the plugin returns to Kubebuilder when it receives // a request for a list of supported flags from Kubebuilder Flags []Flag `json:"flags,omitempty"` + + // Config stores the project configuration file. + Config string `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/init.go b/pkg/plugins/external/init.go index 221eef95ea6..5292361cad5 100644 --- a/pkg/plugins/external/init.go +++ b/pkg/plugins/external/init.go @@ -17,18 +17,22 @@ limitations under the License. package external import ( + "fmt" "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 _ plugin.InitSubcommand = &initSubcommand{} type initSubcommand struct { - Path string - Args []string + Path string + Args []string + config config.Config } func (p *initSubcommand) UpdateMetadata(_ plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) { @@ -40,16 +44,29 @@ 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)) + req := external.PluginRequest{ APIVersion: defaultAPIVersion, Command: "init", Args: p.Args, + Config: string(configBytes), } - err := handlePluginResponse(fs, req, p.Path) + err = handlePluginResponse(fs, req, p.Path) if err != nil { return err } return nil } + +func (p *initSubcommand) InjectConfig(c config.Config) error { + p.config = c + return nil +}