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

Generic Watches for builder #2955

Open
pmalek opened this issue Sep 22, 2024 · 0 comments
Open

Generic Watches for builder #2955

pmalek opened this issue Sep 22, 2024 · 0 comments

Comments

@pmalek
Copy link

pmalek commented Sep 22, 2024

Problem statement

#2214 introduced (through #2783) generic funcs for Event, Predicate and Handler.

TypedBuilder has only 1 type parameter for the request type and since Go (as of 1.23) doesn't allow type parameters on methods golang/go#49085, Watches() uses handler.TypedEventHandler[client.Object, request] as eventHandler parameter.

That client.Object limits the handler functions to only have client.Object as argument types so users have to use:

podForService := func(ctx context.Context, obj client.Object) []reconcile.Request {
	svc , ok := obj.(*core.Service)
	if !ok {
		return nil
	}
	return nil
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
b := ctrl.NewControllerManagedBy(mgr)
b.For(&corev1.Pod{})
b.Watches(
	&corev1.Service{},
	handler.TypedEnqueueRequestsFromMapFunc(
		podForService,
	),
)

and cannot use strongly typed handlers like

podForServiceTyped := func(ctx context.Context, svc *corev1.Service) []reconcile.Request {
	return nil
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
b := ctrl.NewControllerManagedBy(mgr)
b.For(&corev1.Pod{})
b.Watches(
	&corev1.Service{},
	handler.TypedEnqueueRequestsFromMapFunc(
		podForServiceTyped,
	),
)

because

cannot use handler.TypedEnqueueRequestsFromMapFunc(podForServiceTyped) (value of type handler.TypedEventHandler[*"k8s.io/api/core/v1".Service, reconcile.Request]) as handler.TypedEventHandler[client.Object, reconcile.Request] value in argument to b.Watches: handler.TypedEventHandler[*"k8s.io/api/core/v1".Service, reconcile.Request] does not implement handler.TypedEventHandler[client.Object, reconcile.Request] (wrong type for method Create)
		have Create(context.Context, event.TypedCreateEvent[*"k8s.io/api/core/v1".Service], workqueue.TypedRateLimitingInterface[reconcile.Request])
		want Create(context.Context, event.TypedCreateEvent[client.Object], workqueue.TypedRateLimitingInterface[reconcile.Request])

Additional information

Due to golang/go#49085 this cannot be (easily?at all?) implemented as noted in #2783.

This issue is mostly for tracking and to acknowledge the API limitation.

Feel free to comment on the issue if you feel there's a way to move forward without Go implementing golang/go#49085.

Current workaround

If I understand correctly, the following is an equivalent which can be used:

src := source.Kind(
	mgr.GetCache(),
	&corev1.Service{},
	handler.TypedEnqueueRequestsFromMapFunc(
		podForServiceTyped,
	),
)
b.WatchesRawSource(src)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant