Skip to content

Commit e9b09f6

Browse files
authored
Merge pull request #2296 from laozc/runtime-deps
🌱 Move webhooks out of API packages
2 parents c1c6207 + 3d90222 commit e9b09f6

17 files changed

+520
-364
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
246246
paths=./apis/v1alpha3 \
247247
paths=./apis/v1alpha4 \
248248
paths=./apis/v1beta1 \
249+
paths=./internal/webhooks \
249250
crd:crdVersions=v1 \
250251
output:crd:dir=$(CRD_ROOT) \
251252
output:webhook:dir=$(WEBHOOK_ROOT) \
@@ -455,15 +456,15 @@ setup-envtest: $(SETUP_ENVTEST) ## Set up envtest (download kubebuilder assets)
455456

456457
.PHONY: test
457458
test: $(SETUP_ENVTEST) $(GOVC) ## Run unit tests
458-
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" GOVC_BIN_PATH=$(GOVC) go test -v ./apis/... ./controllers/... ./pkg/... $(TEST_ARGS)
459+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" GOVC_BIN_PATH=$(GOVC) go test -v ./apis/... ./controllers/... ./pkg/... ./internal/... $(TEST_ARGS)
459460

460461
.PHONY: test-verbose
461462
test-verbose: ## Run unit tests with verbose flag
462463
$(MAKE) test TEST_ARGS="$(TEST_ARGS) -v"
463464

