Skip to content

Commit 82cc073

Browse files
authored
Merge pull request #3425 from troy0820/troy0820/cache-bug
🐛 Cache reader: Wait for cache sync when ReaderFailOnMissingInformer is true
2 parents aebc15d + 8a2b6fe commit 82cc073

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

pkg/cache/informer_cache.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,7 @@ var _ error = (*ErrCacheNotStarted)(nil)
5151
// ErrResourceNotCached indicates that the resource type
5252
// the client asked the cache for is not cached, i.e. the
5353
// corresponding informer does not exist yet.
54-
type ErrResourceNotCached struct {
55-
GVK schema.GroupVersionKind
56-
}
57-
58-
// Error returns the error
59-
func (r ErrResourceNotCached) Error() string {
60-
return fmt.Sprintf("%s is not cached", r.GVK.String())
61-
}
62-
63-
var _ error = (*ErrResourceNotCached)(nil)
54+
type ErrResourceNotCached = internal.ErrResourceNotCached
6455

6556
// informerCache is a Kubernetes Object cache populated from internal.Informers.
6657
// informerCache wraps internal.Informers.
@@ -157,7 +148,7 @@ func (ic *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou
157148
return nil, err
158149
}
159150

160-
_, i, err := ic.Informers.Get(ctx, gvk, obj, applyGetOptions(opts...))
151+
_, i, err := ic.Informers.Get(ctx, gvk, obj, false, applyGetOptions(opts...))
161152
if err != nil {
162153
return nil, err
163154
}
@@ -171,23 +162,19 @@ func (ic *informerCache) GetInformer(ctx context.Context, obj client.Object, opt
171162
return nil, err
172163
}
173164

174-
_, i, err := ic.Informers.Get(ctx, gvk, obj, applyGetOptions(opts...))
165+
_, i, err := ic.Informers.Get(ctx, gvk, obj, false, applyGetOptions(opts...))
175166
if err != nil {
176167
return nil, err
177168
}
178169
return i.Informer, nil
179170
}
180171

181172
func (ic *informerCache) getInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object) (bool, *internal.Cache, error) {
182-
if ic.readerFailOnMissingInformer {
183-
cache, started, ok := ic.Informers.Peek(gvk, obj)
184-
if !ok {
185-
return false, nil, &ErrResourceNotCached{GVK: gvk}
186-
}
187-
return started, cache, nil
173+
started, cache, err := ic.Informers.Get(ctx, gvk, obj, ic.readerFailOnMissingInformer, &internal.GetOptions{})
174+
if err != nil {
175+
return false, nil, err
188176
}
189-
190-
return ic.Informers.Get(ctx, gvk, obj, &internal.GetOptions{})
177+
return started, cache, nil
191178
}
192179

193180
// RemoveInformer deactivates and removes the informer from the cache.

pkg/cache/internal/informers.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ import (
4545

4646
var log = logf.RuntimeLog.WithName("cache")
4747

48+
// ErrResourceNotCached indicates that the resource type
49+
// the client asked the cache for is not cached, i.e. the
50+
// corresponding informer does not exist yet.
51+
type ErrResourceNotCached struct {
52+
GVK schema.GroupVersionKind
53+
}
54+
55+
// Error returns the error
56+
func (r ErrResourceNotCached) Error() string {
57+
return fmt.Sprintf("%s is not cached", r.GVK.String())
58+
}
59+
60+
var _ error = (*ErrResourceNotCached)(nil)
61+
4862
// InformersOpts configures an InformerMap.
4963
type InformersOpts struct {
5064
HTTPClient *http.Client
@@ -294,10 +308,13 @@ func (ip *Informers) Peek(gvk schema.GroupVersionKind, obj runtime.Object) (res
294308

295309
// Get will create a new Informer and add it to the map of specificInformersMap if none exists. Returns
296310
// the Informer from the map.
297-
func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object, opts *GetOptions) (bool, *Cache, error) {
311+
func (ip *Informers) Get(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object, readerFailOnMissingInformer bool, opts *GetOptions) (bool, *Cache, error) {
298312
// Return the informer if it is found
299313
i, started, ok := ip.Peek(gvk, obj)
300314
if !ok {
315+
if readerFailOnMissingInformer {
316+
return false, nil, &ErrResourceNotCached{GVK: gvk}
317+
}
301318
var err error
302319
if i, started, err = ip.addInformerToMap(gvk, obj); err != nil {
303320
return started, nil, err

0 commit comments

Comments
 (0)