Skip to content

Commit

Permalink
should work again (hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Nov 23, 2021
1 parent 8fc9721 commit dd48432
Show file tree
Hide file tree
Showing 30 changed files with 404 additions and 148 deletions.
13 changes: 9 additions & 4 deletions api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/envtest"
"sigs.k8s.io/cluster-api/internal/envtest/webhooks"
ctrl "sigs.k8s.io/controller-runtime"
)

Expand All @@ -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 := clusterv1.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 {
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
}
9 changes: 7 additions & 2 deletions bootstrap/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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))
}
},
}))
}
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
}
36 changes: 34 additions & 2 deletions bootstrap/kubeadm/internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"testing"
"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"

// +kubebuilder:scaffold:imports
"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"
)

Expand All @@ -32,9 +41,32 @@ var (
)

func TestMain(m *testing.M) {
setupWebhooks := func(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))
}
}

os.Exit(envtest.Run(ctx, envtest.RunInput{
M: m,
SetupEnv: func(e *envtest.Environment) { env = e },
SetupWebhooks: webhooks.SetupAllWebhooksWithManager,
SetupWebhooks: setupWebhooks,
}))
}
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)
}
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
4 changes: 2 additions & 2 deletions bootstrap/util/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}))
}
4 changes: 2 additions & 2 deletions controllers/remote/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}))
}
4 changes: 2 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
}))
}
Expand Down
5 changes: 3 additions & 2 deletions controllers/topology/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -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,
}))
}
4 changes: 2 additions & 2 deletions controlplane/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}))
}
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
}
4 changes: 2 additions & 2 deletions controlplane/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
}))
}
4 changes: 2 additions & 2 deletions controlplane/kubeadm/internal/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}))
}
Loading

0 comments on commit dd48432

Please sign in to comment.