11package  v2
22
33import  (
4+ 	"context" 
5+ 	"fmt" 
6+ 
47	corev1 "k8s.io/api/core/v1" 
58	apierrors "k8s.io/apimachinery/pkg/api/errors" 
69	"k8s.io/apimachinery/pkg/runtime" 
@@ -14,20 +17,30 @@ import (
1417func  (r  * Egress ) SetupWebhookWithManager (mgr  ctrl.Manager ) error  {
1518	return  ctrl .NewWebhookManagedBy (mgr ).
1619		For (r ).
20+ 		WithDefaulter (& EgressCustomDefaulter {}).
21+ 		WithValidator (& EgressCustomValidator {}).
1722		Complete ()
1823}
1924
25+ // EgressCustomDefaulter is an empty struct that implements webhook.CustomDefaulter 
26+ type  EgressCustomDefaulter  struct {}
27+ 
2028// EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN! 
2129
2230// +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} 
2331
24- var  _  webhook.Defaulter  =  & Egress {}
32+ var  _  webhook.CustomDefaulter  =  & EgressCustomDefaulter {}
2533
2634// Default implements webhook.Defaulter so a webhook will be registered for the type 
27- func  (r  * Egress ) Default () {
28- 	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 a %T" , obj )
39+ 	}
40+ 
41+ 	tmpl  :=  egress .Spec .Template 
2942	if  tmpl  ==  nil  {
30- 		return 
43+ 		return   nil 
3144	}
3245
3346	if  len (tmpl .Spec .Containers ) ==  0  {
@@ -37,33 +50,45 @@ func (r *Egress) Default() {
3750			},
3851		}
3952	}
53+ 	return  nil 
4054}
4155
56+ // EgressCustomValidator is an empty struct that implements webhook.CustomValidator 
57+ type  EgressCustomValidator  struct {}
58+ 
4259// +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} 
4360
44- var  _  webhook.Validator  =  & Egress {}
61+ var  _  webhook.CustomValidator  =  & EgressCustomValidator {}
4562
4663// ValidateCreate implements webhook.Validator so a webhook will be registered for the type 
47- func  (r  * Egress ) ValidateCreate () (warnings  admission.Warnings , err  error ) {
48- 	errs   :=  r . Spec . validate ( )
49- 	if  len ( errs )  ==   0  {
50- 		return  nil , nil 
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 a %T" ,  obj ) 
5168	}
5269
53- 	return  nil , apierrors .NewInvalid (schema.GroupKind {Group : GroupVersion .Group , Kind : "Egress" }, r .Name , errs )
70+ 	if  errs  :=  egress .Spec .validate (); len (errs ) !=  0  {
71+ 		return  nil , apierrors .NewInvalid (schema.GroupKind {Group : GroupVersion .Group , Kind : "Egress" }, egress .Name , errs )
72+ 	}
73+ 
74+ 	return  nil , nil 
5475}
5576
5677// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type 
57- func  (r  * Egress ) ValidateUpdate (old  runtime.Object ) (warnings  admission.Warnings , err  error ) {
58- 	errs   :=  r . Spec . validateUpdate ( )
59- 	if  len ( errs )  ==   0  {
60- 		return  nil , nil 
78+ func  (r  * EgressCustomValidator ) ValidateUpdate (ctx  context. Context ,  oldObj ,  newObj  runtime.Object ) (warnings  admission.Warnings , err  error ) {
79+ 	egress ,  ok   :=  newObj .( * Egress )
80+ 	if  ! ok  {
81+ 		return  nil , fmt . Errorf ( "expected an Egress object but got a %T" ,  newObj ) 
6182	}
6283
63- 	return  nil , apierrors .NewInvalid (schema.GroupKind {Group : GroupVersion .Group , Kind : "Egress" }, r .Name , errs )
84+ 	if  errs  :=  egress .Spec .validateUpdate (); len (errs ) !=  0  {
85+ 		return  nil , apierrors .NewInvalid (schema.GroupKind {Group : GroupVersion .Group , Kind : "Egress" }, egress .Name , errs )
86+ 	}
87+ 
88+ 	return  nil , nil 
6489}
6590
6691// ValidateDelete implements webhook.Validator so a webhook will be registered for the type 
67- func  (r  * Egress ) ValidateDelete () (warnings  admission.Warnings , err  error ) {
92+ func  (r  * EgressCustomValidator ) ValidateDelete (ctx  context. Context ,  old  runtime. Object ) (warnings  admission.Warnings , err  error ) {
6893	return  nil , nil 
6994}
0 commit comments