@@ -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,10 +83,78 @@ 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 , reconcile.Request ]
90+
91+ // funcs implements eventhandler
92+ type funcs [object client.Object , request reconcile.Request ] struct {}
93+
94+ func (f * funcs [T , R ]) 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+ addToQueueCreate (priorityQueue , evt , item )
111+ }
112+
113+ // Update implements EventHandler.
114+ func (f * funcs [T , R ]) 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+ addToQueueUpdate (q , evt , item )
123+ case ! isNil (evt .ObjectOld ):
124+ item := reconcile.Request {NamespacedName : types.NamespacedName {
125+ Name : evt .ObjectOld .GetName (),
126+ Namespace : evt .ObjectOld .GetNamespace (),
127+ }}
128+
129+ addToQueueUpdate (q , evt , item )
130+ default :
131+ enqueueLog .Error (nil , "UpdateEvent received with no metadata" , "event" , evt )
132+ }
133+ }
134+
135+ // Delete implements EventHandler.
136+ func (f * funcs [T , R ]) Delete (ctx context.Context , evt event.TypedDeleteEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
137+ if isNil (evt .Object ) {
138+ enqueueLog .Error (nil , "DeleteEvent received with no metadata" , "event" , evt )
139+ return
140+ }
141+ q .Add (reconcile.Request {NamespacedName : types.NamespacedName {
142+ Name : evt .Object .GetName (),
143+ Namespace : evt .Object .GetNamespace (),
144+ }})
145+ }
146+
147+ // Generic implements EventHandler.
148+ func (f * funcs [T , R ]) Generic (ctx context.Context , evt event.TypedGenericEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
149+ if isNil (evt .Object ) {
150+ enqueueLog .Error (nil , "GenericEvent received with no metadata" , "event" , evt )
151+ return
152+ }
153+ q .Add (reconcile.Request {NamespacedName : types.NamespacedName {
154+ Name : evt .Object .GetName (),
155+ Namespace : evt .Object .GetNamespace (),
156+ }})
157+ }
89158
90159// TypedFuncs implements eventhandler.
91160//
@@ -154,28 +223,15 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty
154223 q .Add (item )
155224 return
156225 }
157- var priority int
158- if isObjectUnchanged (tce ) {
159- priority = LowPriority
160- }
161- priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
226+ addToQueueCreate (priorityQueue , tce , item )
162227 },
163228 })
164229 },
165230 UpdateFunc : func (ctx context.Context , tue event.TypedUpdateEvent [object ], trli workqueue.TypedRateLimitingInterface [request ]) {
166231 u .Update (ctx , tue , workqueueWithCustomAddFunc [request ]{
167232 TypedRateLimitingInterface : trli ,
168233 addFunc : func (item request , q workqueue.TypedRateLimitingInterface [request ]) {
169- priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
170- if ! isPriorityQueue {
171- q .Add (item )
172- return
173- }
174- var priority int
175- if tue .ObjectOld .GetResourceVersion () == tue .ObjectNew .GetResourceVersion () {
176- priority = LowPriority
177- }
178- priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
234+ addToQueueUpdate (q , tue , item )
179235 },
180236 })
181237 },
@@ -199,3 +255,35 @@ func (w workqueueWithCustomAddFunc[request]) Add(item request) {
199255func isObjectUnchanged [object client.Object ](e event.TypedCreateEvent [object ]) bool {
200256 return e .Object .GetCreationTimestamp ().Time .Before (time .Now ().Add (- time .Minute ))
201257}
258+
259+ // addToQueueCreate adds the reconcile.Request to the priorityqueue in the handler
260+ // for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
261+ func addToQueueCreate [T client.Object , request comparable ](q workqueue.TypedRateLimitingInterface [request ], evt event.TypedCreateEvent [T ], item request ) {
262+ priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
263+ if ! isPriorityQueue {
264+ q .Add (item )
265+ return
266+ }
267+
268+ var priority int
269+ if isObjectUnchanged (evt ) {
270+ priority = LowPriority
271+ }
272+ priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
273+ }
274+
275+ // addToQueueUpdate adds the reconcile.Request to the priorityqueue in the handler
276+ // for Update requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
277+ func addToQueueUpdate [T client.Object , request comparable ](q workqueue.TypedRateLimitingInterface [request ], evt event.TypedUpdateEvent [T ], item request ) {
278+ priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
279+ if ! isPriorityQueue {
280+ q .Add (item )
281+ return
282+ }
283+
284+ var priority int
285+ if evt .ObjectOld .GetResourceVersion () == evt .ObjectNew .GetResourceVersion () {
286+ priority = LowPriority
287+ }
288+ priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
289+ }
0 commit comments