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 · 4 comments
Open

Proposal: Generics for CustomValidator & CustomDefaulter #2675

ahmetb opened this issue Feb 7, 2024 · 4 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

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.

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 9, 2024
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. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests

4 participants