Skip to content

Commit 025c1e2

Browse files
committed
✨Cache: Export NewInformer
Overwriting NewInformer may be useful for testing purposes and allows to bring a custom Informer implementation.
1 parent 711b48b commit 025c1e2

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

pkg/cache/cache.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ type Options struct {
239239
// If unset, this will fall through to the Default* settings.
240240
ByObject map[client.Object]ByObject
241241

242-
// newInformer allows overriding of NewSharedIndexInformer for testing.
243-
newInformer *func(toolscache.ListerWatcher, runtime.Object, time.Duration, toolscache.Indexers) toolscache.SharedIndexInformer
242+
// NewInformer allows overriding of NewSharedIndexInformer, for example for testing
243+
// or if someone wants to write their own Informer.
244+
NewInformer *func(toolscache.ListerWatcher, runtime.Object, time.Duration, toolscache.Indexers) toolscache.SharedIndexInformer
244245
}
245246

246247
// ByObject offers more fine-grained control over the cache's ListWatch by object.
@@ -432,7 +433,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
432433
WatchErrorHandler: opts.DefaultWatchErrorHandler,
433434
UnsafeDisableDeepCopy: ptr.Deref(config.UnsafeDisableDeepCopy, false),
434435
EnableWatchBookmarks: ptr.Deref(config.EnableWatchBookmarks, true),
435-
NewInformer: opts.newInformer,
436+
NewInformer: opts.NewInformer,
436437
}),
437438
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
438439
}

pkg/cache/cache_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,9 @@ func NonBlockingGetTest(createCacheFunc func(config *rest.Config, opts cache.Opt
544544
Expect(err).NotTo(HaveOccurred())
545545

546546
By("creating the informer cache")
547-
v := reflect.ValueOf(&opts).Elem()
548-
newInformerField := v.FieldByName("newInformer")
549-
newFakeInformer := func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcache.SharedIndexInformer {
547+
opts.NewInformer = &func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcache.SharedIndexInformer {
550548
return &controllertest.FakeInformer{Synced: false}
551549
}
552-
reflect.NewAt(newInformerField.Type(), newInformerField.Addr().UnsafePointer()).
553-
Elem().
554-
Set(reflect.ValueOf(&newFakeInformer))
555550
informerCache, err = createCacheFunc(cfg, opts)
556551
Expect(err).NotTo(HaveOccurred())
557552
By("running the cache and waiting for it to sync")

0 commit comments

Comments
 (0)