@@ -2,6 +2,7 @@ package v2
22
33import (
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 (
1617func (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