Skip to content

Commit

Permalink
feat: add support for create API
Browse files Browse the repository at this point in the history
fix: golang lint

fix: uppercase for Kind

fix:nit

fix: better name
  • Loading branch information
yyy1000 committed Jul 7, 2023
1 parent e7a89ec commit bd9a94f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand All @@ -132,3 +152,44 @@ 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 if resource.API.Namespaced {
args = append(args, "--namespaced")
}

if resource.Controller {
args = append(args, "--controller")
} else {
args = append(args, "--controller=false")
}
return args
}
46 changes: 46 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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())
}

0 comments on commit bd9a94f

Please sign in to comment.