Skip to content

Commit b98880e

Browse files
authored
Merge pull request kubernetes-sigs#9459 from Ankitasw/move-inmemory-infra-webhooks
🌱 Move inmemory infrastructure API v1beta1 webhooks to separate package
2 parents 8cc1fd5 + c8d10b4 commit b98880e

File tree

9 files changed

+175
-45
lines changed

9 files changed

+175
-45
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,12 @@ generate-manifests-docker-infrastructure: $(CONTROLLER_GEN) ## Generate manifest
352352

353353
.PHONY: generate-manifests-in-memory-infrastructure
354354
generate-manifests-in-memory-infrastructure: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for in-memory infrastructure provider
355-
$(MAKE) clean-generated-yaml SRC_DIRS="$(CAPIM_DIR)/config/crd/bases"
355+
$(MAKE) clean-generated-yaml SRC_DIRS="$(CAPIM_DIR)/config/crd/bases,$(CAPIM_DIR)/config/webhook/manifests.yaml"
356356
cd $(CAPIM_DIR); $(CONTROLLER_GEN) \
357357
paths=./ \
358358
paths=./api/... \
359359
paths=./internal/controllers/... \
360+
paths=./internal/webhooks/... \
360361
crd:crdVersions=v1 \
361362
rbac:roleName=manager-role \
362363
output:crd:dir=./config/crd/bases \
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package webhooks implements inmemory infrastructure webhooks.
18+
package webhooks

test/infrastructure/inmemory/api/v1alpha1/inmemorycluster_webhook.go renamed to test/infrastructure/inmemory/internal/webhooks/inmemorycluster_webhook.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,54 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1alpha1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26+
27+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/api/v1alpha1"
2428
)
2529

26-
func (c *InMemoryCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
30+
// InMemoryCluster implements a validating and defaulting webhook for InMemoryCluster.
31+
type InMemoryCluster struct{}
32+
33+
func (webhook *InMemoryCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
2734
return ctrl.NewWebhookManagedBy(mgr).
28-
For(c).
35+
For(&v1alpha1.InMemoryCluster{}).
36+
WithDefaulter(webhook).
37+
WithValidator(webhook).
2938
Complete()
3039
}
3140

3241
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorycluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemoryclusters,versions=v1alpha1,name=default.inmemorycluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3342

34-
var _ webhook.Defaulter = &InMemoryCluster{}
43+
var _ webhook.CustomDefaulter = &InMemoryCluster{}
3544

3645
// Default implements webhook.Defaulter so a webhook will be registered for the type.
37-
func (c *InMemoryCluster) Default() {
38-
46+
func (webhook *InMemoryCluster) Default(_ context.Context, _ runtime.Object) error {
47+
return nil
3948
}
4049

4150
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorycluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemoryclusters,versions=v1alpha1,name=validation.inmemorycluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4251

43-
var _ webhook.Validator = &InMemoryCluster{}
52+
var _ webhook.CustomValidator = &InMemoryCluster{}
4453

4554
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
46-
func (c *InMemoryCluster) ValidateCreate() (admission.Warnings, error) {
55+
func (webhook *InMemoryCluster) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4756
return nil, nil
4857
}
4958

5059
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
51-
func (c *InMemoryCluster) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
60+
func (webhook *InMemoryCluster) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
5261
return nil, nil
5362
}
5463

5564
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
56-
func (c *InMemoryCluster) ValidateDelete() (admission.Warnings, error) {
65+
func (webhook *InMemoryCluster) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5766
return nil, nil
5867
}

test/infrastructure/inmemory/api/v1alpha1/inmemoryclustertemplate_webhook.go renamed to test/infrastructure/inmemory/internal/webhooks/inmemoryclustertemplate_webhook.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,54 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1alpha1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26+
27+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/api/v1alpha1"
2428
)
2529

