Skip to content

Commit 66e8599

Browse files
committed
apply methods to funcs handler
Signed-off-by: Troy Connor <[email protected]>
1 parent eaf978d commit 66e8599

File tree

1 file changed

+83
-18
lines changed

1 file changed

+83
-18
lines changed

pkg/handler/eventhandler.go

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"time"
2222

23+
"k8s.io/apimachinery/pkg/types"
2324
"k8s.io/client-go/util/workqueue"
2425
"sigs.k8s.io/controller-runtime/pkg/client"
2526
"sigs.k8s.io/controller-runtime/pkg/controller/priorityqueue"
@@ -82,15 +83,87 @@ type TypedEventHandler[object any, request comparable] interface {
8283
Generic(context.Context, event.TypedGenericEvent[object], workqueue.TypedRateLimitingInterface[request])
8384
}
8485

85-
var _ EventHandler = Funcs{}
86+
var _ EventHandler = &Funcs{}
8687

8788
// Funcs implements eventhandler.
88-
type Funcs = TypedFuncs[client.Object, reconcile.Request]
89+
type Funcs = funcs[client.Object]
8990

90-
// NewFuncsWithLowPriority creates a Funcs Handler wrapped in
91-
// WithLowPriorityWhenUnchanged
92-
func NewFuncsWithLowPriority(u Funcs) EventHandler {
93-
return WithLowPriorityWhenUnchanged(u)
91+
// funcs implements eventhandler
92+
type funcs[object client.Object] struct{}
93+
94+
func (f *funcs[T]) Create(ctx context.Context, evt event.TypedCreateEvent[T], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
95+
if isNil(evt.Object) {
96+
enqueueLog.Error(nil, "CreateEvent received with no metadata", "event", evt)
97+
return
98+
}
99+
100+
item := reconcile.Request{NamespacedName: types.NamespacedName{
101+
Name: evt.Object.GetName(),
102+
Namespace: evt.Object.GetNamespace(),
103+
}}
104+
105+
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[reconcile.Request])
106+
if !isPriorityQueue {
107+
q.Add(item)
108+
return
109+
}
110+
addToPriorityQueueCreate(priorityQueue, evt, item)
111+
}
112+
113+
// Update implements EventHandler.
114+
func (f *funcs[T]) Update(ctx context.Context, evt event.TypedUpdateEvent[T], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
115+
switch {
116+
case !isNil(evt.ObjectNew):
117+
item := reconcile.Request{NamespacedName: types.NamespacedName{
118+
Name: evt.ObjectNew.GetName(),
119+
Namespace: evt.ObjectNew.GetNamespace(),
120+
}}
121+
122+
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[reconcile.Request])
123+
if !isPriorityQueue {
124+
q.Add(item)
125+
return
126+
}
127+
addToPriorityQueueUpdate(priorityQueue, evt, item)
128+
case !isNil(evt.ObjectOld):
129+
item := reconcile.Request{NamespacedName: types.NamespacedName{
130+
Name: evt.ObjectOld.GetName(),
131+
Namespace: evt.ObjectOld.GetNamespace(),
132+
}}
133+
134+
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[reconcile.Request])
135+
if !isPriorityQueue {
136+
q.Add(item)
137+
return
138+
}
139+
addToPriorityQueueUpdate(priorityQueue, evt, item)
140+
default:
141+
enqueueLog.Error(nil, "UpdateEvent received with no metadata", "event", evt)
142+
}
143+
}
144+
145+
// Delete implements EventHandler.
146+
func (f *funcs[T]) Delete(ctx context.Context, evt event.TypedDeleteEvent[T], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
147+
if isNil(evt.Object) {
148+
enqueueLog.Error(nil, "DeleteEvent received with no metadata", "event", evt)
149+
return
150+
}
151+
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
152+
Name: evt.Object.GetName(),
153+
Namespace: evt.Object.GetNamespace(),
154+
}})
155+
}
156+
157+
// Generic implements EventHandler.
158+
func (f *funcs[T]) Generic(ctx context.Context, evt event.TypedGenericEvent[T], q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
159+
if isNil(evt.Object) {
160+
enqueueLog.Error(nil, "GenericEvent received with no metadata", "event", evt)
161+
return
162+
}
163+
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
164+
Name: evt.Object.GetName(),
165+
Namespace: evt.Object.GetNamespace(),
166+
}})
94167
}
95168

96169
// TypedFuncs implements eventhandler.
@@ -160,11 +233,7 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty
160233
q.Add(item)
161234
return
162235
}
163-
var priority int
164-
if isObjectUnchanged(tce) {
165-
priority = LowPriority
166-
}
167-
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item)
236+
addToPriorityQueueCreate(priorityQueue, tce, item)
168237
},
169238
})
170239
},
@@ -177,11 +246,7 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty
177246
q.Add(item)
178247
return
179248
}
180-
var priority int
181-
if tue.ObjectOld.GetResourceVersion() == tue.ObjectNew.GetResourceVersion() {
182-
priority = LowPriority
183-
}
184-
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item)
249+
addToPriorityQueueUpdate(priorityQueue, tue, item)
185250
},
186251
})
187252
},
@@ -208,7 +273,7 @@ func isObjectUnchanged[object client.Object](e event.TypedCreateEvent[object]) b
208273

209274
// addToPriorityQueueCreate adds the reconcile.Request to the priorityqueue in the handler
210275
// for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
211-
func addToPriorityQueueCreate[T client.Object](q priorityqueue.PriorityQueue[reconcile.Request], evt event.TypedCreateEvent[T], item reconcile.Request) {
276+
func addToPriorityQueueCreate[T client.Object, request comparable](q priorityqueue.PriorityQueue[request], evt event.TypedCreateEvent[T], item request) {
212277
var priority int
213278
if isObjectUnchanged(evt) {
214279
priority = LowPriority
@@ -218,7 +283,7 @@ func addToPriorityQueueCreate[T client.Object](q priorityqueue.PriorityQueue[rec
218283

219284
// addToPriorityQueueUpdate adds the reconcile.Request to the priorityqueue in the handler
220285
// for Update requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
221-
func addToPriorityQueueUpdate[T client.Object](q priorityqueue.PriorityQueue[reconcile.Request], evt event.TypedUpdateEvent[T], item reconcile.Request) {
286+
func addToPriorityQueueUpdate[T client.Object, request comparable](q priorityqueue.PriorityQueue[request], evt event.TypedUpdateEvent[T], item request) {
222287
var priority int
223288
if evt.ObjectOld.GetResourceVersion() == evt.ObjectNew.GetResourceVersion() {
224289
priority = LowPriority

0 commit comments

Comments
 (0)