464465
.PHONY: test-junit
465466
test-junit: $(SETUP_ENVTEST) $(GOTESTSUM) $(GOVC) ## Run unit tests
466-
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" GOVC_BIN_PATH=$(GOVC) go test -json ./apis/... ./controllers/... ./pkg/... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
467+
set +o errexit; (KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" GOVC_BIN_PATH=$(GOVC) go test -json ./apis/... ./controllers/... ./pkg/... ./internal/... $(TEST_ARGS); echo $$? > $(ARTIFACTS)/junit.exitcode) | tee $(ARTIFACTS)/junit.stdout
467468
$(GOTESTSUM) --junitfile $(ARTIFACTS)/junit.xml --raw-command cat $(ARTIFACTS)/junit.stdout
468469
exit $$(cat $(ARTIFACTS)/junit.exitcode)
469470

apis/v1beta1/vspherefailuredomain_webhook.go

Lines changed: 0 additions & 94 deletions
This file was deleted.

internal/webhooks/doc.go

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 contains webhooks for the infrastructure v1beta1 API group.
18+
package webhooks

apis/v1beta1/webhooks.go renamed to internal/webhooks/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1beta1
17+
package webhooks
1818

1919
import (
2020
apierrors "k8s.io/apimachinery/pkg/api/errors"

apis/v1beta1/vsphereclustertemplate_webhook.go renamed to internal/webhooks/vsphereclustertemplate.go

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

17-
package v1beta1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
"fmt"
2022
"reflect"
2123

24+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2225
"k8s.io/apimachinery/pkg/runtime"
2326
"k8s.io/apimachinery/pkg/util/validation/field"
2427
ctrl "sigs.k8s.io/controller-runtime"
2528
"sigs.k8s.io/controller-runtime/pkg/webhook"
2629
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
30+
31+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
2732
)
2833

29-
func (r *VSphereClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
34+
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-vsphereclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vsphereclustertemplates,versions=v1beta1,name=validation.vsphereclustertemplate.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
35+
36+
// VSphereClusterTemplateWebhook implements a validation and defaulting webhook for VSphereClusterTemplate.
37+
type VSphereClusterTemplateWebhook struct{}
38+
39+
var _ webhook.CustomValidator = &VSphereClusterTemplateWebhook{}
40+
41+
func (webhook *VSphereClusterTemplateWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
3042
return ctrl.NewWebhookManagedBy(mgr).
31-
For(r).
43+
For(&infrav1.VSphereClusterTemplate{}).
44+
WithValidator(webhook).
3245
Complete()
3346
}
3447

35-
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-vsphereclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vsphereclustertemplates,versions=v1beta1,name=validation.vsphereclustertemplate.infrastructure.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
36-
37-
var _ webhook.Validator = &VSphereClusterTemplate{}
38-
3948
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
40-
func (r *VSphereClusterTemplate) ValidateCreate() (admission.Warnings, error) {
49+
func (webhook *VSphereClusterTemplateWebhook) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
4150
return nil, nil
4251
}
4352

4453
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
45-
func (r *VSphereClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
46-
old := oldRaw.(*VSphereClusterTemplate)
47-
if !reflect.DeepEqual(r.Spec.Template.Spec, old.Spec.Template.Spec) {
54+
func (webhook *VSphereClusterTemplateWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) {
55+
oldTyped, ok := oldRaw.(*infrav1.VSphereClusterTemplate)
56+
if !ok {
57+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereClusterTemplate but got a %T", oldRaw))
58+
}
59+
newTyped, ok := newRaw.(*infrav1.VSphereClusterTemplate)
60+
if !ok {
61+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereClusterTemplate but got a %T", newRaw))
62+
}
63+
if !reflect.DeepEqual(newTyped.Spec.Template.Spec, oldTyped.Spec.Template.Spec) {
4864
return nil, field.Forbidden(field.NewPath("spec", "template", "spec"), "VSphereClusterTemplate spec is immutable")
4965
}
5066
return nil, nil
5167
}
5268

5369
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
54-
func (r *VSphereClusterTemplate) ValidateDelete() (admission.Warnings, error) {
70+
func (webhook *VSphereClusterTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5571
return nil, nil
5672
}

apis/v1beta1/vspheredeploymentzone_webhook.go renamed to internal/webhooks/vspheredeploymentzone.go

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

17-
package v1beta1
17+
package webhooks
1818

1919
import (
20+
"context"
21+
"fmt"
22+
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
24+
"k8s.io/apimachinery/pkg/runtime"
2025
"k8s.io/utils/pointer"
2126
ctrl "sigs.k8s.io/controller-runtime"
2227
"sigs.k8s.io/controller-runtime/pkg/webhook"
28+
29+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
2330
)
2431

25-
func (z *VSphereDeploymentZone) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-vspheredeploymentzone,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vspheredeploymentzones,versions=v1beta1,name=default.vspheredeploymentzone.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
33+
34+
// VSphereDeploymentZoneWebhook implements a validation and defaulting webhook for VSphereDeploymentZone.
35+
type VSphereDeploymentZoneWebhook struct{}
36+
37+
var _ webhook.CustomDefaulter = &VSphereDeploymentZoneWebhook{}
38+
39+
func (webhook *VSphereDeploymentZoneWebhook) SetupWebhookWithManager(mgr ctrl.Manager) error {
2640
return ctrl.NewWebhookManagedBy(mgr).
27-
For(z).
41+
For(&infrav1.VSphereDeploymentZone{}).
42+
WithDefaulter(webhook).
2843
Complete()
2944
}
3045

31-
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-vspheredeploymentzone,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vspheredeploymentzones,versions=v1beta1,name=default.vspheredeploymentzone.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1
32-
33-
var _ webhook.Defaulter = &VSphereDeploymentZone{}
34-
3546
// Default implements webhook.Defaulter so a webhook will be registered for the type.
36-
func (z *VSphereDeploymentZone) Default() {
37-
if z.Spec.ControlPlane == nil {
38-
z.Spec.ControlPlane = pointer.Bool(true)
47+
func (webhook *VSphereDeploymentZoneWebhook) Default(_ context.Context, obj runtime.Object) error {
48+
typedObj, ok := obj.(*infrav1.VSphereDeploymentZone)
49+
if !ok {
50+
return apierrors.NewBadRequest(fmt.Sprintf("expected a VSphereDeploymentZone but got a %T", obj))
3951
}
52+
if typedObj.Spec.ControlPlane == nil {
53+
typedObj.Spec.ControlPlane = pointer.Bool(true)
54+
}
55+
56+
return nil
4057
}

apis/v1beta1/vspheredeploymentzone_webhook_test.go renamed to internal/webhooks/vspheredeploymentzone_test.go

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

17-
package v1beta1
17+
package webhooks
1818

1919
import (
20+
"context"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
2324
"k8s.io/utils/pointer"
25+
26+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
2427
)
2528

2629
func TestVSphereDeploymentZone_Default(t *testing.T) {
@@ -47,12 +50,13 @@ func TestVSphereDeploymentZone_Default(t *testing.T) {
4750
// Need to reinit the test variable
4851
tt := tt
4952
t.Run(tt.name, func(t *testing.T) {
50-
vdz := VSphereDeploymentZone{
51-
Spec: VSphereDeploymentZoneSpec{
53+
vdz := infrav1.VSphereDeploymentZone{
54+
Spec: infrav1.VSphereDeploymentZoneSpec{
5255
ControlPlane: tt.boolPtr,
5356
},
5457
}
55-
vdz.Default()
58+
webhook := VSphereDeploymentZoneWebhook{}
59+
g.Expect(webhook.Default(context.Background(), &vdz)).NotTo(HaveOccurred())
5660
g.Expect(vdz.Spec.ControlPlane).NotTo(BeNil())
5761
g.Expect(*vdz.Spec.ControlPlane).To(Equal(tt.expectedVal))
5862
})

0 commit comments

Comments
 (0)