26-
func (c *InMemoryClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
30+
// InMemoryClusterTemplate implements a validating and defaulting webhook for InMemoryClusterTemplate.
31+
type InMemoryClusterTemplate struct{}
32+
33+
func (webhook *InMemoryClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
2734
return ctrl.NewWebhookManagedBy(mgr).
28-
For(c).
35+
For(&v1alpha1.InMemoryClusterTemplate{}).
36+
WithDefaulter(webhook).
37+
WithValidator(webhook).
2938
Complete()
3039
}
3140

3241
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemoryclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemoryclustertemplates,versions=v1alpha1,name=default.inmemoryclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3342

34-
var _ webhook.Defaulter = &InMemoryClusterTemplate{}
43+
var _ webhook.CustomDefaulter = &InMemoryClusterTemplate{}
3544

3645
// Default implements webhook.Defaulter so a webhook will be registered for the type.
37-
func (c *InMemoryClusterTemplate) Default() {
38-
46+
func (webhook *InMemoryClusterTemplate) Default(_ context.Context, _ runtime.Object) error {
47+
return nil
3948
}
4049

4150
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemoryclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemoryclustertemplates,versions=v1alpha1,name=validation.inmemoryclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4251

43-
var _ webhook.Validator = &InMemoryClusterTemplate{}
52+
var _ webhook.CustomValidator = &InMemoryClusterTemplate{}
4453

4554
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
46-
func (c *InMemoryClusterTemplate) ValidateCreate() (admission.Warnings, error) {
55+
func (webhook *InMemoryClusterTemplate) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4756
return nil, nil
4857
}
4958

5059
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
51-
func (c *InMemoryClusterTemplate) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
60+
func (webhook *InMemoryClusterTemplate) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
5261
return nil, nil
5362
}
5463

5564
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
56-
func (c *InMemoryClusterTemplate) ValidateDelete() (admission.Warnings, error) {
65+
func (webhook *InMemoryClusterTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5766
return nil, nil
5867
}

test/infrastructure/inmemory/api/v1alpha1/inmemorymachine_webhook.go renamed to test/infrastructure/inmemory/internal/webhooks/inmemorymachine_webhook.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,54 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1alpha1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26+
27+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/api/v1alpha1"
2428
)
2529

26-
func (c *InMemoryMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
30+
// InMemoryMachine implements a validating and defaulting webhook for InMemoryMachine.
31+
type InMemoryMachine struct{}
32+
33+
func (webhook *InMemoryMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
2734
return ctrl.NewWebhookManagedBy(mgr).
28-
For(c).
35+
For(&v1alpha1.InMemoryMachine{}).
36+
WithDefaulter(webhook).
37+
WithValidator(webhook).
2938
Complete()
3039
}
3140

3241
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorymachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemorymachines,versions=v1alpha1,name=default.inmemorymachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3342

34-
var _ webhook.Defaulter = &InMemoryMachine{}
43+
var _ webhook.CustomDefaulter = &InMemoryMachine{}
3544

3645
// Default implements webhook.Defaulter so a webhook will be registered for the type.
37-
func (c *InMemoryMachine) Default() {
38-
46+
func (webhook *InMemoryMachine) Default(_ context.Context, _ runtime.Object) error {
47+
return nil
3948
}
4049

4150
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorymachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemorymachines,versions=v1alpha1,name=validation.inmemorymachine.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4251

43-
var _ webhook.Validator = &InMemoryMachine{}
52+
var _ webhook.CustomValidator = &InMemoryMachine{}
4453

4554
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
46-
func (c *InMemoryMachine) ValidateCreate() (admission.Warnings, error) {
55+
func (webhook *InMemoryMachine) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4756
return nil, nil
4857
}
4958

5059
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
51-
func (c *InMemoryMachine) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
60+
func (webhook *InMemoryMachine) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
5261
return nil, nil
5362
}
5463

5564
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
56-
func (c *InMemoryMachine) ValidateDelete() (admission.Warnings, error) {
65+
func (webhook *InMemoryMachine) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5766
return nil, nil
5867
}

test/infrastructure/inmemory/api/v1alpha1/inmemorymachinetemplate_webhook.go renamed to test/infrastructure/inmemory/internal/webhooks/inmemorymachinetemplate_webhook.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,54 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1alpha1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123
ctrl "sigs.k8s.io/controller-runtime"
2224
"sigs.k8s.io/controller-runtime/pkg/webhook"
2325
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
26+
27+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/api/v1alpha1"
2428
)
2529

26-
func (c *InMemoryMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
30+
// InMemoryMachineTemplate implements a validating and defaulting webhook for InMemoryMachineTemplate.
31+
type InMemoryMachineTemplate struct{}
32+
33+
func (webhook *InMemoryMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
2734
return ctrl.NewWebhookManagedBy(mgr).
28-
For(c).
35+
For(&v1alpha1.InMemoryMachineTemplate{}).
36+
WithDefaulter(webhook).
37+
WithValidator(webhook).
2938
Complete()
3039
}
3140

3241
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorymachinetemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemorymachinetemplates,versions=v1alpha1,name=default.inmemorymachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3342

34-
var _ webhook.Defaulter = &InMemoryMachineTemplate{}
43+
var _ webhook.CustomDefaulter = &InMemoryMachineTemplate{}
3544

3645
// Default implements webhook.Defaulter so a webhook will be registered for the type.
37-
func (c *InMemoryMachineTemplate) Default() {
38-
46+
func (webhook *InMemoryMachineTemplate) Default(_ context.Context, _ runtime.Object) error {
47+
return nil
3948
}
4049

4150
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-inmemorymachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=inmemorymachinetemplates,versions=v1alpha1,name=validation.inmemorymachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4251

43-
var _ webhook.Validator = &InMemoryMachineTemplate{}
52+
var _ webhook.CustomValidator = &InMemoryMachineTemplate{}
4453

4554
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
46-
func (c *InMemoryMachineTemplate) ValidateCreate() (admission.Warnings, error) {
55+
func (webhook *InMemoryMachineTemplate) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4756
return nil, nil
4857
}
4958

5059
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
51-
func (c *InMemoryMachineTemplate) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
60+
func (webhook *InMemoryMachineTemplate) ValidateUpdate(_ context.Context, _, _ runtime.Object) (admission.Warnings, error) {
5261
return nil, nil
5362
}
5463

5564
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
56-
func (c *InMemoryMachineTemplate) ValidateDelete() (admission.Warnings, error) {
65+
func (webhook *InMemoryMachineTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5766
return nil, nil
5867
}

test/infrastructure/inmemory/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/internal/cloud"
5252
cloudv1 "sigs.k8s.io/cluster-api/test/infrastructure/inmemory/internal/cloud/api/v1alpha1"
5353
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/internal/server"
54+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/webhooks"
5455
"sigs.k8s.io/cluster-api/util/flags"
5556
"sigs.k8s.io/cluster-api/version"
5657
)
@@ -309,22 +310,22 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
309310
}
310311

311312
func setupWebhooks(mgr ctrl.Manager) {
312-
if err := (&infrav1.InMemoryCluster{}).SetupWebhookWithManager(mgr); err != nil {
313+
if err := (&webhooks.InMemoryCluster{}).SetupWebhookWithManager(mgr); err != nil {
313314
setupLog.Error(err, "unable to create webhook", "webhook", "InMemoryCluster")
314315
os.Exit(1)
315316
}
316317

317-
if err := (&infrav1.InMemoryClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
318+
if err := (&webhooks.InMemoryClusterTemplate{}).SetupWebhookWithManager(mgr); err != nil {
318319
setupLog.Error(err, "unable to create webhook", "webhook", "InMemoryClusterTemplate")
319320
os.Exit(1)
320321
}
321322

322-
if err := (&infrav1.InMemoryMachine{}).SetupWebhookWithManager(mgr); err != nil {
323+
if err := (&webhooks.InMemoryMachine{}).SetupWebhookWithManager(mgr); err != nil {
323324
setupLog.Error(err, "unable to create webhook", "webhook", "InMemoryMachine")
324325
os.Exit(1)
325326
}
326327

327-
if err := (&infrav1.InMemoryMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
328+
if err := (&webhooks.InMemoryMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
328329
setupLog.Error(err, "unable to create webhook", "webhook", "InMemoryMachineTemplate")
329330
os.Exit(1)
330331
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package webhooks implements inmemory infrastructure webhooks.
18+
package webhooks
19+
20+
import (
21+
ctrl "sigs.k8s.io/controller-runtime"
22+
23+
"sigs.k8s.io/cluster-api/test/infrastructure/inmemory/internal/webhooks"
24+
)
25+
26+
// InMemoryCluster implements a validating and defaulting webhook for InMemoryCluster.
27+
type InMemoryCluster struct{}
28+
29+
// SetupWebhookWithManager sets up InMemoryCluster webhooks.
30+
func (webhook *InMemoryCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
31+
return (&webhooks.InMemoryCluster{}).SetupWebhookWithManager(mgr)
32+
}
33+
34+
// InMemoryClusterTemplate implements a validating and defaulting webhook for InMemoryClusterTemplate.
35+
type InMemoryClusterTemplate struct{}
36+
37+
// SetupWebhookWithManager sets up InMemoryClusterTemplate webhooks.
38+
func (webhook *InMemoryClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
39+
return (&webhooks.InMemoryClusterTemplate{}).SetupWebhookWithManager(mgr)
40+
}
41+
42+
// InMemoryMachine implements a validating and defaulting webhook for InMemoryMachine.
43+
type InMemoryMachine struct{}
44+
45+
// SetupWebhookWithManager sets up InMemoryMachine webhooks.
46+
func (webhook *InMemoryMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
47+
return (&webhooks.InMemoryMachine{}).SetupWebhookWithManager(mgr)
48+
}
49+
50+
// InMemoryMachineTemplate implements a validating and defaulting webhook for InMemoryMachineTemplate.
51+
type InMemoryMachineTemplate struct{}
52+
53+
// SetupWebhookWithManager sets up InMemoryMachineTemplate webhooks.
54+
func (webhook *InMemoryMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
55+
return (&webhooks.InMemoryMachineTemplate{}).SetupWebhookWithManager(mgr)
56+
}

0 commit comments

Comments
 (0)