Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/internal/source/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var log = logf.RuntimeLog.WithName("source").WithName("EventHandler")
var _ cache.ResourceEventHandler = &EventHandler[client.Object, any]{}

// NewEventHandler creates a new EventHandler.
func NewEventHandler[object client.Object, request comparable](
func NewEventHandler[object any, request comparable](
ctx context.Context,
queue workqueue.TypedRateLimitingInterface[request],
handler handler.TypedEventHandler[object, request],
Expand All @@ -49,7 +49,7 @@ func NewEventHandler[object client.Object, request comparable](
}

// EventHandler adapts a handler.EventHandler interface to a cache.ResourceEventHandler interface.
type EventHandler[object client.Object, request comparable] struct {
type EventHandler[object any, request comparable] struct {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being changed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the definition of handler.TypedEventHandler doesn't constrain the object and allows any, neither does any of the source generic types. Without that change, the TypedInformer definition would also have to be constrained to client.Object which wouldn't be desirable, and it would limit its use.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it limit its use? What use-case exists where TypedInformer is typed to something that does not satisfy client.Object?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one here: https://github.com/kubernetes-sigs/aws-load-balancer-controller/pull/4678/changes#diff-8ce64d4b8a4655d407ba70bf654823f4f4d4e7a7036da24f85d1b1fe99bf6aebR281

It's possible to use cache.SharedIndexInformer{}.SetTransform() to transform the original object into an instance that doesn't satisfy client.Object.

// ctx stores the context that created the event handler
// that is used to propagate cancellation signals to each handler function.
ctx context.Context
Expand Down
16 changes: 10 additions & 6 deletions pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,23 @@ func (cs *channel[object, request]) syncLoop(ctx context.Context) {
}
}

// Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create).
type Informer struct {
// TypedInformer is used to provide a source of events originating inside the cluster from Watches using generic
// event handlers and predicates.
type TypedInformer[object any, request comparable] struct {
// Informer is the controller-runtime Informer
Informer cache.Informer
Handler handler.EventHandler
Predicates []predicate.Predicate
Handler handler.TypedEventHandler[object, request]
Predicates []predicate.TypedPredicate[object]
}

// Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create).
type Informer = TypedInformer[client.Object, reconcile.Request]

var _ Source = &Informer{}

// Start is internal and should be called only by the Controller to register an EventHandler with the Informer
// to enqueue reconcile.Requests.
func (is *Informer) Start(ctx context.Context, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) error {
func (is *TypedInformer[object, request]) Start(ctx context.Context, queue workqueue.TypedRateLimitingInterface[request]) error {
// Informer should have been specified by the user.
if is.Informer == nil {
return fmt.Errorf("must specify Informer.Informer")
Expand All @@ -294,7 +298,7 @@ func (is *Informer) Start(ctx context.Context, queue workqueue.TypedRateLimiting
return nil
}

func (is *Informer) String() string {
func (is *TypedInformer[object, request]) String() string {
return fmt.Sprintf("informer source: %p", is.Informer)
}

Expand Down