diff --git a/pkg/rescaffold/migrate.go b/pkg/rescaffold/migrate.go index 9f02efac10e..29f975ef96e 100644 --- a/pkg/rescaffold/migrate.go +++ b/pkg/rescaffold/migrate.go @@ -23,6 +23,7 @@ import ( "sigs.k8s.io/kubebuilder/v3/pkg/config/store" "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml" "sigs.k8s.io/kubebuilder/v3/pkg/machinery" + "sigs.k8s.io/kubebuilder/v3/pkg/model/resource" "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" ) @@ -55,6 +56,10 @@ func (opts *MigrateOptions) Rescaffold() error { if err := kubebuilderEdit(config); err != nil { log.Fatalf("Failed to run edit subcommand %v", err) } + // create APIs + if err := kubebuilderCreate(config); err != nil { + log.Fatalf("Failed to run create API subcommand %v", err) + } return nil } @@ -119,6 +124,21 @@ func kubebuilderEdit(store store.Store) error { return nil } +func kubebuilderCreate(store store.Store) error { + resources, err := store.Config().GetResources() + if err != nil { + return err + } + + for _, r := range resources { + if err = createAPI(r); err != nil { + return err + } + } + + return nil +} + func getInitArgs(store store.Store) []string { var args []string plugins := store.Config().GetPluginChain() @@ -132,3 +152,47 @@ func getInitArgs(store store.Store) []string { } return args } + +func createAPI(resource resource.Resource) error { + var args []string + args = append(args, "create") + args = append(args, "api") + args = append(args, getAPIGVKFlags(resource)...) + args = append(args, getAPIResourceFlags(resource)...) + return util.RunCmd("kubebuilder create api", "kubebuilder", args...) +} + +func getAPIGVKFlags(resource resource.Resource) []string { + var args []string + + if len(resource.Group) > 0 { + args = append(args, "--group", resource.Group) + } + if len(resource.Version) > 0 { + args = append(args, "--version", resource.Version) + } + if len(resource.Kind) > 0 { + args = append(args, "--kind", resource.Kind) + } + return args +} + +func getAPIResourceFlags(resource resource.Resource) []string { + var args []string + if resource.API == nil || resource.API.IsEmpty() { + // create API without creating resource + args = append(args, "--resource=false") + } else { + args = append(args, "--resource") + if resource.API.Namespaced { + args = append(args, "--namespaced") + } + } + + if resource.Controller { + args = append(args, "--controller") + } else { + args = append(args, "--controller=false") + } + return args +} diff --git a/test/e2e/alphagenerate/generate_test.go b/test/e2e/alphagenerate/generate_test.go index d20dfdd21fd..5dff728987f 100644 --- a/test/e2e/alphagenerate/generate_test.go +++ b/test/e2e/alphagenerate/generate_test.go @@ -100,6 +100,17 @@ func ReGenerateProject(kbc *utils.TestContext) { ) ExpectWithOffset(1, err).NotTo(HaveOccurred()) + By("create APIs with resource and controller") + err = kbc.CreateAPI( + "--group", "crew", + "--version", "v1", + "--kind", "Captain", + "--namespaced", + "--resource", + "--controller", + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + By("regenerating the project at another output directory") err = kbc.Regenerate( "--input-dir", kbc.Dir, @@ -113,4 +124,39 @@ func ReGenerateProject(kbc *utils.TestContext) { filepath.Join(kbc.Dir, "testdir2", "PROJECT"), multiGroup) ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected group") + var APIGroup = "group: crew" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIGroup) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected kind") + var APIKind = "kind: Captain" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIKind) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected version") + var APIVersion = "version: v1" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIVersion) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected namespaced") + var namespaced = "namespaced: true" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), namespaced) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected controller") + var controller = "controller: true" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), controller) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) }