Skip to content

Commit

Permalink
Merge pull request #755 from joelanford/fix-cache-race
Browse files Browse the repository at this point in the history
🐛 wait for cache start before returning from WaitForCacheSync
  • Loading branch information
k8s-ci-robot committed Jan 14, 2020
2 parents 0d78900 + faacf85 commit 8c39906
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/cache/internal/deleg_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ func (m *InformersMap) Start(stop <-chan struct{}) error {
return nil
}

// WaitForCacheSync waits until all the caches have been synced.
// WaitForCacheSync waits until all the caches have been started and synced.
func (m *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
syncedFuncs := append([]cache.InformerSynced(nil), m.structured.HasSyncedFuncs()...)
syncedFuncs = append(syncedFuncs, m.unstructured.HasSyncedFuncs()...)

if !m.structured.waitForStarted(stop) {
return false
}
if !m.unstructured.waitForStarted(stop) {
return false
}
return cache.WaitForCacheSync(stop, syncedFuncs...)
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/cache/internal/informers_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func newSpecificInformersMap(config *rest.Config,
codecs: serializer.NewCodecFactory(scheme),
paramCodec: runtime.NewParameterCodec(scheme),
resync: resync,
startWait: make(chan struct{}),
createListWatcher: createListWatcher,
namespace: namespace,
}
Expand Down Expand Up @@ -104,6 +105,10 @@ type specificInformersMap struct {
// start is true if the informers have been started
started bool

// startWait is a channel that is closed after the
// informer has been started.
startWait chan struct{}

// createClient knows how to create a client and a list object,
// and allows for abstracting over the particulars of structured vs
// unstructured objects.
Expand Down Expand Up @@ -131,10 +136,20 @@ func (ip *specificInformersMap) Start(stop <-chan struct{}) {

// Set started to true so we immediately start any informers added later.
ip.started = true
close(ip.startWait)
}()
<-stop
}

func (ip *specificInformersMap) waitForStarted(stop <-chan struct{}) bool {
select {
case <-ip.startWait:
return true
case <-stop:
return false
}
}

// HasSyncedFuncs returns all the HasSynced functions for the informers in this map.
func (ip *specificInformersMap) HasSyncedFuncs() []cache.InformerSynced {
ip.mu.RLock()
Expand Down

0 comments on commit 8c39906

Please sign in to comment.