Skip to content

Commit

Permalink
🐛 (go/v4): Start to check ENABLE_WEBHOOKS envvar into main.go to fix …
Browse files Browse the repository at this point in the history
…issue when webhooks are used (#3578)

🐛 (go/v4): Start to check ENABLE_WEBHOOKS envvar into main.go to fix issue when webhooks are used (#3578)
  • Loading branch information
lowang-bh authored Sep 4, 2023
1 parent 37027a3 commit a8ad43f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 47 deletions.
8 changes: 0 additions & 8 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ CronJob controller's`+" `"+`SetupWithManager`+"`"+` method.
}`, `
// +kubebuilder:docs-gen:collapse=old stuff`)
CheckError("fixing main.go", err)

err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "cmd/main.go"),
` if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)
}`, `}`)
CheckError("fixing main.go", err)
}

func updateWebhook(sp *Sample) {
Expand Down
7 changes: 1 addition & 6 deletions hack/docs/internal/cronjob-tutorial/main_revisited.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,4 @@ const MainEnableWebhook = `
locally, we'll put them behind an environment variable.
We'll just make sure to set` + " `" + `ENABLE_WEBHOOKS=false` + "`" + ` when we run locally.
*/
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)
}`
*/`
8 changes: 5 additions & 3 deletions pkg/plugins/golang/v4/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ const (
os.Exit(1)
}
`
webhookSetupCodeFragment = `if err = (&%s.%s{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "%s")
os.Exit(1)
webhookSetupCodeFragment = `if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&%s.%s{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "%s")
os.Exit(1)
}
}
`
)
Expand Down
48 changes: 30 additions & 18 deletions testdata/project-v4-multigroup/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
os.Exit(1)
}
}
if err = (&shipcontroller.FrigateReconciler{
Client: mgr.GetClient(),
Expand All @@ -135,9 +137,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Frigate")
os.Exit(1)
}
if err = (&shipv1beta1.Frigate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Frigate")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&shipv1beta1.Frigate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Frigate")
os.Exit(1)
}
}
if err = (&shipcontroller.DestroyerReconciler{
Client: mgr.GetClient(),
Expand All @@ -146,9 +150,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Destroyer")
os.Exit(1)
}
if err = (&shipv1.Destroyer{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Destroyer")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&shipv1.Destroyer{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Destroyer")
os.Exit(1)
}
}
if err = (&shipcontroller.CruiserReconciler{
Client: mgr.GetClient(),
Expand All @@ -157,9 +163,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Cruiser")
os.Exit(1)
}
if err = (&shipv2alpha1.Cruiser{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Cruiser")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&shipv2alpha1.Cruiser{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Cruiser")
os.Exit(1)
}
}
if err = (&seacreaturescontroller.KrakenReconciler{
Client: mgr.GetClient(),
Expand Down Expand Up @@ -210,9 +218,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Lakers")
os.Exit(1)
}
if err = (&testprojectorgv1.Lakers{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Lakers")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&testprojectorgv1.Lakers{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Lakers")
os.Exit(1)
}
}
if err = (&examplecomcontroller.MemcachedReconciler{
Client: mgr.GetClient(),
Expand All @@ -230,9 +240,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Busybox")
os.Exit(1)
}
if err = (&examplecomv1alpha1.Memcached{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&examplecomv1alpha1.Memcached{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

Expand Down
8 changes: 5 additions & 3 deletions testdata/project-v4-with-deploy-image/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Busybox")
os.Exit(1)
}
if err = (&examplecomv1alpha1.Memcached{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&examplecomv1alpha1.Memcached{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

Expand Down
24 changes: 15 additions & 9 deletions testdata/project-v4/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Captain")
os.Exit(1)
}
if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&crewv1.Captain{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
os.Exit(1)
}
}
if err = (&controller.FirstMateReconciler{
Client: mgr.GetClient(),
Expand All @@ -107,9 +109,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "FirstMate")
os.Exit(1)
}
if err = (&crewv1.FirstMate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "FirstMate")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&crewv1.FirstMate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "FirstMate")
os.Exit(1)
}
}
if err = (&controller.AdmiralReconciler{
Client: mgr.GetClient(),
Expand All @@ -118,9 +122,11 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "Admiral")
os.Exit(1)
}
if err = (&crewv1.Admiral{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Admiral")
os.Exit(1)
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&crewv1.Admiral{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Admiral")
os.Exit(1)
}
}
if err = (&controller.LakerReconciler{
Client: mgr.GetClient(),
Expand Down

0 comments on commit a8ad43f

Please sign in to comment.