Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2657,3 +2657,43 @@ func TestReaderWaitsForCacheSync(t *testing.T) {
})
}
}

func TestIndexFieldDoesNotBlock(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
g := NewWithT(t)

fakeInformer := &controllertest.FakeInformer{Synced: false}
c, err := cache.New(&rest.Config{}, cache.Options{
Mapper: &fakeRESTMapper{},
NewInformer: func(kcache.ListerWatcher, runtime.Object, time.Duration, kcache.Indexers) kcache.SharedIndexInformer {
return fakeInformer
},
})
g.Expect(err).NotTo(HaveOccurred())

ctx, cancel := context.WithCancel(t.Context())
defer cancel()
cacheDone := make(chan struct{})
go func() {
g.Expect(c.Start(ctx)).To(Succeed())
close(cacheDone)
}()
synctest.Wait() // Let the cache finish starting

// Call IndexField before informer is synced with a short timeout
// If IndexField blocks waiting for sync, this will timeout
indexCtx, indexCancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer indexCancel()
fieldName := "testField"
indexFunc := func(obj client.Object) []string {
return []string{"test-value"}
}
pod := &corev1.Pod{}
err = c.IndexField(indexCtx, pod, fieldName, indexFunc)
g.Expect(err).NotTo(HaveOccurred())

cancel()
<-cacheDone
})
}
2 changes: 1 addition & 1 deletion pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (ic *informerCache) NeedLeaderElection() bool {
// The values may be anything. They will automatically be prefixed with the namespace of the
// given object, if present. The objects passed are guaranteed to be objects of the correct type.
func (ic *informerCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
informer, err := ic.GetInformer(ctx, obj)
informer, err := ic.GetInformer(ctx, obj, BlockUntilSynced(false))
if err != nil {
return err
}
Expand Down
Loading