Skip to content

Commit 1947a94

Browse files
authored
Merge pull request #3061 from alvaroaleman/export
✨Cache: Export NewInformer
2 parents fc16390 + d937d9a commit 1947a94

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
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")

pkg/cache/internal/informers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type InformersOpts struct {
4747
Mapper meta.RESTMapper
4848
ResyncPeriod time.Duration
4949
Namespace string
50-
NewInformer *func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
50+
NewInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
5151
Selector Selector
5252
Transform cache.TransformFunc
5353
UnsafeDisableDeepCopy bool
@@ -59,7 +59,7 @@ type InformersOpts struct {
5959
func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
6060
newInformer := cache.NewSharedIndexInformer
6161
if options.NewInformer != nil {
62-
newInformer = *options.NewInformer
62+
newInformer = options.NewInformer
6363
}
6464
return &Informers{
6565
config: config,

0 commit comments

Comments
 (0)