Skip to content

Commit 728ae43

Browse files
committed
fix webhook setup
Signed-off-by: Hiroki Hanada <hiroki-hanada@cybozu.co.jp>
1 parent 0d29a05 commit 728ae43

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

v2/api/v2/addresspool_webhook.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/cybozu-go/coil/v2/pkg/constants"
78
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -17,46 +18,67 @@ import (
1718
func (r *AddressPool) SetupWebhookWithManager(mgr ctrl.Manager) error {
1819
return ctrl.NewWebhookManagedBy(mgr).
1920
For(r).
21+
WithDefaulter(&AddressPoolCustomDefaulter{}).
22+
WithValidator(&AddressPoolCustomValidator{}).
2023
Complete()
2124
}
2225

26+
// AddressPoolCustomDefaulter is an empty struct that implements webhook.CustomDefaulter
27+
type AddressPoolCustomDefaulter struct{}
28+
2329
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2430

2531
//+kubebuilder:webhook:path=/mutate-coil-cybozu-com-v2-addresspool,mutating=true,failurePolicy=fail,sideEffects=None,groups=coil.cybozu.com,resources=addresspools,verbs=create,versions=v2,name=maddresspool.kb.io,admissionReviewVersions={v1,v1beta1}
2632

27-
var _ webhook.CustomDefaulter = &AddressPool{}
33+
var _ webhook.CustomDefaulter = &AddressPoolCustomDefaulter{}
2834

2935
// Default implements webhook.Defaulter so a webhook will be registered for the type
30-
func (r *AddressPool) Default(ctx context.Context, obj runtime.Object) error {
31-
controllerutil.AddFinalizer(r, constants.FinCoil)
36+
func (r *AddressPoolCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
37+
addressPool, ok := obj.(*AddressPool)
38+
if !ok {
39+
return fmt.Errorf("expected an AddressPool object but got %T", obj)
40+
}
41+
42+
controllerutil.AddFinalizer(addressPool, constants.FinCoil)
3243
return nil
3344
}
3445

46+
// AddressPoolCustomValidator is an empty struct that implements webhook.CustomValidator
47+
type AddressPoolCustomValidator struct{}
48+
3549
// +kubebuilder:webhook:path=/validate-coil-cybozu-com-v2-addresspool,mutating=false,failurePolicy=fail,sideEffects=None,groups=coil.cybozu.com,resources=addresspools,verbs=create;update,versions=v2,name=vaddresspool.kb.io,admissionReviewVersions={v1,v1beta1}
3650

37-
var _ webhook.CustomValidator = &AddressPool{}
51+
var _ webhook.CustomValidator = &AddressPoolCustomValidator{}
3852

3953
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
40-
func (r *AddressPool) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
41-
errs := r.Spec.validate()
54+
func (r *AddressPoolCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
55+
addressPool, ok := obj.(*AddressPool)
56+
if !ok {
57+
return nil, fmt.Errorf("expected an AddressPool object but got %T", obj)
58+
}
59+
errs := addressPool.Spec.validate()
4260
if len(errs) == 0 {
4361
return nil, nil
4462
}
4563

46-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, r.Name, errs)
64+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
4765
}
4866

4967
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
50-
func (r *AddressPool) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
51-
errs := r.Spec.validateUpdate(oldObj.(*AddressPool).Spec)
68+
func (r *AddressPoolCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
69+
addressPool, ok := newObj.(*AddressPool)
70+
if !ok {
71+
return nil, fmt.Errorf("expected an AddressPool object but got %T", newObj)
72+
}
73+
errs := addressPool.Spec.validateUpdate(oldObj.(*AddressPool).Spec)
5274
if len(errs) == 0 {
5375
return nil, nil
5476
}
5577

56-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, r.Name, errs)
78+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "AddressPool"}, addressPool.Name, errs)
5779
}
5880

5981
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
60-
func (r *AddressPool) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
82+
func (r *AddressPoolCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
6183
return nil, nil
6284
}

