@@ -18,99 +18,73 @@ package v1
1818
1919import (
2020 "context"
21- "fmt"
2221
2322 "github.com/go-logr/logr"
2423 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25- "k8s.io/apimachinery/pkg/runtime"
2624 "k8s.io/apimachinery/pkg/util/validation/field"
2725 ctrl "sigs.k8s.io/controller-runtime"
28- "sigs.k8s.io/controller-runtime/pkg/conversion"
29- "sigs.k8s.io/controller-runtime/pkg/webhook"
3026 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3127)
3228
3329func (r * ServiceBinding ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
34- return ctrl .NewWebhookManagedBy (mgr ).
35- For (r ).
30+ return ctrl .NewWebhookManagedBy (mgr , r ).
3631 WithDefaulter (r ).
3732 WithValidator (r ).
3833 Complete ()
3934}
4035
41- var _ webhook. CustomDefaulter = & ServiceBinding {}
36+ var _ admission. Defaulter [ * ServiceBinding ] = & ServiceBinding {}
4237
4338// Default implements webhook.CustomDefaulter so a webhook will be registered for the type
44- func (r * ServiceBinding ) Default (ctx context.Context , obj runtime.Object ) error {
45- r = obj .(* ServiceBinding )
46-
47- if r .Spec .Name == "" {
48- r .Spec .Name = r .Name
39+ func (* ServiceBinding ) Default (ctx context.Context , obj * ServiceBinding ) error {
40+ if obj .Spec .Name == "" {
41+ obj .Spec .Name = obj .Name
4942 }
5043
5144 return nil
5245}
5346
5447//+kubebuilder:webhook:path=/validate-servicebinding-io-v1-servicebinding,mutating=false,failurePolicy=fail,sideEffects=None,groups=servicebinding.io,resources=servicebindings,verbs=create;update,versions=v1,name=v1.servicebindings.servicebinding.io,admissionReviewVersions={v1,v1beta1}
5548
56- var _ webhook. CustomValidator = & ServiceBinding {}
49+ var _ admission. Validator [ * ServiceBinding ] = & ServiceBinding {}
5750
5851// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
59- func (r * ServiceBinding ) ValidateCreate (ctx context.Context , obj runtime. Object ) (admission.Warnings , error ) {
52+ func (* ServiceBinding ) ValidateCreate (ctx context.Context , obj * ServiceBinding ) (admission.Warnings , error ) {
6053 log := logr .FromContextOrDiscard (ctx )
6154 log .V (1 ).Info ("Validating Create" )
6255
63- r = obj .(* ServiceBinding )
64-
65- (& ServiceBinding {}).Default (ctx , r )
66- return nil , r .validate ().ToAggregate ()
56+ (& ServiceBinding {}).Default (ctx , obj )
57+ return nil , obj .validate ().ToAggregate ()
6758}
6859
6960// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
70- func (r * ServiceBinding ) ValidateUpdate (ctx context.Context , old , obj runtime. Object ) (admission.Warnings , error ) {
61+ func (* ServiceBinding ) ValidateUpdate (ctx context.Context , old , obj * ServiceBinding ) (admission.Warnings , error ) {
7162 log := logr .FromContextOrDiscard (ctx )
7263 log .V (1 ).Info ("Validating Update" )
7364
74- r = obj .(* ServiceBinding )
75-
76- (& ServiceBinding {}).Default (ctx , r )
65+ (& ServiceBinding {}).Default (ctx , obj )
7766 errs := field.ErrorList {}
7867
7968 // check immutable fields
80- var ro * ServiceBinding
81- if o , ok := old .(* ServiceBinding ); ok {
82- ro = o
83- } else if o , ok := old .(conversion.Convertible ); ok {
84- ro = & ServiceBinding {}
85- if err := o .ConvertTo (ro ); err != nil {
86- return nil , err
87- }
88- } else {
69+ if obj .Spec .Workload .APIVersion != old .Spec .Workload .APIVersion {
8970 errs = append (errs ,
90- field .InternalError ( nil , fmt . Errorf ( "old object must be of type v1. ServiceBinding" ) ),
71+ field .Forbidden ( field . NewPath ( "spec" , "workload" , "apiVersion" ), "Workload apiVersion is immutable. Delete and recreate the ServiceBinding to update." ),
9172 )
9273 }
93- if len (errs ) == 0 {
94- if r .Spec .Workload .APIVersion != ro .Spec .Workload .APIVersion {
95- errs = append (errs ,
96- field .Forbidden (field .NewPath ("spec" , "workload" , "apiVersion" ), "Workload apiVersion is immutable. Delete and recreate the ServiceBinding to update." ),
97- )
98- }
99- if r .Spec .Workload .Kind != ro .Spec .Workload .Kind {
100- errs = append (errs ,
101- field .Forbidden (field .NewPath ("spec" , "workload" , "kind" ), "Workload kind is immutable. Delete and recreate the ServiceBinding to update." ),
102- )
103- }
74+ if obj .Spec .Workload .Kind != old .Spec .Workload .Kind {
75+ errs = append (errs ,
76+ field .Forbidden (field .NewPath ("spec" , "workload" , "kind" ), "Workload kind is immutable. Delete and recreate the ServiceBinding to update." ),
77+ )
10478 }
10579
10680 // validate new object
107- errs = append (errs , r .validate ()... )
81+ errs = append (errs , obj .validate ()... )
10882
10983 return nil , errs .ToAggregate ()
11084}
11185
11286// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
113- func (r * ServiceBinding ) ValidateDelete (ctx context.Context , obj runtime. Object ) (admission.Warnings , error ) {
87+ func (* ServiceBinding ) ValidateDelete (ctx context.Context , obj * ServiceBinding ) (admission.Warnings , error ) {
11488 log := logr .FromContextOrDiscard (ctx )
11589 log .V (1 ).Info ("Validating Delete" )
11690
0 commit comments