Skip to content
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

Proposal: Generics for CustomValidator & CustomDefaulter #2675

Open
ahmetb opened this issue Feb 7, 2024 · 3 comments
Open

Proposal: Generics for CustomValidator & CustomDefaulter #2675

ahmetb opened this issue Feb 7, 2024 · 3 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@ahmetb
Copy link
Member

ahmetb commented Feb 7, 2024

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

@k8s-ci-robot
Copy link
Contributor

@ahmetb: The label(s) kind/proposal cannot be applied, because the repository doesn't have them.

In response to this:

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)?).

/kind proposal
/kind feature

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Feb 7, 2024
@alvaroaleman
Copy link
Member

@ahmetb are you sure this is possible? My understanding is that it isn't due to golang/go#49085, the WebhookBuilder would only be able to support one type. I'd love to be wrong on this though.

@ahmetb
Copy link
Member Author

ahmetb commented Feb 9, 2024

I will give it a shot. I hit that in porting go-linq to generics as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants