Skip to content

Commit 20c4f7b

Browse files
Make event handler type aware
Signed-off-by: Danil Grigorev <[email protected]>
1 parent 67b27f2 commit 20c4f7b

File tree

6 files changed

+222
-44
lines changed

6 files changed

+222
-44
lines changed

pkg/builder/controller.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type WatchesInput struct {
135135
// This is the equivalent of calling
136136
// WatchesRawSource(source.Kind(cache, object), eventHandler, opts...).
137137
func (blder *Builder) Watches(object client.Object, eventHandler handler.EventHandler, opts ...WatchesOption) *Builder {
138-
src := source.Kind(blder.mgr.GetCache(), object)
138+
src := source.ObjectKind(blder.mgr.GetCache(), object)
139139
return blder.WatchesRawSource(src, eventHandler, opts...)
140140
}
141141

@@ -176,6 +176,9 @@ func (blder *Builder) WatchesMetadata(object client.Object, eventHandler handler
176176
//
177177
// STOP! Consider using For(...), Owns(...), Watches(...), WatchesMetadata(...) instead.
178178
// This method is only exposed for more advanced use cases, most users should use one of the higher level functions.
179+
//
180+
// Example:
181+
// WatchesRawSource(source.Kind(cache, &corev1.Pod{}), eventHandler, opts...) // ensure that source propagates only valid Pod objects.
179182
func (blder *Builder) WatchesRawSource(src source.Source, eventHandler handler.EventHandler, opts ...WatchesOption) *Builder {
180183
input := WatchesInput{src: src, eventHandler: eventHandler}
181184
for _, opt := range opts {
@@ -272,7 +275,7 @@ func (blder *Builder) doWatch() error {
272275
if err != nil {
273276
return err
274277
}
275-
src := source.Kind(blder.mgr.GetCache(), obj)
278+
src := source.ObjectKind(blder.mgr.GetCache(), obj)
276279
hdler := &handler.EnqueueRequestForObject{}
277280
allPredicates := append([]predicate.Predicate(nil), blder.globalPredicates...)
278281
allPredicates = append(allPredicates, blder.forInput.predicates...)
@@ -290,7 +293,7 @@ func (blder *Builder) doWatch() error {
290293
if err != nil {
291294
return err
292295
}
293-
src := source.Kind(blder.mgr.GetCache(), obj)
296+
src := source.ObjectKind(blder.mgr.GetCache(), obj)
294297
opts := []handler.OwnerOption{}
295298
if !own.matchEveryOwner {
296299
opts = append(opts, handler.OnlyControllerOwner())
@@ -313,7 +316,7 @@ func (blder *Builder) doWatch() error {
313316
}
314317
for _, w := range blder.watchesInput {
315318
// If the source of this watch is of type Kind, project it.
316-
if srcKind, ok := w.src.(*internalsource.Kind); ok {
319+
if srcKind, ok := w.src.(*internalsource.Kind[client.Object]); ok {
317320
typeForSrc, err := blder.project(srcKind.Type, w.objectProjection)
318321
if err != nil {
319322
return err

pkg/internal/source/event_handler.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
var log = logf.RuntimeLog.WithName("source").WithName("EventHandler")
3434

3535
// NewEventHandler creates a new EventHandler.
36-
func NewEventHandler(ctx context.Context, queue workqueue.RateLimitingInterface, handler handler.EventHandler, predicates []predicate.Predicate) *EventHandler {
37-
return &EventHandler{
36+
func NewEventHandler[T client.Object](ctx context.Context, queue workqueue.RateLimitingInterface, handler handler.EventHandler, predicates []predicate.Predicate) *EventHandler[T] {
37+
return &EventHandler[T]{
3838
ctx: ctx,
3939
handler: handler,
4040
queue: queue,
@@ -43,7 +43,7 @@ func NewEventHandler(ctx context.Context, queue workqueue.RateLimitingInterface,
4343
}
4444

4545
// EventHandler adapts a handler.EventHandler interface to a cache.ResourceEventHandler interface.
46-
type EventHandler struct {
46+
type EventHandler[T client.Object] struct {
4747
// ctx stores the context that created the event handler
4848
// that is used to propagate cancellation signals to each handler function.
4949
ctx context.Context
@@ -55,7 +55,7 @@ type EventHandler struct {
5555

5656
// HandlerFuncs converts EventHandler to a ResourceEventHandlerFuncs
5757
// TODO: switch to ResourceEventHandlerDetailedFuncs with client-go 1.27
58-
func (e *EventHandler) HandlerFuncs() cache.ResourceEventHandlerFuncs {
58+
func (e *EventHandler[T]) HandlerFuncs() cache.ResourceEventHandlerFuncs {
5959
return cache.ResourceEventHandlerFuncs{
6060
AddFunc: e.OnAdd,
6161
UpdateFunc: e.OnUpdate,
@@ -64,11 +64,11 @@ func (e *EventHandler) HandlerFuncs() cache.ResourceEventHandlerFuncs {
6464
}
6565

6666
// OnAdd creates CreateEvent and calls Create on EventHandler.
67-
func (e *EventHandler) OnAdd(obj interface{}) {
67+
func (e *EventHandler[T]) OnAdd(obj interface{}) {
6868
c := event.CreateEvent{}
6969

7070
// Pull Object out of the object
71-
if o, ok := obj.(client.Object); ok {
71+
if o, ok := obj.(T); ok {
7272
c.Object = o
7373
} else {
7474
log.Error(nil, "OnAdd missing Object",
@@ -89,10 +89,10 @@ func (e *EventHandler) OnAdd(obj interface{}) {
8989
}
9090

9191
// OnUpdate creates UpdateEvent and calls Update on EventHandler.
92-
func (e *EventHandler) OnUpdate(oldObj, newObj interface{}) {
92+
func (e *EventHandler[T]) OnUpdate(oldObj, newObj interface{}) {
9393
u := event.UpdateEvent{}
9494

95-
if o, ok := oldObj.(client.Object); ok {
95+
if o, ok := oldObj.(T); ok {
9696
u.ObjectOld = o
9797
} else {
9898
log.Error(nil, "OnUpdate missing ObjectOld",
@@ -101,7 +101,7 @@ func (e *EventHandler) OnUpdate(oldObj, newObj interface{}) {
101101
}
102102

103103
// Pull Object out of the object
104-
if o, ok := newObj.(client.Object); ok {
104+
if o, ok := newObj.(T); ok {
105105
u.ObjectNew = o
106106
} else {
107107
log.Error(nil, "OnUpdate missing ObjectNew",
@@ -122,7 +122,7 @@ func (e *EventHandler) OnUpdate(oldObj, newObj interface{}) {
122122
}
123123

124124
// OnDelete creates DeleteEvent and calls Delete on EventHandler.
125-
func (e *EventHandler) OnDelete(obj interface{}) {
125+
func (e *EventHandler[T]) OnDelete(obj interface{}) {
126126
d := event.DeleteEvent{}
127127

128128
// Deal with tombstone events by pulling the object out. Tombstone events wrap the object in a
@@ -131,7 +131,7 @@ func (e *EventHandler) OnDelete(obj interface{}) {
131131
// This should never happen if we aren't missing events, which we have concluded that we are not
132132
// and made decisions off of this belief. Maybe this shouldn't be here?
133133
var ok bool
134-
if _, ok = obj.(client.Object); !ok {
134+
if _, ok = obj.(T); !ok {
135135
// If the object doesn't have Metadata, assume it is a tombstone object of type DeletedFinalStateUnknown
136136
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
137137
if !ok {
@@ -149,7 +149,7 @@ func (e *EventHandler) OnDelete(obj interface{}) {
149149
}
150150

151151
// Pull Object out of the object
152-
if o, ok := obj.(client.Object); ok {
152+
if o, ok := obj.(T); ok {
153153
d.Object = o
154154
} else {
155155
log.Error(nil, "OnDelete missing Object",

0 commit comments

Comments
 (0)