Skip to content

Proposal: Generics for CustomValidator & CustomDefaulter #2675

@ahmetb

Description

@ahmetb

CustomValidator and CustomDefaulter interfaces could use generics support to potentially shorten the handler funcs like this:

func (w *KubernetesPoolWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { oldO, ok := oldObj.(apiv1alpha1.KubernetesPool) if err != nil { return nil, errors.NewBadRequest(fmt.Sprintf("unexpected type %T", oldObj))	} newO, ok := newObj.(apiv1alpha1.KubernetesPool) if err != nil { return nil, errors.NewBadRequest(fmt.Sprintf("unexpected type %T", newObj))	} return nil, validateUpdate(oldO, newO) }

to

func (w *KubernetesPoolWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj apiv1alpha1.KubernetesPool) (admission.Warnings, error) { return nil, validateUpdate(oldO, newO) }

Also statements like:

return ctrl.NewWebhookManagedBy(mgr).	For(&apiv1alpha1.KubernetesPool{}).	WithDefaulter(w).	WithValidator(w).	Complete() 

can probably be inferred with more type safety (right now there's nothing ensures a & b are handlers of the same type in WithDefaulter(a).WithDefaulter(b)?). e.g.

return ctrl.NewWebhookManagedBy[apiv1alpha1.KubernetesPool](mgr).	WithDefaulter(w).	WithValidator(w).	Complete() 

/kind proposal
/kind feature

Metadata

Metadata

Assignees

Labels

kind/featureCategorizes issue or PR as related to a new feature.lifecycle/frozenIndicates that an issue or PR should not be auto-closed due to staleness.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions