@@ -3,11 +3,13 @@ package factory
33import (
44 "context"
55 "fmt"
6+ "reflect"
67 "time"
78
89 "github.com/robfig/cron"
910 "k8s.io/apimachinery/pkg/runtime"
1011 errorutil "k8s.io/apimachinery/pkg/util/errors"
12+ "k8s.io/apimachinery/pkg/util/sets"
1113 "k8s.io/client-go/tools/cache"
1214
1315 "github.com/openshift/library-go/pkg/operator/events"
@@ -245,6 +247,11 @@ func (f *Factory) WithControllerInstanceName(controllerInstanceName string) *Fac
245247 return f
246248}
247249
250+ type informerHandleTuple struct {
251+ informer Informer
252+ filter uintptr
253+ }
254+
248255// Controller produce a runnable controller.
249256func (f * Factory ) ToController (name string , eventRecorder events.Recorder ) Controller {
250257 if f .sync == nil {
@@ -286,19 +293,37 @@ func (f *Factory) ToController(name string, eventRecorder events.Recorder) Contr
286293 cacheSyncTimeout : defaultCacheSyncTimeout ,
287294 }
288295
296+ // avoid adding an informer more than once
297+ informerQueueKeySet := sets .New [informerHandleTuple ]()
289298 for i := range f .informerQueueKeys {
290299 for d := range f .informerQueueKeys [i ].informers {
291300 informer := f .informerQueueKeys [i ].informers [d ]
292301 queueKeyFn := f .informerQueueKeys [i ].queueKeyFn
293- informer .AddEventHandler (c .syncContext .(syncContext ).eventHandler (queueKeyFn , f .informerQueueKeys [i ].filter ))
302+ tuple := informerHandleTuple {
303+ informer : informer ,
304+ filter : reflect .ValueOf (f .informerQueueKeys [i ].filter ).Pointer (),
305+ }
306+ if ! informerQueueKeySet .Has (tuple ) {
307+ sets .Insert (informerQueueKeySet , tuple )
308+ informer .AddEventHandler (c .syncContext .(syncContext ).eventHandler (queueKeyFn , f .informerQueueKeys [i ].filter ))
309+ }
294310 c .cachesToSync = append (c .cachesToSync , informer .HasSynced )
295311 }
296312 }
297313
314+ // avoid adding an informer more than once
315+ informerSet := sets .New [informerHandleTuple ]()
298316 for i := range f .informers {
299317 for d := range f .informers [i ].informers {
300318 informer := f .informers [i ].informers [d ]
301- informer .AddEventHandler (c .syncContext .(syncContext ).eventHandler (DefaultQueueKeysFunc , f .informers [i ].filter ))
319+ tuple := informerHandleTuple {
320+ informer : informer ,
321+ filter : reflect .ValueOf (f .informers [i ].filter ).Pointer (),
322+ }
323+ if ! informerSet .Has (tuple ) {
324+ sets .Insert (informerSet , tuple )
325+ informer .AddEventHandler (c .syncContext .(syncContext ).eventHandler (DefaultQueueKeysFunc , f .informers [i ].filter ))
326+ }
302327 c .cachesToSync = append (c .cachesToSync , informer .HasSynced )
303328 }
304329 }
0 commit comments