@@ -305,42 +305,63 @@ type multiNamespaceInformer struct {
305
305
namespaceToInformer map [string ]Informer
306
306
}
307
307
308
+ type handlerRegistration struct {
309
+ handles map [string ]toolscache.ResourceEventHandlerRegistration
310
+ }
311
+
312
+ type syncer interface {
313
+ HasSynced () bool
314
+ }
315
+
316
+ // HasSynced asserts that the handler has been called for the full initial state of the informer.
317
+ // This uses syncer to be compatible between client-go 1.27+ and older versions when the interface changed.
318
+ func (h handlerRegistration ) HasSynced () bool {
319
+ for _ , reg := range h .handles {
320
+ if s , ok := reg .(syncer ); ok {
321
+ if ! s .HasSynced () {
322
+ return false
323
+ }
324
+ }
325
+ }
326
+ return true
327
+ }
328
+
308
329
var _ Informer = & multiNamespaceInformer {}
309
330
310
331
// AddEventHandler adds the handler to each namespaced informer.
311
332
func (i * multiNamespaceInformer ) AddEventHandler (handler toolscache.ResourceEventHandler ) (toolscache.ResourceEventHandlerRegistration , error ) {
312
- handles := make (map [string ]toolscache.ResourceEventHandlerRegistration , len (i .namespaceToInformer ))
333
+ handles := handlerRegistration { handles : make (map [string ]toolscache.ResourceEventHandlerRegistration , len (i .namespaceToInformer ))}
313
334
for ns , informer := range i .namespaceToInformer {
314
335
registration , err := informer .AddEventHandler (handler )
315
336
if err != nil {
316
337
return nil , err
317
338
}
318
- handles [ns ] = registration
339
+ handles . handles [ns ] = registration
319
340
}
320
341
return handles , nil
321
342
}
322
343
323
344
// AddEventHandlerWithResyncPeriod adds the handler with a resync period to each namespaced informer.
324
345
func (i * multiNamespaceInformer ) AddEventHandlerWithResyncPeriod (handler toolscache.ResourceEventHandler , resyncPeriod time.Duration ) (toolscache.ResourceEventHandlerRegistration , error ) {
325
- handles := make (map [string ]toolscache.ResourceEventHandlerRegistration , len (i .namespaceToInformer ))
346
+ handles := handlerRegistration { handles : make (map [string ]toolscache.ResourceEventHandlerRegistration , len (i .namespaceToInformer ))}
326
347
for ns , informer := range i .namespaceToInformer {
327
348
registration , err := informer .AddEventHandlerWithResyncPeriod (handler , resyncPeriod )
328
349
if err != nil {
329
350
return nil , err
330
351
}
331
- handles [ns ] = registration
352
+ handles . handles [ns ] = registration
332
353
}
333
354
return handles , nil
334
355
}
335
356
336
357
// RemoveEventHandler removes a formerly added event handler given by its registration handle.
337
358
func (i * multiNamespaceInformer ) RemoveEventHandler (h toolscache.ResourceEventHandlerRegistration ) error {
338
- handles , ok := h .(map [ string ]toolscache. ResourceEventHandlerRegistration )
359
+ handles , ok := h .(handlerRegistration )
339
360
if ! ok {
340
361
return fmt .Errorf ("it is not the registration returned by multiNamespaceInformer" )
341
362
}
342
363
for ns , informer := range i .namespaceToInformer {
343
- registration , ok := handles [ns ]
364
+ registration , ok := handles . handles [ns ]
344
365
if ! ok {
345
366
continue
346
367
}
0 commit comments