diff --git a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/external/types.go b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/external/types.go index 0b3eeb9bd26..c6bf8aaad96 100644 --- a/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/external/types.go +++ b/docs/book/src/simple-external-plugin-tutorial/testdata/sampleexternalplugin/v1/external/types.go @@ -93,7 +93,8 @@ type Flag struct { } type PluginConfig struct { - Resources []ResourceData `json:"resources,omitempty"` + ProjectName string `json:"projectname,omitempty"` + Resources []ResourceData `json:"resources,omitempty"` } type ResourceData struct { 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 bf633a109a1..35934f6c0ee 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,6 +21,7 @@ import ( "github.com/spf13/pflag" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" + "sigs.k8s.io/yaml" // "sigs.k8s.io/kubebuilder/v3/pkg/plugin/external" ) @@ -58,6 +59,40 @@ func ApiCmd(pr *external.PluginRequest) external.PluginResponse { flags.Parse(pr.Args) number, _ := flags.GetInt("number") + // Update the project config with GVK + cfg := external.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 := external.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 d5ba4de0b80..2e726e9d2d3 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,6 +16,9 @@ limitations under the License. package scaffolds import ( + "os" + "path/filepath" + "github.com/spf13/pflag" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" "sigs.k8s.io/yaml" @@ -59,7 +62,7 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse { flags.Parse(pr.Args) domain, _ := flags.GetString("domain") - // Update the project config + // Update the project config with ProjectName cfg := external.PluginConfig{} err := yaml.Unmarshal([]byte(pr.Config), &cfg) if err != nil { @@ -71,14 +74,20 @@ func InitCmd(pr *external.PluginRequest) external.PluginResponse { } } - // Create and append the new config info - newResource := external.ResourceData{ - Group: "group", - Domain: "my.domain", - Version: "v1", - Kind: "Externalpluginsample", + // Get current directory as the project name + cwd, err := os.Getwd() + if err != nil { + return external.PluginResponse{ + Error: true, + ErrorMsgs: []string{ + err.Error(), + }, + } } - cfg.Resources = append(cfg.Resources, newResource) + + dirName := filepath.Base(cwd) + + cfg.ProjectName = dirName updatedConfig, err := yaml.Marshal(cfg) if err != nil { diff --git a/test/e2e/externalplugin/generate_test.go b/test/e2e/externalplugin/generate_test.go index fa495485682..55750ea1c5b 100644 --- a/test/e2e/externalplugin/generate_test.go +++ b/test/e2e/externalplugin/generate_test.go @@ -77,6 +77,14 @@ func GenerateProject(kbc *utils.TestContext) { ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check initFile.txt should return no error.") ExpectWithOffset(1, initFileContainsExpr).To(BeTrue(), "The init file does not contain the expected expression.") + var initSubcommandConfigTmpl = "projectName" + + initSubcommandConfigContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "PROJECT"), initSubcommandConfigTmpl) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check PROJECT should return no error.") + //nolint:lll + ExpectWithOffset(1, initSubcommandConfigContainsExpr).To(BeTrue(), "The PROJECT file does not contain the expected config with the init subcommand.") + By("creating API definition") err = kbc.CreateAPI( "--plugins", "sampleexternalplugin/v1", @@ -93,6 +101,19 @@ func GenerateProject(kbc *utils.TestContext) { ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check apiFile.txt should return no error.") ExpectWithOffset(1, apiFileContainsExpr).To(BeTrue(), "The api file does not contain the expected expression.") + var apiSubcommandConfigTmpl = ` +resources: +- domain: my.domain + group: group + kind: Externalpluginsample + version: v1` + + apiSubcommandConfigContainsExpr, err := pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "PROJECT"), apiSubcommandConfigTmpl) + ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Check PROJECT should return no error.") + //nolint:lll + ExpectWithOffset(1, apiSubcommandConfigContainsExpr).To(BeTrue(), "The PROJECT file does not contain the expected config with the create api subcommand.") + By("scaffolding webhook") err = kbc.CreateWebhook( "--plugins", "sampleexternalplugin/v1",