Skip to content

Commit d65a7e0

Browse files
[COR-287] Replace deprecated webhook.Validator with webhook.CustomValidator
1 parent a3e2090 commit d65a7e0

5 files changed

Lines changed: 199 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ New deprecation(s):
8989

9090
### Other
9191

92-
- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))
92+
- **General**: Replace deprecated webhook.Validator with webhook.CustomValidator ([#6660](https://github.com/kedacore/keda/issues/6660))
9393

9494
## v2.17.1
9595

apis/eventing/v1alpha1/cloudeventsource_webhook.go

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223
"slices"
@@ -33,28 +34,61 @@ var cloudeventsourcelog = logf.Log.WithName("cloudeventsource-validation-webhook
3334

3435
func (ces *CloudEventSource) SetupWebhookWithManager(mgr ctrl.Manager) error {
3536
return ctrl.NewWebhookManagedBy(mgr).
37+
WithValidator(&CloudEventSourceCustomValidator{}).
3638
For(ces).
3739
Complete()
3840
}
3941

4042
func (cces *ClusterCloudEventSource) SetupWebhookWithManager(mgr ctrl.Manager) error {
4143
return ctrl.NewWebhookManagedBy(mgr).
44+
WithValidator(&ClusterCloudEventSourceCustomValidator{}).
4245
For(cces).
4346
Complete()
4447
}
4548

4649
// +kubebuilder:webhook:path=/validate-eventing-keda-sh-v1alpha1-cloudeventsource,mutating=false,failurePolicy=ignore,sideEffects=None,groups=eventing.keda.sh,resources=cloudeventsources,verbs=create;update,versions=v1alpha1,name=vcloudeventsource.kb.io,admissionReviewVersions=v1
4750

48-
var _ webhook.Validator = &CloudEventSource{}
51+
// CloudEventSourceCustomValidator is a custom validator for CloudEventSource objects
52+
type CloudEventSourceCustomValidator struct{}
53+
54+
func (cescv CloudEventSourceCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
55+
request, err := admission.RequestFromContext(ctx)
56+
if err != nil {
57+
return nil, err
58+
}
59+
ces := obj.(*CloudEventSource)
60+
return ces.ValidateCreate(request.DryRun)
61+
}
62+
63+
func (cescv CloudEventSourceCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
64+
request, err := admission.RequestFromContext(ctx)
65+
if err != nil {
66+
return nil, err
67+
}
68+
ces := newObj.(*CloudEventSource)
69+
old := oldObj.(*CloudEventSource)
70+
return ces.ValidateUpdate(old, request.DryRun)
71+
}
72+
73+
func (cescv CloudEventSourceCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
74+
request, err := admission.RequestFromContext(ctx)
75+
if err != nil {
76+
return nil, err
77+
}
78+
ces := obj.(*CloudEventSource)
79+
return ces.ValidateDelete(request.DryRun)
80+
}
81+
82+
var _ webhook.CustomValidator = &CloudEventSourceCustomValidator{}
4983

5084
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
51-
func (ces *CloudEventSource) ValidateCreate() (admission.Warnings, error) {
85+
func (ces *CloudEventSource) ValidateCreate(_ *bool) (admission.Warnings, error) {
5286
val, _ := json.MarshalIndent(ces, "", " ")
5387
cloudeventsourcelog.Info(fmt.Sprintf("validating cloudeventsource creation for %s", string(val)))
5488
return validateSpec(&ces.Spec)
5589
}
5690

57-
func (ces *CloudEventSource) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
91+
func (ces *CloudEventSource) ValidateUpdate(old runtime.Object, _ *bool) (admission.Warnings, error) {
5892
val, _ := json.MarshalIndent(ces, "", " ")
5993
cloudeventsourcelog.V(1).Info(fmt.Sprintf("validating cloudeventsource update for %s", string(val)))
6094

@@ -66,22 +100,53 @@ func (ces *CloudEventSource) ValidateUpdate(old runtime.Object) (admission.Warni
66100
return validateSpec(&ces.Spec)
67101
}
68102

69-
func (ces *CloudEventSource) ValidateDelete() (admission.Warnings, error) {
103+
func (ces *CloudEventSource) ValidateDelete(_ *bool) (admission.Warnings, error) {
70104
return nil, nil
71105
}
72106

73107
// +kubebuilder:webhook:path=/validate-eventing-keda-sh-v1alpha1-clustercloudeventsource,mutating=false,failurePolicy=ignore,sideEffects=None,groups=eventing.keda.sh,resources=clustercloudeventsources,verbs=create;update,versions=v1alpha1,name=vclustercloudeventsource.kb.io,admissionReviewVersions=v1
74108

75-
var _ webhook.Validator = &ClusterCloudEventSource{}
109+
// ClusterCloudEventSourceCustomValidator is a custom validator for ClusterCloudEventSource objects
110+
type ClusterCloudEventSourceCustomValidator struct{}
111+
112+
func (ccescv ClusterCloudEventSourceCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
113+
request, err := admission.RequestFromContext(ctx)
114+
if err != nil {
115+
return nil, err
116+
}
117+
cces := obj.(*ClusterCloudEventSource)
118+
return cces.ValidateCreate(request.DryRun)
119+
}
120+
121+
func (ccescv ClusterCloudEventSourceCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
122+
request, err := admission.RequestFromContext(ctx)
123+
if err != nil {
124+
return nil, err
125+
}
126+
cces := newObj.(*ClusterCloudEventSource)
127+
old := oldObj.(*ClusterCloudEventSource)
128+
return cces.ValidateUpdate(old, request.DryRun)
129+
}
130+
131+
func (ccescv ClusterCloudEventSourceCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
132+
request, err := admission.RequestFromContext(ctx)
133+
if err != nil {
134+
return nil, err
135+
}
136+
cces := obj.(*ClusterCloudEventSource)
137+
return cces.ValidateDelete(request.DryRun)
138+
}
139+
140+
var _ webhook.CustomValidator = &ClusterCloudEventSourceCustomValidator{}
76141

77142
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
78-
func (cces *ClusterCloudEventSource) ValidateCreate() (admission.Warnings, error) {
143+
func (cces *ClusterCloudEventSource) ValidateCreate(_ *bool) (admission.Warnings, error) {
79144
val, _ := json.MarshalIndent(cces, "", " ")
80145
cloudeventsourcelog.Info(fmt.Sprintf("validating clustercloudeventsource creation for %s", string(val)))
81146
return validateSpec(&cces.Spec)
82147
}
83148

84-
func (cces *ClusterCloudEventSource) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
149+
func (cces *ClusterCloudEventSource) ValidateUpdate(old runtime.Object, _ *bool) (admission.Warnings, error) {
85150
val, _ := json.MarshalIndent(cces, "", " ")
86151
cloudeventsourcelog.V(1).Info(fmt.Sprintf("validating clustercloudeventsource update for %s", string(val)))
87152

@@ -93,7 +158,7 @@ func (cces *ClusterCloudEventSource) ValidateUpdate(old runtime.Object) (admissi
93158
return validateSpec(&cces.Spec)
94159
}
95160

96-
func (cces *ClusterCloudEventSource) ValidateDelete() (admission.Warnings, error) {
161+
func (cces *ClusterCloudEventSource) ValidateDelete(_ *bool) (admission.Warnings, error) {
97162
return nil, nil
98163
}
99164

apis/keda/v1alpha1/scaledjob_webhook.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223

@@ -32,22 +33,54 @@ var scaledjoblog = logf.Log.WithName("scaledjob-validation-webhook")
3233

3334
func (s *ScaledJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
3435
return ctrl.NewWebhookManagedBy(mgr).
36+
WithValidator(&ScaledJobCustomValidator{}).
3537
For(s).
3638
Complete()
3739
}
3840

3941
// +kubebuilder:webhook:path=/validate-keda-sh-v1alpha1-scaledjob,mutating=false,failurePolicy=ignore,sideEffects=None,groups=keda.sh,resources=scaledjobs,verbs=create;update,versions=v1alpha1,name=vscaledjob.kb.io,admissionReviewVersions=v1
4042

41-
var _ webhook.Validator = &ScaledJob{}
43+
// ScaledJobCustomValidator is a custom validator for ScaledJob objects
44+
type ScaledJobCustomValidator struct{}
45+
46+
func (sjcv ScaledJobCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
47+
request, err := admission.RequestFromContext(ctx)
48+
if err != nil {
49+
return nil, err
50+
}
51+
sj := obj.(*ScaledJob)
52+
return sj.ValidateCreate(request.DryRun)
53+
}
54+
55+
func (sjcv ScaledJobCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
56+
request, err := admission.RequestFromContext(ctx)
57+
if err != nil {
58+
return nil, err
59+
}
60+
sj := newObj.(*ScaledJob)
61+
old := oldObj.(*ScaledJob)
62+
return sj.ValidateUpdate(old, request.DryRun)
63+
}
64+
65+
func (sjcv ScaledJobCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
66+
request, err := admission.RequestFromContext(ctx)
67+
if err != nil {
68+
return nil, err
69+
}
70+
sj := obj.(*ScaledJob)
71+
return sj.ValidateDelete(request.DryRun)
72+
}
73+
74+
var _ webhook.CustomValidator = &ScaledJobCustomValidator{}
4275

4376
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
44-
func (s *ScaledJob) ValidateCreate() (admission.Warnings, error) {
77+
func (s *ScaledJob) ValidateCreate(dryRun *bool) (admission.Warnings, error) {
4578
val, _ := json.MarshalIndent(s, "", " ")
4679
scaledjoblog.Info(fmt.Sprintf("validating scaledjob creation for %s", string(val)))
47-
return nil, verifyTriggers(s, "create", false)
80+
return nil, verifyTriggers(s, "create", *dryRun)
4881
}
4982

50-
func (s *ScaledJob) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
83+
func (s *ScaledJob) ValidateUpdate(old runtime.Object, dryRun *bool) (admission.Warnings, error) {
5184
val, _ := json.MarshalIndent(s, "", " ")
5285
scaledobjectlog.V(1).Info(fmt.Sprintf("validating scaledjob update for %s", string(val)))
5386

@@ -56,10 +89,10 @@ func (s *ScaledJob) ValidateUpdate(old runtime.Object) (admission.Warnings, erro
5689
scaledjoblog.V(1).Info("finalizer removal, skipping validation")
5790
return nil, nil
5891
}
59-
return nil, verifyTriggers(s, "update", false)
92+
return nil, verifyTriggers(s, "update", *dryRun)
6093
}
6194

62-
func (s *ScaledJob) ValidateDelete() (admission.Warnings, error) {
95+
func (s *ScaledJob) ValidateDelete(_ *bool) (admission.Warnings, error) {
6396
return nil, nil
6497
}
6598

apis/keda/v1alpha1/scaledobject_webhook.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ var memoryString = "memory"
5353
var cpuString = "cpu"
5454

5555
func (so *ScaledObject) SetupWebhookWithManager(mgr ctrl.Manager, cacheMissFallback bool) error {
56+
err := setupKubernetesClients(mgr, cacheMissFallback)
57+
if err != nil {
58+
return fmt.Errorf("failed to setup kubernetes clients: %w", err)
59+
}
60+
61+
return ctrl.NewWebhookManagedBy(mgr).
62+
WithValidator(&ScaledObjectCustomValidator{}).
63+
For(so).
64+
Complete()
65+
}
66+
67+
func setupKubernetesClients(mgr ctrl.Manager, cacheMissFallback bool) error {
5668
kc = mgr.GetClient()
5769
restMapper = mgr.GetRESTMapper()
5870
cacheMissToDirectClient = cacheMissFallback
@@ -70,10 +82,7 @@ func (so *ScaledObject) SetupWebhookWithManager(mgr ctrl.Manager, cacheMissFallb
7082
return fmt.Errorf("failed to initialize direct client: %w", err)
7183
}
7284
}
73-
return ctrl.NewWebhookManagedBy(mgr).
74-
WithValidator(&ScaledObjectCustomValidator{}).
75-
For(so).
76-
Complete()
85+
return nil
7786
}
7887

7988
// +kubebuilder:webhook:path=/validate-keda-sh-v1alpha1-scaledobject,mutating=false,failurePolicy=ignore,sideEffects=None,groups=keda.sh,resources=scaledobjects,verbs=create;update,versions=v1alpha1,name=vscaledobject.kb.io,admissionReviewVersions=v1

apis/keda/v1alpha1/triggerauthentication_webhook.go

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223

@@ -37,28 +38,61 @@ var triggerauthenticationlog = logf.Log.WithName("triggerauthentication-validati
3738

3839
func (ta *TriggerAuthentication) SetupWebhookWithManager(mgr ctrl.Manager) error {
3940
return ctrl.NewWebhookManagedBy(mgr).
41+
WithValidator(&TriggerAuthenticationCustomValidator{}).
4042
For(ta).
4143
Complete()
4244
}
4345

4446
func (cta *ClusterTriggerAuthentication) SetupWebhookWithManager(mgr ctrl.Manager) error {
4547
return ctrl.NewWebhookManagedBy(mgr).
48+
WithValidator(&ClusterTriggerAuthenticationCustomValidator{}).
4649
For(cta).
4750
Complete()
4851
}
4952

5053
// +kubebuilder:webhook:path=/validate-keda-sh-v1alpha1-triggerauthentication,mutating=false,failurePolicy=ignore,sideEffects=None,groups=keda.sh,resources=triggerauthentications,verbs=create;update,versions=v1alpha1,name=vstriggerauthentication.kb.io,admissionReviewVersions=v1
5154

52-
var _ webhook.Validator = &TriggerAuthentication{}
55+
// TriggerAuthenticationCustomValidator is a custom validator for TriggerAuthentication objects
56+
type TriggerAuthenticationCustomValidator struct{}
57+
58+
func (tacv TriggerAuthenticationCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
59+
request, err := admission.RequestFromContext(ctx)
60+
if err != nil {
61+
return nil, err
62+
}
63+
ta := obj.(*TriggerAuthentication)
64+
return ta.ValidateCreate(request.DryRun)
65+
}
66+
67+
func (tacv TriggerAuthenticationCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
68+
request, err := admission.RequestFromContext(ctx)
69+
if err != nil {
70+
return nil, err
71+
}
72+
ta := newObj.(*TriggerAuthentication)
73+
old := oldObj.(*TriggerAuthentication)
74+
return ta.ValidateUpdate(old, request.DryRun)
75+
}
76+
77+
func (tacv TriggerAuthenticationCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
78+
request, err := admission.RequestFromContext(ctx)
79+
if err != nil {
80+
return nil, err
81+
}
82+
ta := obj.(*TriggerAuthentication)
83+
return ta.ValidateDelete(request.DryRun)
84+
}
85+
86+
var _ webhook.CustomValidator = &TriggerAuthenticationCustomValidator{}
5387

5488
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
55-
func (ta *TriggerAuthentication) ValidateCreate() (admission.Warnings, error) {
89+
func (ta *TriggerAuthentication) ValidateCreate(_ *bool) (admission.Warnings, error) {
5690
val, _ := json.MarshalIndent(ta, "", " ")
5791
triggerauthenticationlog.Info(fmt.Sprintf("validating triggerauthentication creation for %s", string(val)))
5892
return validateSpec(&ta.Spec)
5993
}
6094

61-
func (ta *TriggerAuthentication) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
95+
func (ta *TriggerAuthentication) ValidateUpdate(old runtime.Object, _ *bool) (admission.Warnings, error) {
6296
val, _ := json.MarshalIndent(ta, "", " ")
6397
scaledobjectlog.V(1).Info(fmt.Sprintf("validating triggerauthentication update for %s", string(val)))
6498

@@ -70,22 +104,53 @@ func (ta *TriggerAuthentication) ValidateUpdate(old runtime.Object) (admission.W
70104
return validateSpec(&ta.Spec)
71105
}
72106

73-
func (ta *TriggerAuthentication) ValidateDelete() (admission.Warnings, error) {
107+
func (ta *TriggerAuthentication) ValidateDelete(_ *bool) (admission.Warnings, error) {
74108
return nil, nil
75109
}
76110

77111
// +kubebuilder:webhook:path=/validate-keda-sh-v1alpha1-clustertriggerauthentication,mutating=false,failurePolicy=ignore,sideEffects=None,groups=keda.sh,resources=clustertriggerauthentications,verbs=create;update,versions=v1alpha1,name=vsclustertriggerauthentication.kb.io,admissionReviewVersions=v1
78112

79-
var _ webhook.Validator = &ClusterTriggerAuthentication{}
113+
// ClusterTriggerAuthenticationCustomValidator is a custom validator for ClusterTriggerAuthentication objects
114+
type ClusterTriggerAuthenticationCustomValidator struct{}
115+
116+
func (ctacv ClusterTriggerAuthenticationCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
117+
request, err := admission.RequestFromContext(ctx)
118+
if err != nil {
119+
return nil, err
120+
}
121+
cta := obj.(*ClusterTriggerAuthentication)
122+
return cta.ValidateCreate(request.DryRun)
123+
}
124+
125+
func (ctacv ClusterTriggerAuthenticationCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
126+
request, err := admission.RequestFromContext(ctx)
127+
if err != nil {
128+
return nil, err
129+
}
130+
cta := newObj.(*ClusterTriggerAuthentication)
131+
old := oldObj.(*ClusterTriggerAuthentication)
132+
return cta.ValidateUpdate(old, request.DryRun)
133+
}
134+
135+
func (ctacv ClusterTriggerAuthenticationCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
136+
request, err := admission.RequestFromContext(ctx)
137+
if err != nil {
138+
return nil, err
139+
}
140+
cta := obj.(*ClusterTriggerAuthentication)
141+
return cta.ValidateDelete(request.DryRun)
142+
}
143+
144+
var _ webhook.CustomValidator = &ClusterTriggerAuthenticationCustomValidator{}
80145

81146
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
82-
func (cta *ClusterTriggerAuthentication) ValidateCreate() (admission.Warnings, error) {
147+
func (cta *ClusterTriggerAuthentication) ValidateCreate(_ *bool) (admission.Warnings, error) {
83148
val, _ := json.MarshalIndent(cta, "", " ")
84149
triggerauthenticationlog.Info(fmt.Sprintf("validating clustertriggerauthentication creation for %s", string(val)))
85150
return validateSpec(&cta.Spec)
86151
}
87152

88-
func (cta *ClusterTriggerAuthentication) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
153+
func (cta *ClusterTriggerAuthentication) ValidateUpdate(old runtime.Object, _ *bool) (admission.Warnings, error) {
89154
val, _ := json.MarshalIndent(cta, "", " ")
90155
scaledobjectlog.V(1).Info(fmt.Sprintf("validating clustertriggerauthentication update for %s", string(val)))
91156

@@ -98,7 +163,7 @@ func (cta *ClusterTriggerAuthentication) ValidateUpdate(old runtime.Object) (adm
98163
return validateSpec(&cta.Spec)
99164
}
100165

101-
func (cta *ClusterTriggerAuthentication) ValidateDelete() (admission.Warnings, error) {
166+
func (cta *ClusterTriggerAuthentication) ValidateDelete(_ *bool) (admission.Warnings, error) {
102167
return nil, nil
103168
}
104169

0 commit comments

Comments
 (0)