@@ -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"
@@ -83,9 +84,73 @@ type TypedEventHandler[object any, request comparable] interface {
8384}
8485
8586var _ EventHandler = Funcs {}
87+ var _ EventHandler = TypedFuncs [client.Object , reconcile.Request ]{}
88+ var _ TypedEventHandler [client.Object , reconcile.Request ] = TypedFuncs [client.Object , reconcile.Request ]{}
8689
8790// Funcs implements eventhandler.
88- type Funcs = TypedFuncs [client.Object , reconcile.Request ]
91+ type Funcs = funcs [client.Object , reconcile.Request ]
92+
93+ // funcs implements eventhandler
94+ type funcs [object client.Object , request reconcile.Request ] struct {}
95+
96+ func (f funcs [T , R ]) Create (ctx context.Context , evt event.TypedCreateEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
97+ if isNil (evt .Object ) {
98+ enqueueLog .Error (nil , "CreateEvent received with no metadata" , "event" , evt )
99+ return
100+ }
101+
102+ item := reconcile.Request {NamespacedName : types.NamespacedName {
103+ Name : evt .Object .GetName (),
104+ Namespace : evt .Object .GetNamespace (),
105+ }}
106+ addToQueueCreate (q , evt , item )
107+ }
108+
109+ // Update implements EventHandler.
110+ func (f funcs [T , R ]) Update (ctx context.Context , evt event.TypedUpdateEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
111+ switch {
112+ case ! isNil (evt .ObjectNew ):
113+ item := reconcile.Request {NamespacedName : types.NamespacedName {
114+ Name : evt .ObjectNew .GetName (),
115+ Namespace : evt .ObjectNew .GetNamespace (),
116+ }}
117+
118+ addToQueueUpdate (q , evt , item )
119+ case ! isNil (evt .ObjectOld ):
120+ item := reconcile.Request {NamespacedName : types.NamespacedName {
121+ Name : evt .ObjectOld .GetName (),
122+ Namespace : evt .ObjectOld .GetNamespace (),
123+ }}
124+
125+ addToQueueUpdate (q , evt , item )
126+ default :
127+ enqueueLog .Error (nil , "UpdateEvent received with no metadata" , "event" , evt )
128+ }
129+ }
130+
131+ // Delete implements EventHandler.
132+ func (f funcs [T , R ]) Delete (ctx context.Context , evt event.TypedDeleteEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
133+ if isNil (evt .Object ) {
134+ enqueueLog .Error (nil , "DeleteEvent received with no metadata" , "event" , evt )
135+ return
136+ }
137+ q .Add (reconcile.Request {NamespacedName : types.NamespacedName {
138+ Name : evt .Object .GetName (),
139+ Namespace : evt .Object .GetNamespace (),
140+ }})
141+ }
142+
143+ // Generic implements EventHandler.
144+ func (f funcs [T , R ]) Generic (ctx context.Context , evt event.TypedGenericEvent [T ], q workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
145+ if isNil (evt .Object ) {
146+ enqueueLog .Error (nil , "GenericEvent received with no metadata" , "event" , evt )
147+ return
148+ }
149+ q .Add (reconcile.Request {NamespacedName : types.NamespacedName {
150+ Name : evt .Object .GetName (),
151+ Namespace : evt .Object .GetNamespace (),
152+ }})
153+ }
89154
90155// TypedFuncs implements eventhandler.
91156//
@@ -149,33 +214,15 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty
149214 u .Create (ctx , tce , workqueueWithCustomAddFunc [request ]{
150215 TypedRateLimitingInterface : trli ,
151216 addFunc : func (item request , q workqueue.TypedRateLimitingInterface [request ]) {
152- priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
153- if ! isPriorityQueue {
154- q .Add (item )
155- return
156- }
157- var priority int
158- if isObjectUnchanged (tce ) {
159- priority = LowPriority
160- }
161- priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
217+ addToQueueCreate (q , tce , item )
162218 },
163219 })
164220 },
165221 UpdateFunc : func (ctx context.Context , tue event.TypedUpdateEvent [object ], trli workqueue.TypedRateLimitingInterface [request ]) {
166222 u .Update (ctx , tue , workqueueWithCustomAddFunc [request ]{
167223 TypedRateLimitingInterface : trli ,
168224 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 )
225+ addToQueueUpdate (q , tue , item )
179226 },
180227 })
181228 },
@@ -199,3 +246,35 @@ func (w workqueueWithCustomAddFunc[request]) Add(item request) {
199246func isObjectUnchanged [object client.Object ](e event.TypedCreateEvent [object ]) bool {
200247 return e .Object .GetCreationTimestamp ().Time .Before (time .Now ().Add (- time .Minute ))
201248}
249+
250+ // addToQueueCreate adds the reconcile.Request to the priorityqueue in the handler
251+ // for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
252+ func addToQueueCreate [T client.Object , request comparable ](q workqueue.TypedRateLimitingInterface [request ], evt event.TypedCreateEvent [T ], item request ) {
253+ priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
254+ if ! isPriorityQueue {
255+ q .Add (item )
256+ return
257+ }
258+
259+ var priority int
260+ if isObjectUnchanged (evt ) {
261+ priority = LowPriority
262+ }
263+ priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
264+ }
265+
266+ // addToQueueUpdate adds the reconcile.Request to the priorityqueue in the handler
267+ // for Update requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request]
268+ func addToQueueUpdate [T client.Object , request comparable ](q workqueue.TypedRateLimitingInterface [request ], evt event.TypedUpdateEvent [T ], item request ) {
269+ priorityQueue , isPriorityQueue := q .(priorityqueue.PriorityQueue [request ])
270+ if ! isPriorityQueue {
271+ q .Add (item )
272+ return
273+ }
274+
275+ var priority int
276+ if evt .ObjectOld .GetResourceVersion () == evt .ObjectNew .GetResourceVersion () {
277+ priority = LowPriority
278+ }
279+ priorityQueue .AddWithOpts (priorityqueue.AddOpts {Priority : priority }, item )
280+ }
0 commit comments