v2/api/v2/egress_webhook.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2
22

33
import (
44
"context"
5+
"fmt"
56

67
corev1 "k8s.io/api/core/v1"
78
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -16,18 +17,28 @@ import (
1617
func (r *Egress) SetupWebhookWithManager(mgr ctrl.Manager) error {
1718
return ctrl.NewWebhookManagedBy(mgr).
1819
For(r).
20+
WithDefaulter(&EgressCustomDefaulter{}).
21+
WithValidator(&EgressCustomValidator{}).
1922
Complete()
2023
}
2124

25+
// EgressCustomDefaulter is an empty struct that implements webhook.CustomDefaulter
26+
type EgressCustomDefaulter struct{}
27+
2228
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2329

2430
// +kubebuilder:webhook:path=/mutate-coil-cybozu-com-v2-egress,mutating=true,failurePolicy=fail,sideEffects=None,groups=coil.cybozu.com,resources=egresses,verbs=create,versions=v2,name=megress.kb.io,admissionReviewVersions={v1,v1beta1}
2531

26-
var _ webhook.CustomDefaulter = &Egress{}
32+
var _ webhook.CustomDefaulter = &EgressCustomDefaulter{}
2733

2834
// Default implements webhook.Defaulter so a webhook will be registered for the type
29-
func (r *Egress) Default(ctx context.Context, obj runtime.Object) error {
30-
tmpl := r.Spec.Template
35+
func (r *EgressCustomDefaulter) Default(ctx context.Context, obj runtime.Object) error {
36+
egress, ok := obj.(*Egress)
37+
if !ok {
38+
return fmt.Errorf("expected an Egress object but got %T", obj)
39+
}
40+
41+
tmpl := egress.Spec.Template
3142
if tmpl == nil {
3243
return nil
3344
}
@@ -42,31 +53,44 @@ func (r *Egress) Default(ctx context.Context, obj runtime.Object) error {
4253
return nil
4354
}
4455

56+
// EgressCustomValidator is an empty struct that implements webhook.CustomValidator
57+
type EgressCustomValidator struct{}
58+
4559
// +kubebuilder:webhook:path=/validate-coil-cybozu-com-v2-egress,mutating=false,failurePolicy=fail,sideEffects=None,groups=coil.cybozu.com,resources=egresses,verbs=create;update,versions=v2,name=vegress.kb.io,admissionReviewVersions={v1,v1beta1}
4660

47-
var _ webhook.CustomValidator = &Egress{}
61+
var _ webhook.CustomValidator = &EgressCustomValidator{}
4862

4963
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
50-
func (r *Egress) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
51-
errs := r.Spec.validate()
64+
func (r *EgressCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
65+
egress, ok := obj.(*Egress)
66+
if !ok {
67+
return nil, fmt.Errorf("expected an Egress object but got %T", obj)
68+
}
69+
70+
errs := egress.Spec.validate()
5271
if len(errs) == 0 {
5372
return nil, nil
5473
}
5574

56-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, r.Name, errs)
75+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
5776
}
5877

5978
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
60-
func (r *Egress) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
61-
errs := r.Spec.validateUpdate()
79+
func (r *EgressCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
80+
egress, ok := newObj.(*Egress)
81+
if !ok {
82+
return nil, fmt.Errorf("expected an Egress object but got %T", newObj)
83+
}
84+
85+
errs := egress.Spec.validateUpdate()
6286
if len(errs) == 0 {
6387
return nil, nil
6488
}
6589

66-
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, r.Name, errs)
90+
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Egress"}, egress.Name, errs)
6791
}
6892

6993
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
70-
func (r *Egress) ValidateDelete(ctx context.Context, old runtime.Object) (warnings admission.Warnings, err error) {
94+
func (r *EgressCustomValidator) ValidateDelete(ctx context.Context, old runtime.Object) (warnings admission.Warnings, err error) {
7195
return nil, nil
7296
}

0 commit comments

Comments
 (0)