Skip to content

Generic Watches for builder #2955

Closed as not planned
Closed as not planned
@pmalek

Description

@pmalek

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/staleDenotes an issue or PR has remained open with no activity and has become stale.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions