Skip to content

Commit ac44c77

Browse files
committed
Verify informers have started before waiting to sync.
1 parent 726fd0a commit ac44c77

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

pkg/cache/cache.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,14 @@ func (o *objectCache) init(obj runtime.Object) error {
8181
if err != nil {
8282
return err
8383
}
84-
log.Info("Waiting to sync cache for type.", "Type", fmt.Sprintf("%T", obj))
85-
toolscache.WaitForCacheSync(o.informers.stop, i.HasSynced)
86-
log.Info("Finished to syncing cache for type.", "Type", fmt.Sprintf("%T", obj))
84+
if o.informers.started {
85+
log.Info("Waiting to sync cache for type.", "Type", fmt.Sprintf("%T", obj))
86+
toolscache.WaitForCacheSync(o.informers.stop, i.HasSynced)
87+
log.Info("Finished to syncing cache for type.", "Type", fmt.Sprintf("%T", obj))
88+
} else {
89+
return fmt.Errorf("must start Cache before calling Get or List %s %s",
90+
"Object", fmt.Sprintf("%T", obj))
91+
}
8792
return nil
8893
}
8994

pkg/source/source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"k8s.io/apimachinery/pkg/runtime"
2727
"k8s.io/client-go/util/workqueue"
2828

29+
"sync"
30+
2931
"github.com/kubernetes-sigs/controller-runtime/pkg/cache"
3032
"github.com/kubernetes-sigs/controller-runtime/pkg/predicate"
3133
)
@@ -67,6 +69,8 @@ type Kind struct {
6769

6870
// cache used to watch APIs
6971
cache cache.Cache
72+
73+
once sync.Once
7074
}
7175

7276
var _ Source = &Kind{}

0 commit comments

Comments
 (0)