Skip to content

Commit

Permalink
Merge pull request #3497 from yyy1000/create-webhook
Browse files Browse the repository at this point in the history
✨ Add Webhooks in alpha generate subcommand
  • Loading branch information
k8s-ci-robot authored Jul 14, 2023
2 parents fc50728 + 38ee2c9 commit 8b0a5b8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
52 changes: 42 additions & 10 deletions pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func kubebuilderCreate(store store.Store) error {
if err = createAPI(r); err != nil {
return err
}
if err = createWebhook(r); err != nil {
return err
}
}

return nil
Expand All @@ -153,18 +156,12 @@ 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 {
func getGVKFlags(resource resource.Resource) []string {
var args []string

if len(resource.Plural) > 0 {
args = append(args, "--plural", resource.Plural)
}
if len(resource.Group) > 0 {
args = append(args, "--group", resource.Group)
}
Expand All @@ -177,6 +174,15 @@ func getAPIGVKFlags(resource resource.Resource) []string {
return args
}

func createAPI(resource resource.Resource) error {
var args []string
args = append(args, "create")
args = append(args, "api")
args = append(args, getGVKFlags(resource)...)
args = append(args, getAPIResourceFlags(resource)...)
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
}

func getAPIResourceFlags(resource resource.Resource) []string {
var args []string
if resource.API == nil || resource.API.IsEmpty() {
Expand All @@ -196,3 +202,29 @@ func getAPIResourceFlags(resource resource.Resource) []string {
}
return args
}

func createWebhook(resource resource.Resource) error {
if resource.Webhooks == nil || resource.Webhooks.IsEmpty() {
return nil
}
var args []string
args = append(args, "create")
args = append(args, "webhook")
args = append(args, getGVKFlags(resource)...)
args = append(args, getWebhookResourceFlags(resource)...)
return util.RunCmd("kubebuilder create webhook", "kubebuilder", args...)
}

func getWebhookResourceFlags(resource resource.Resource) []string {
var args []string
if resource.HasConversionWebhook() {
args = append(args, "--conversion")
}
if resource.HasValidationWebhook() {
args = append(args, "--programmatic-validation")
}
if resource.HasDefaultingWebhook() {
args = append(args, "--defaulting")
}
return args
}
19 changes: 19 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ func ReGenerateProject(kbc *utils.TestContext) {
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("create Webhooks with conversion and validating webhook")
err = kbc.CreateWebhook(
"--group", "crew",
"--version", "v1",
"--kind", "Captain",
"--programmatic-validation",
"--conversion",
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("regenerating the project at another output directory")
err = kbc.Regenerate(
"--input-dir", kbc.Dir,
Expand Down Expand Up @@ -159,4 +169,13 @@ func ReGenerateProject(kbc *utils.TestContext) {
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), controller)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())

By("checking if the project file was generated with the expected webhook")
var webhook = `webhooks:
conversion: true
validation: true`
fileContainsExpr, err = pluginutil.HasFileContentWith(
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), webhook)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
}

0 comments on commit 8b0a5b8

Please sign in to comment.