Skip to content

Commit ce46575

Browse files
florekszreigz
andauthored
fix(cache): ensure notify gets called only on actual cache changes, not tmp cache during refresh (#532)
Co-authored-by: Lukasz Zajaczkowski <[email protected]>
1 parent 0b4b249 commit ce46575

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

pkg/cache/discovery/cache.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,41 @@ func (in *cache) Refresh() error {
250250

251251
resourceWG.Wait()
252252

253+
// Added entries that are not in the cache.
254+
addedGVKs := gvkCache.Difference(in.gvkCache)
255+
addedGVRs := gvrCache.Difference(in.gvrCache)
256+
addedGVs := gvCache.Difference(in.gvCache)
257+
258+
for _, entry := range addedGVKs.List() {
259+
klog.V(log.LogLevelDebug).InfoS("gvk added", "gvk", entry)
260+
in.notifyGroupVersionKindAdded(entry)
261+
}
262+
263+
for _, entry := range addedGVRs.List() {
264+
klog.V(log.LogLevelDebug).InfoS("gvr added", "gvr", entry)
265+
in.notifyGroupVersionResourceAdded(entry)
266+
}
267+
268+
for _, entry := range addedGVs.List() {
269+
klog.V(log.LogLevelDebug).InfoS("gv added", "gv", entry)
270+
in.notifyGroupVersionAdded(entry)
271+
}
272+
273+
// Delete entries that are no longer in the discovery client.
253274
deletedGVKs := in.gvkCache.Difference(gvkCache)
275+
deletedGVRs := in.gvrCache.Difference(gvrCache)
276+
deletedGVs := in.gvCache.Difference(gvCache)
277+
254278
for _, entry := range deletedGVKs.List() {
255279
klog.V(log.LogLevelDebug).InfoS("gvk deleted", "gvk", entry)
256280
in.notifyGroupVersionKindDeleted(entry)
257281
}
258282

259-
deletedGVRs := in.gvrCache.Difference(gvrCache)
260283
for _, entry := range deletedGVRs.List() {
261284
klog.V(log.LogLevelDebug).InfoS("gvr deleted", "gvr", entry)
262285
in.notifyGroupVersionResourceDeleted(entry)
263286
}
264287

265-
deletedGVs := in.gvCache.Difference(gvCache)
266288
for _, entry := range deletedGVs.List() {
267289
klog.V(log.LogLevelDebug).InfoS("gv deleted", "gv", entry)
268290
in.notifyGroupVersionDeleted(entry)
@@ -444,7 +466,38 @@ func (in *cache) toGroupVersionResource(gvk schema.GroupVersionKind) (schema.Gro
444466
}
445467

446468
func (in *cache) add(gvk schema.GroupVersionKind) {
447-
in.addTo(gvk, in.gvkCache, in.gvrCache, in.gvCache, in.gvrToGVKCache)
469+
in.cacheMu.Lock()
470+
defer in.cacheMu.Unlock()
471+
472+
if !in.gvCache.Has(gvk.GroupVersion()) {
473+
in.gvCache.Add(gvk.GroupVersion())
474+
in.notifyGroupVersionAdded(gvk.GroupVersion())
475+
klog.V(log.LogLevelDebug).InfoS("added gv to cache", "gv", gvk.GroupVersion())
476+
}
477+
478+
// if kind is empty, we are dealing with a server group and version only, not a resource.
479+
if len(gvk.Kind) == 0 {
480+
return
481+
}
482+
483+
if !in.gvkCache.Has(gvk) {
484+
in.gvkCache.Add(gvk)
485+
in.notifyGroupVersionKindAdded(gvk)
486+
klog.V(log.LogLevelDebug).InfoS("added gvk to cache", "gvk", gvk)
487+
}
488+
489+
gvr, err := in.toGroupVersionResource(gvk)
490+
if err != nil {
491+
klog.V(log.LogLevelExtended).ErrorS(err, "unable to map gvk to gvr", "gvk", gvk)
492+
return
493+
}
494+
495+
if !in.gvrCache.Has(gvr) {
496+
in.gvrCache.Add(gvr)
497+
in.gvrToGVKCache.Set(gvr, gvk)
498+
in.notifyGroupVersionResourceAdded(gvr)
499+
klog.V(log.LogLevelDebug).InfoS("added gvr to cache", "gvr", gvr)
500+
}
448501
}
449502

450503
func (in *cache) addTo(
@@ -468,7 +521,6 @@ func (in *cache) addTo(
468521
func (in *cache) addGroupVersionTo(groupVersion schema.GroupVersion, gvCacheSet containers.Set[schema.GroupVersion]) {
469522
in.cacheMu.RLock()
470523
if gvCacheSet.Has(groupVersion) {
471-
klog.V(log.LogLevelDebug).InfoS("gv already in cache, skipping", "gv", groupVersion)
472524
in.cacheMu.RUnlock()
473525
return
474526
}
@@ -477,14 +529,11 @@ func (in *cache) addGroupVersionTo(groupVersion schema.GroupVersion, gvCacheSet
477529
in.cacheMu.Lock()
478530
gvCacheSet.Add(groupVersion)
479531
in.cacheMu.Unlock()
480-
in.notifyGroupVersionAdded(groupVersion)
481-
klog.V(log.LogLevelDebug).InfoS("added gv to cache", "gv", groupVersion)
482532
}
483533

484534
func (in *cache) addGroupVersionKindTo(gvk schema.GroupVersionKind, gvkSet containers.Set[schema.GroupVersionKind]) {
485535
in.cacheMu.RLock()
486536
if gvkSet.Has(gvk) {
487-
klog.V(log.LogLevelDebug).InfoS("gvk already in cache, skipping", "gvk", gvk)
488537
in.cacheMu.RUnlock()
489538
return
490539
}
@@ -493,8 +542,6 @@ func (in *cache) addGroupVersionKindTo(gvk schema.GroupVersionKind, gvkSet conta
493542
in.cacheMu.Lock()
494543
gvkSet.Add(gvk)
495544
in.cacheMu.Unlock()
496-
in.notifyGroupVersionKindAdded(gvk)
497-
klog.V(log.LogLevelDebug).InfoS("added gvk to cache", "gvk", gvk)
498545
}
499546

500547
func (in *cache) addGroupVersionResourceTo(gvk schema.GroupVersionKind, gvrSet containers.Set[schema.GroupVersionResource],
@@ -507,7 +554,6 @@ func (in *cache) addGroupVersionResourceTo(gvk schema.GroupVersionKind, gvrSet c
507554

508555
in.cacheMu.RLock()
509556
if gvrSet.Has(gvr) {
510-
klog.V(log.LogLevelDebug).InfoS("gvr already in cache, skipping", "gvr", gvr)
511557
in.cacheMu.RUnlock()
512558
return
513559
}
@@ -517,8 +563,6 @@ func (in *cache) addGroupVersionResourceTo(gvk schema.GroupVersionKind, gvrSet c
517563
gvrSet.Add(gvr)
518564
gvrToGVKMap.Set(gvr, gvk)
519565
in.cacheMu.Unlock()
520-
in.notifyGroupVersionResourceAdded(gvr)
521-
klog.V(log.LogLevelDebug).InfoS("added gvr to cache", "gvr", gvr)
522566
}
523567

524568
func (in *cache) hasGroupVersion(groupVersion schema.GroupVersion) bool {

0 commit comments

Comments
 (0)