Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 🌱 Enable webhook envtests #5722

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +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/webhooks"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -38,5 +41,13 @@ 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 := 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))
}
},
}))
}
42 changes: 42 additions & 0 deletions api/v1beta1/webhooks.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member Author

@sbueringer sbueringer Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't necessarily need the package level functions to avoid the circular dependencies, but they would avoid some repetition (otherwise we would have to inline those funcs, aka we would need the full list of resources in all those cases)

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
}
7 changes: 7 additions & 0 deletions bootstrap/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ 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"
ctrl "sigs.k8s.io/controller-runtime"
)
Expand All @@ -38,5 +40,10 @@ 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 := kubeadmbootstrapv1.SetupWebhooksWithManager(mgr); err != nil {
panic(fmt.Sprintf("Failed to set up webhooks: %v", err))
}
},
}))
}
33 changes: 33 additions & 0 deletions bootstrap/kubeadm/api/v1beta1/webhooks.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions bootstrap/kubeadm/internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

// +kubebuilder:scaffold:imports
"sigs.k8s.io/cluster-api/internal/envtest"
"sigs.k8s.io/cluster-api/internal/envtest/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -32,7 +33,8 @@ var (

func TestMain(m *testing.M) {
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
8 changes: 0 additions & 8 deletions bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,10 @@ func setupWebhooks(mgr ctrl.Manager) {
setupLog.Error(err, "unable to create webhook", "webhook", "KubeadmConfig")
os.Exit(1)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to register those webhooks (we don't register any other List webhooks)

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 {
Expand Down
6 changes: 4 additions & 2 deletions bootstrap/util/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

// +kubebuilder:scaffold:imports
"sigs.k8s.io/cluster-api/internal/envtest"
"sigs.k8s.io/cluster-api/internal/envtest/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -32,7 +33,8 @@ var (

func TestMain(m *testing.M) {
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
6 changes: 4 additions & 2 deletions controllers/remote/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

// +kubebuilder:scaffold:imports
"sigs.k8s.io/cluster-api/internal/envtest"
"sigs.k8s.io/cluster-api/internal/envtest/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -37,7 +38,8 @@ var (

func TestMain(m *testing.M) {
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
2 changes: 2 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +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/setup"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
)
Expand Down Expand Up @@ -127,6 +128,7 @@ func TestMain(m *testing.M) {
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupIndexes: setupIndexes,
SetupWebhooks: setup.AllWebhooksWithManager,
SetupReconcilers: setupReconcilers,
}))
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/topology/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +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/setup"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -52,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{
Expand Down Expand Up @@ -87,6 +89,7 @@ func TestMain(m *testing.M) {
ManagerUncachedObjs: []client.Object{},
SetupEnv: func(e *envtest.Environment) { env = e },
SetupIndexes: setupIndexes,
SetupWebhooks: setup.AllWebhooksWithManager,
SetupReconcilers: setupReconcilers,
}))
}
6 changes: 4 additions & 2 deletions controlplane/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -36,7 +37,8 @@ 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 },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
33 changes: 33 additions & 0 deletions controlplane/kubeadm/api/v1beta1/webhooks.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions controlplane/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand Down Expand Up @@ -53,7 +54,8 @@ var (

func TestMain(m *testing.M) {
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
6 changes: 4 additions & 2 deletions controlplane/kubeadm/internal/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"sigs.k8s.io/cluster-api/internal/envtest"
"sigs.k8s.io/cluster-api/internal/envtest/setup"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -31,7 +32,8 @@ var (

func TestMain(m *testing.M) {
os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: setup.AllWebhooksWithManager,
}))
}
7 changes: 7 additions & 0 deletions exp/addons/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ 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"
ctrl "sigs.k8s.io/controller-runtime"
)
Expand All @@ -38,5 +40,10 @@ 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 := addonsv1.SetupWebhooksWithManager(mgr); err != nil {
panic(fmt.Sprintf("Failed to set up webhooks: %v", err))
}
},
}))
}
30 changes: 30 additions & 0 deletions exp/addons/api/v1beta1/webhooks.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading