From 8600de17bb87f71ba807a9cec617b5cc9043f256 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Tue, 23 Nov 2021 16:32:49 +0100 Subject: [PATCH] should work again (hopefully) --- api/v1alpha3/suite_test.go | 17 +++- api/v1beta1/webhooks.go | 42 +++++++++ bootstrap/kubeadm/api/v1alpha3/suite_test.go | 9 +- bootstrap/kubeadm/api/v1beta1/webhooks.go | 33 +++++++ .../internal/controllers/suite_test.go | 4 +- bootstrap/kubeadm/main.go | 8 -- bootstrap/util/suite_test.go | 4 +- controllers/remote/suite_test.go | 4 +- controllers/suite_test.go | 4 +- controllers/topology/suite_test.go | 5 +- .../kubeadm/api/v1alpha3/suite_test.go | 4 +- controlplane/kubeadm/api/v1beta1/webhooks.go | 33 +++++++ .../kubeadm/controllers/suite_test.go | 4 +- controlplane/kubeadm/internal/suite_test.go | 4 +- exp/addons/api/v1alpha3/suite_test.go | 13 ++- exp/addons/api/v1beta1/webhooks.go | 30 +++++++ exp/addons/internal/controllers/suite_test.go | 6 +- exp/api/v1alpha3/suite_test.go | 13 ++- .../api/v1beta1/webhooks.go | 11 ++- exp/controllers/suite_test.go | 4 +- internal/envtest/environment.go | 8 +- internal/envtest/setup/doc.go | 19 ++++ internal/envtest/setup/webhook.go | 55 ++++++++++++ internal/envtest/webhooks/webhooks.go | 86 ------------------- internal/webhooks/clusterclass_test.go | 2 - internal/webhooks/suite_test.go | 45 ++++++++++ internal/webhooks/webhooks.go | 39 +++++++++ util/collections/suite_test.go | 4 +- util/patch/suite_test.go | 4 +- .../webhooks.go | 10 +-- 30 files changed, 376 insertions(+), 148 deletions(-) create mode 100644 api/v1beta1/webhooks.go create mode 100644 bootstrap/kubeadm/api/v1beta1/webhooks.go create mode 100644 controlplane/kubeadm/api/v1beta1/webhooks.go create mode 100644 exp/addons/api/v1beta1/webhooks.go rename bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplatelist_webhook.go => exp/api/v1beta1/webhooks.go (67%) create mode 100644 internal/envtest/setup/doc.go create mode 100644 internal/envtest/setup/webhook.go delete mode 100644 internal/envtest/webhooks/webhooks.go create mode 100644 internal/webhooks/suite_test.go create mode 100644 internal/webhooks/webhooks.go rename bootstrap/kubeadm/api/v1beta1/kubeadmconfiglist_webhook.go => webhooks/webhooks.go (72%) diff --git a/api/v1alpha3/suite_test.go b/api/v1alpha3/suite_test.go index 1276567c2e03..e1dcbdb13611 100644 --- a/api/v1alpha3/suite_test.go +++ b/api/v1alpha3/suite_test.go @@ -17,14 +17,16 @@ limitations under the License. package v1alpha3 import ( + "fmt" "os" "testing" // +kubebuilder:scaffold:imports utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/webhooks" ctrl "sigs.k8s.io/controller-runtime" ) @@ -37,8 +39,15 @@ func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) os.Exit(envtest.Run(ctx, envtest.RunInput{ - M: m, - SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupCoreWebhooksWithManager, + M: m, + SetupEnv: func(e *envtest.Environment) { env = e }, + SetupWebhooks: func(mgr ctrl.Manager) { + if err := clusterv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("Failed to set up webhooks: %v", err)) + } + if err := webhooks.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + }, })) } diff --git a/api/v1beta1/webhooks.go b/api/v1beta1/webhooks.go new file mode 100644 index 000000000000..16685edaf7bb --- /dev/null +++ b/api/v1beta1/webhooks.go @@ -0,0 +1,42 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta1 + +import ( + "github.com/pkg/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + if err := (&Machine{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create Machine webhook") + } + + if err := (&MachineSet{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create MachineSet webhook") + } + + if err := (&MachineDeployment{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create MachineDeployment webhook") + } + + if err := (&MachineHealthCheck{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create MachineHealthCheck webhook") + } + return nil +} diff --git a/bootstrap/kubeadm/api/v1alpha3/suite_test.go b/bootstrap/kubeadm/api/v1alpha3/suite_test.go index 53a9c9dc6015..f608abb05a49 100644 --- a/bootstrap/kubeadm/api/v1alpha3/suite_test.go +++ b/bootstrap/kubeadm/api/v1alpha3/suite_test.go @@ -17,14 +17,15 @@ limitations under the License. package v1alpha3 import ( + "fmt" "os" "testing" // +kubebuilder:scaffold:imports utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" + kubeadmbootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" ctrl "sigs.k8s.io/controller-runtime" ) @@ -39,6 +40,10 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupCABPKWebhooksWithManager, + SetupWebhooks: func(mgr ctrl.Manager) { + if err := kubeadmbootstrapv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("Failed to set up webhooks: %v", err)) + } + }, })) } diff --git a/bootstrap/kubeadm/api/v1beta1/webhooks.go b/bootstrap/kubeadm/api/v1beta1/webhooks.go new file mode 100644 index 000000000000..cd089eaaa273 --- /dev/null +++ b/bootstrap/kubeadm/api/v1beta1/webhooks.go @@ -0,0 +1,33 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta1 + +import ( + "github.com/pkg/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + if err := (&KubeadmConfig{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create KubeadmConfig webhook") + } + if err := (&KubeadmConfigTemplate{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create KubeadmConfigTemplate webhook") + } + return nil +} diff --git a/bootstrap/kubeadm/internal/controllers/suite_test.go b/bootstrap/kubeadm/internal/controllers/suite_test.go index 9014ff5c95f0..68957101f4be 100644 --- a/bootstrap/kubeadm/internal/controllers/suite_test.go +++ b/bootstrap/kubeadm/internal/controllers/suite_test.go @@ -22,7 +22,7 @@ import ( // +kubebuilder:scaffold:imports "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -35,6 +35,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/bootstrap/kubeadm/main.go b/bootstrap/kubeadm/main.go index fb7a7d6bf174..fc13c290dc7e 100644 --- a/bootstrap/kubeadm/main.go +++ b/bootstrap/kubeadm/main.go @@ -216,18 +216,10 @@ func setupWebhooks(mgr ctrl.Manager) { setupLog.Error(err, "unable to create webhook", "webhook", "KubeadmConfig") os.Exit(1) } - if err := (&kubeadmbootstrapv1.KubeadmConfigList{}).SetupWebhookWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create webhook", "webhook", "KubeadmConfigList") - os.Exit(1) - } if err := (&kubeadmbootstrapv1.KubeadmConfigTemplate{}).SetupWebhookWithManager(mgr); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "KubeadmConfigTemplate") os.Exit(1) } - if err := (&kubeadmbootstrapv1.KubeadmConfigTemplateList{}).SetupWebhookWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create webhook", "webhook", "KubeadmConfigTemplateList") - os.Exit(1) - } } func concurrency(c int) controller.Options { diff --git a/bootstrap/util/suite_test.go b/bootstrap/util/suite_test.go index 46f362b2cdc6..bc6176bd9b2a 100644 --- a/bootstrap/util/suite_test.go +++ b/bootstrap/util/suite_test.go @@ -22,7 +22,7 @@ import ( // +kubebuilder:scaffold:imports "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -35,6 +35,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/controllers/remote/suite_test.go b/controllers/remote/suite_test.go index 91d111f047b0..db6a790d36d5 100644 --- a/controllers/remote/suite_test.go +++ b/controllers/remote/suite_test.go @@ -23,7 +23,7 @@ import ( // +kubebuilder:scaffold:imports "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -40,6 +40,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/controllers/suite_test.go b/controllers/suite_test.go index fcfe64b14129..7a493fc183c7 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -36,7 +36,7 @@ import ( "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/controllers/remote" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller" ) @@ -128,7 +128,7 @@ func TestMain(m *testing.M) { M: m, SetupEnv: func(e *envtest.Environment) { env = e }, SetupIndexes: setupIndexes, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, SetupReconcilers: setupReconcilers, })) } diff --git a/controllers/topology/suite_test.go b/controllers/topology/suite_test.go index 3ec28248f129..0723f01a5dae 100644 --- a/controllers/topology/suite_test.go +++ b/controllers/topology/suite_test.go @@ -30,7 +30,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -53,6 +53,7 @@ func TestMain(m *testing.M) { panic(fmt.Sprintf("unable to setup index: %v", err)) } } + setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) { unstructuredCachingClient, err := client.NewDelegatingClient( client.NewDelegatingClientInput{ @@ -88,7 +89,7 @@ func TestMain(m *testing.M) { ManagerUncachedObjs: []client.Object{}, SetupEnv: func(e *envtest.Environment) { env = e }, SetupIndexes: setupIndexes, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, SetupReconcilers: setupReconcilers, })) } diff --git a/controlplane/kubeadm/api/v1alpha3/suite_test.go b/controlplane/kubeadm/api/v1alpha3/suite_test.go index 5bb55c483cb9..b42235ecb16a 100644 --- a/controlplane/kubeadm/api/v1alpha3/suite_test.go +++ b/controlplane/kubeadm/api/v1alpha3/suite_test.go @@ -24,7 +24,7 @@ import ( utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -39,6 +39,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupKCPWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/controlplane/kubeadm/api/v1beta1/webhooks.go b/controlplane/kubeadm/api/v1beta1/webhooks.go new file mode 100644 index 000000000000..0712908c6654 --- /dev/null +++ b/controlplane/kubeadm/api/v1beta1/webhooks.go @@ -0,0 +1,33 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta1 + +import ( + "github.com/pkg/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + if err := (&KubeadmControlPlane{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create KubeadmControlPlane webhook") + } + if err := (&KubeadmControlPlaneTemplate{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create KubeadmControlPlaneTemplate webhook") + } + return nil +} diff --git a/controlplane/kubeadm/controllers/suite_test.go b/controlplane/kubeadm/controllers/suite_test.go index c5989d85cce8..444343c3b560 100644 --- a/controlplane/kubeadm/controllers/suite_test.go +++ b/controlplane/kubeadm/controllers/suite_test.go @@ -24,7 +24,7 @@ import ( apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -56,6 +56,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/controlplane/kubeadm/internal/suite_test.go b/controlplane/kubeadm/internal/suite_test.go index 100023eae277..28d18843a868 100644 --- a/controlplane/kubeadm/internal/suite_test.go +++ b/controlplane/kubeadm/internal/suite_test.go @@ -21,7 +21,7 @@ import ( "testing" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -34,6 +34,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/exp/addons/api/v1alpha3/suite_test.go b/exp/addons/api/v1alpha3/suite_test.go index 1276567c2e03..05c58c805b11 100644 --- a/exp/addons/api/v1alpha3/suite_test.go +++ b/exp/addons/api/v1alpha3/suite_test.go @@ -17,14 +17,15 @@ limitations under the License. package v1alpha3 import ( + "fmt" "os" "testing" // +kubebuilder:scaffold:imports utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" + addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" ctrl "sigs.k8s.io/controller-runtime" ) @@ -37,8 +38,12 @@ func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) os.Exit(envtest.Run(ctx, envtest.RunInput{ - M: m, - SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupCoreWebhooksWithManager, + M: m, + SetupEnv: func(e *envtest.Environment) { env = e }, + SetupWebhooks: func(mgr ctrl.Manager) { + if err := addonsv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("Failed to set up webhooks: %v", err)) + } + }, })) } diff --git a/exp/addons/api/v1beta1/webhooks.go b/exp/addons/api/v1beta1/webhooks.go new file mode 100644 index 000000000000..acf4a3ee2c51 --- /dev/null +++ b/exp/addons/api/v1beta1/webhooks.go @@ -0,0 +1,30 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1beta1 + +import ( + "github.com/pkg/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + if err := (&ClusterResourceSet{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create Machine webhook") + } + return nil +} diff --git a/exp/addons/internal/controllers/suite_test.go b/exp/addons/internal/controllers/suite_test.go index 2f31920b23c7..981b77760157 100644 --- a/exp/addons/internal/controllers/suite_test.go +++ b/exp/addons/internal/controllers/suite_test.go @@ -26,9 +26,9 @@ import ( corev1 "k8s.io/api/core/v1" "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/controllers/remote" - v1alpha4 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" + "sigs.k8s.io/cluster-api/exp/addons/api/v1alpha4" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -76,7 +76,7 @@ func TestMain(m *testing.M) { &v1alpha4.ClusterResourceSetBinding{}, }, SetupIndexes: setupIndexes, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, SetupReconcilers: setupReconcilers, })) } diff --git a/exp/api/v1alpha3/suite_test.go b/exp/api/v1alpha3/suite_test.go index 1276567c2e03..1e09176b11df 100644 --- a/exp/api/v1alpha3/suite_test.go +++ b/exp/api/v1alpha3/suite_test.go @@ -17,14 +17,15 @@ limitations under the License. package v1alpha3 import ( + "fmt" "os" "testing" // +kubebuilder:scaffold:imports utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes/scheme" + expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" ctrl "sigs.k8s.io/controller-runtime" ) @@ -37,8 +38,12 @@ func TestMain(m *testing.M) { utilruntime.Must(AddToScheme(scheme.Scheme)) os.Exit(envtest.Run(ctx, envtest.RunInput{ - M: m, - SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupCoreWebhooksWithManager, + M: m, + SetupEnv: func(e *envtest.Environment) { env = e }, + SetupWebhooks: func(mgr ctrl.Manager) { + if err := expv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("Failed to set up webhooks: %v", err)) + } + }, })) } diff --git a/bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplatelist_webhook.go b/exp/api/v1beta1/webhooks.go similarity index 67% rename from bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplatelist_webhook.go rename to exp/api/v1beta1/webhooks.go index 16970ec717b2..104ee0715e0e 100644 --- a/bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplatelist_webhook.go +++ b/exp/api/v1beta1/webhooks.go @@ -17,11 +17,14 @@ limitations under the License. package v1beta1 import ( + "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" ) -func (r *KubeadmConfigTemplateList) SetupWebhookWithManager(mgr ctrl.Manager) error { - return ctrl.NewWebhookManagedBy(mgr). - For(r). - Complete() +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + if err := (&MachinePool{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create Machine webhook") + } + return nil } diff --git a/exp/controllers/suite_test.go b/exp/controllers/suite_test.go index a68deff6c0bc..33cbe1cd47c2 100644 --- a/exp/controllers/suite_test.go +++ b/exp/controllers/suite_test.go @@ -25,7 +25,7 @@ import ( // +kubebuilder:scaffold:imports "sigs.k8s.io/cluster-api/api/v1beta1/index" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller" ) @@ -57,7 +57,7 @@ func TestMain(m *testing.M) { M: m, SetupEnv: func(e *envtest.Environment) { env = e }, SetupIndexes: setupIndexes, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, SetupReconcilers: setupReconcilers, })) } diff --git a/internal/envtest/environment.go b/internal/envtest/environment.go index 7d7de92837f3..1010df057f03 100644 --- a/internal/envtest/environment.go +++ b/internal/envtest/environment.go @@ -68,6 +68,9 @@ func init() { klog.SetOutput(ginkgo.GinkgoWriter) // Calculate the scheme. + // FIXME(tbd): if we want to enable envtests in those packages too we + // have to move those schema registrations to the setup package too + // TBD if we want that (for our current case it's enough to enable envtests for webhooks/) utilruntime.Must(apiextensionsv1.AddToScheme(scheme.Scheme)) utilruntime.Must(admissionv1.AddToScheme(scheme.Scheme)) utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme)) @@ -106,7 +109,7 @@ func Run(ctx context.Context, input RunInput) int { input.SetupIndexes(ctx, env.Manager) } if input.SetupWebhooks != nil { - input.SetupWebhooks(ctx, env.Manager) + input.SetupWebhooks(env.Manager) } if input.SetupReconcilers != nil { input.SetupReconcilers(ctx, env.Manager) @@ -216,9 +219,6 @@ func newEnvironment(uncachedObjs ...client.Object) *Environment { klog.Fatalf("Failed to start testenv manager: %v", err) } - // Set minNodeStartupTimeout for Test, so it does not need to be at least 30s - clusterv1.SetMinNodeStartupTimeout(metav1.Duration{Duration: 1 * time.Millisecond}) - return &Environment{ Manager: mgr, Client: mgr.GetClient(), diff --git a/internal/envtest/setup/doc.go b/internal/envtest/setup/doc.go new file mode 100644 index 000000000000..fbc8769d1c05 --- /dev/null +++ b/internal/envtest/setup/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package setup contains helper to set up for envtest. +// FIXME: add something about circular dependencies. +package setup diff --git a/internal/envtest/setup/webhook.go b/internal/envtest/setup/webhook.go new file mode 100644 index 000000000000..f6dc6141aa99 --- /dev/null +++ b/internal/envtest/setup/webhook.go @@ -0,0 +1,55 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package setup + +import ( + "fmt" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + kubeadmbootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" + kcpv1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" + addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" + expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" + "sigs.k8s.io/cluster-api/webhooks" + ctrl "sigs.k8s.io/controller-runtime" +) + +// AllWebhooksWithManager sets up all webhooks. +func AllWebhooksWithManager(mgr ctrl.Manager) { + if err := clusterv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + // Set minNodeStartupTimeout for Test, so it does not need to be at least 30s + clusterv1.SetMinNodeStartupTimeout(metav1.Duration{Duration: 1 * time.Millisecond}) + if err := webhooks.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + if err := expv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + if err := addonsv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + if err := kubeadmbootstrapv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } + if err := kcpv1.SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("failed to set up webhooks: %v", err)) + } +} diff --git a/internal/envtest/webhooks/webhooks.go b/internal/envtest/webhooks/webhooks.go deleted file mode 100644 index d8fd2a0f81fc..000000000000 --- a/internal/envtest/webhooks/webhooks.go +++ /dev/null @@ -1,86 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package webhooks - -import ( - "k8s.io/klog/v2" - clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" - bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" - kcpv1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1" - addonv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1" - expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" - "sigs.k8s.io/cluster-api/webhooks" - ctrl "sigs.k8s.io/controller-runtime" -) - -// SetupAllWebhooksWithManager sets up all webhooks. -func SetupAllWebhooksWithManager(mgr ctrl.Manager) { - SetupCoreWebhooksWithManager(mgr) - SetupCABPKWebhooksWithManager(mgr) - SetupKCPWebhooksWithManager(mgr) -} - -// SetupCoreWebhooksWithManager sets up webhooks of the core provider. -func SetupCoreWebhooksWithManager(mgr ctrl.Manager) { - if err := (&webhooks.Cluster{Client: mgr.GetClient()}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&webhooks.ClusterClass{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&clusterv1.Machine{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&clusterv1.MachineHealthCheck{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&clusterv1.Machine{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&clusterv1.MachineSet{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&clusterv1.MachineDeployment{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&addonv1.ClusterResourceSet{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook for crs: %+v", err) - } - if err := (&expv1.MachinePool{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook for machinepool: %+v", err) - } -} - -// SetupCABPKWebhooksWithManager sets up webhooks of the CABPK provider. -func SetupCABPKWebhooksWithManager(mgr ctrl.Manager) { - if err := (&bootstrapv1.KubeadmConfig{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&bootstrapv1.KubeadmConfigTemplate{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } - if err := (&bootstrapv1.KubeadmConfigTemplateList{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } -} - -// SetupKCPWebhooksWithManager sets up webhooks of the KCP provider. -func SetupKCPWebhooksWithManager(mgr ctrl.Manager) { - if err := (&kcpv1.KubeadmControlPlane{}).SetupWebhookWithManager(mgr); err != nil { - klog.Fatalf("unable to create webhook: %+v", err) - } -} diff --git a/internal/webhooks/clusterclass_test.go b/internal/webhooks/clusterclass_test.go index baa91a577c6a..54ad3b9230ee 100644 --- a/internal/webhooks/clusterclass_test.go +++ b/internal/webhooks/clusterclass_test.go @@ -27,11 +27,9 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/feature" "sigs.k8s.io/cluster-api/internal/builder" - ctrl "sigs.k8s.io/controller-runtime" ) var ( - ctx = ctrl.SetupSignalHandler() fakeScheme = runtime.NewScheme() ) diff --git a/internal/webhooks/suite_test.go b/internal/webhooks/suite_test.go new file mode 100644 index 000000000000..8f676aa7b920 --- /dev/null +++ b/internal/webhooks/suite_test.go @@ -0,0 +1,45 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package webhooks + +import ( + "fmt" + "os" + "testing" + + // +kubebuilder:scaffold:imports + "sigs.k8s.io/cluster-api/internal/envtest" + ctrl "sigs.k8s.io/controller-runtime" +) + +var ( + env *envtest.Environment + ctx = ctrl.SetupSignalHandler() +) + +// FIXME(sbueringer): just part of this PR to prove that it works. +func TestMain(m *testing.M) { + os.Exit(envtest.Run(ctx, envtest.RunInput{ + M: m, + SetupEnv: func(e *envtest.Environment) { env = e }, + SetupWebhooks: func(mgr ctrl.Manager) { + if err := SetupWebhooksWithManager(mgr); err != nil { + panic(fmt.Sprintf("Failed to set up webhooks: %v", err)) + } + }, + })) +} diff --git a/internal/webhooks/webhooks.go b/internal/webhooks/webhooks.go new file mode 100644 index 000000000000..072274a84037 --- /dev/null +++ b/internal/webhooks/webhooks.go @@ -0,0 +1,39 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package webhooks + +import ( + "github.com/pkg/errors" + ctrl "sigs.k8s.io/controller-runtime" +) + +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + // NOTE: ClusterClass and managed topologies are behind ClusterTopology feature gate flag; the webhook + // is going to prevent creating or updating new objects in case the feature flag is disabled. + if err := (&ClusterClass{}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create ClusterClass webhook") + } + + // NOTE: ClusterClass and managed topologies are behind ClusterTopology feature gate flag; the webhook + // is going to prevent usage of Cluster.Topology in case the feature flag is disabled. + if err := (&Cluster{Client: mgr.GetClient()}).SetupWebhookWithManager(mgr); err != nil { + return errors.Wrapf(err, "failed to create Cluster webhook") + } + + return nil +} diff --git a/util/collections/suite_test.go b/util/collections/suite_test.go index ded6d504e57f..2df7d2acb87a 100644 --- a/util/collections/suite_test.go +++ b/util/collections/suite_test.go @@ -21,7 +21,7 @@ import ( "testing" "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -34,6 +34,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/util/patch/suite_test.go b/util/patch/suite_test.go index 999df5204725..d6f92c2b2fc1 100644 --- a/util/patch/suite_test.go +++ b/util/patch/suite_test.go @@ -23,7 +23,7 @@ import ( // +kubebuilder:scaffold:imports "sigs.k8s.io/cluster-api/internal/envtest" - "sigs.k8s.io/cluster-api/internal/envtest/webhooks" + "sigs.k8s.io/cluster-api/internal/envtest/setup" ctrl "sigs.k8s.io/controller-runtime" ) @@ -40,6 +40,6 @@ func TestMain(m *testing.M) { os.Exit(envtest.Run(ctx, envtest.RunInput{ M: m, SetupEnv: func(e *envtest.Environment) { env = e }, - SetupWebhooks: webhooks.SetupAllWebhooksWithManager, + SetupWebhooks: setup.AllWebhooksWithManager, })) } diff --git a/bootstrap/kubeadm/api/v1beta1/kubeadmconfiglist_webhook.go b/webhooks/webhooks.go similarity index 72% rename from bootstrap/kubeadm/api/v1beta1/kubeadmconfiglist_webhook.go rename to webhooks/webhooks.go index 7771eb653895..c76930703c49 100644 --- a/bootstrap/kubeadm/api/v1beta1/kubeadmconfiglist_webhook.go +++ b/webhooks/webhooks.go @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -package v1beta1 +package webhooks import ( + "sigs.k8s.io/cluster-api/internal/webhooks" ctrl "sigs.k8s.io/controller-runtime" ) -func (r *KubeadmConfigList) SetupWebhookWithManager(mgr ctrl.Manager) error { - return ctrl.NewWebhookManagedBy(mgr). - For(r). - Complete() +// SetupWebhooksWithManager sets up all webhooks of the current package. +func SetupWebhooksWithManager(mgr ctrl.Manager) error { + return webhooks.SetupWebhooksWithManager(mgr) }