Skip to content

Replace deprecated webhook.Validator with webhook.CustomValidator #6700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ New deprecation(s):

### Other

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

## v2.17.0

Expand Down
81 changes: 73 additions & 8 deletions apis/eventing/v1alpha1/cloudeventsource_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"encoding/json"
"fmt"
"slices"
Expand All @@ -33,28 +34,61 @@ var cloudeventsourcelog = logf.Log.WithName("cloudeventsource-validation-webhook

func (ces *CloudEventSource) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
WithValidator(&CloudEventSourceCustomValidator{}).
For(ces).
Complete()
}

func (cces *ClusterCloudEventSource) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
WithValidator(&ClusterCloudEventSourceCustomValidator{}).
For(cces).
Complete()
}

// +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

var _ webhook.Validator = &CloudEventSource{}
// CloudEventSourceCustomValidator is a custom validator for CloudEventSource objects
type CloudEventSourceCustomValidator struct{}

func (cescv CloudEventSourceCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
ces := obj.(*CloudEventSource)
return ces.ValidateCreate(request.DryRun)
}

func (cescv CloudEventSourceCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
ces := newObj.(*CloudEventSource)
old := oldObj.(*CloudEventSource)
return ces.ValidateUpdate(old, request.DryRun)
}

func (cescv CloudEventSourceCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
ces := obj.(*CloudEventSource)
return ces.ValidateDelete(request.DryRun)
}

var _ webhook.CustomValidator = &CloudEventSourceCustomValidator{}

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

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

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

func (ces *CloudEventSource) ValidateDelete() (admission.Warnings, error) {
func (ces *CloudEventSource) ValidateDelete(_ *bool) (admission.Warnings, error) {
return nil, nil
}

// +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

var _ webhook.Validator = &ClusterCloudEventSource{}
// ClusterCloudEventSourceCustomValidator is a custom validator for ClusterCloudEventSource objects
type ClusterCloudEventSourceCustomValidator struct{}

func (ccescv ClusterCloudEventSourceCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
cces := obj.(*ClusterCloudEventSource)
return cces.ValidateCreate(request.DryRun)
}

func (ccescv ClusterCloudEventSourceCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
cces := newObj.(*ClusterCloudEventSource)
old := oldObj.(*ClusterCloudEventSource)
return cces.ValidateUpdate(old, request.DryRun)
}

func (ccescv ClusterCloudEventSourceCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
cces := obj.(*ClusterCloudEventSource)
return cces.ValidateDelete(request.DryRun)
}

var _ webhook.CustomValidator = &ClusterCloudEventSourceCustomValidator{}

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

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

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

func (cces *ClusterCloudEventSource) ValidateDelete() (admission.Warnings, error) {
func (cces *ClusterCloudEventSource) ValidateDelete(_ *bool) (admission.Warnings, error) {
return nil, nil
}

Expand Down
45 changes: 39 additions & 6 deletions apis/keda/v1alpha1/scaledjob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"encoding/json"
"fmt"

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

func (s *ScaledJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
WithValidator(&ScaledJobCustomValidator{}).
For(s).
Complete()
}

// +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

var _ webhook.Validator = &ScaledJob{}
// ScaledJobCustomValidator is a custom validator for ScaledJob objects
type ScaledJobCustomValidator struct{}

func (sjcv ScaledJobCustomValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
sj := obj.(*ScaledJob)
return sj.ValidateCreate(request.DryRun)
}

func (sjcv ScaledJobCustomValidator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
sj := newObj.(*ScaledJob)
old := oldObj.(*ScaledJob)
return sj.ValidateUpdate(old, request.DryRun)
}

func (sjcv ScaledJobCustomValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
request, err := admission.RequestFromContext(ctx)
if err != nil {
return nil, err
}
sj := obj.(*ScaledJob)
return sj.ValidateDelete(request.DryRun)
}

var _ webhook.CustomValidator = &ScaledJobCustomValidator{}

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

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

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

func (s *ScaledJob) ValidateDelete() (admission.Warnings, error) {
func (s *ScaledJob) ValidateDelete(_ *bool) (admission.Warnings, error) {
return nil, nil
}

Expand Down
18 changes: 14 additions & 4 deletions apis/keda/v1alpha1/scaledobject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ var memoryString = "memory"
var cpuString = "cpu"

func (so *ScaledObject) SetupWebhookWithManager(mgr ctrl.Manager, cacheMissFallback bool) error {
err := setupKubernetesClients(mgr, cacheMissFallback)
if err != nil {
return fmt.Errorf("failed to setup kubernetes clients: %w", err)
}

return ctrl.NewWebhookManagedBy(mgr).
WithValidator(&ScaledObjectCustomValidator{}).
For(so).
Complete()
}

func setupKubernetesClients(mgr ctrl.Manager, cacheMissFallback bool) error {
kc = mgr.GetClient()
restMapper = mgr.GetRESTMapper()
cacheMissToDirectClient = cacheMissFallback
Expand All @@ -70,10 +82,8 @@ func (so *ScaledObject) SetupWebhookWithManager(mgr ctrl.Manager, cacheMissFallb
return fmt.Errorf("failed to initialize direct client: %w", err)
}
}
return ctrl.NewWebhookManagedBy(mgr).
WithValidator(&ScaledObjectCustomValidator{}).
For(so).
Complete()

return nil
}

// +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
Expand Down
Loading
Loading