From d916017d5e12a383454bcab1185e77675cfc64b6 Mon Sep 17 00:00:00 2001 From: alex <8968914+acpana@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:11:53 -0700 Subject: [PATCH] fix:enh: protect agg against empty gvks (#3040) Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com> --- pkg/cachemanager/aggregator/aggregator.go | 13 ++++++++++--- pkg/cachemanager/aggregator/aggregator_test.go | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/pkg/cachemanager/aggregator/aggregator.go b/pkg/cachemanager/aggregator/aggregator.go index 5b0b78aec63..b36b3cada13 100644 --- a/pkg/cachemanager/aggregator/aggregator.go +++ b/pkg/cachemanager/aggregator/aggregator.go @@ -85,10 +85,15 @@ func (b *GVKAgreggator) Upsert(k Key, gvks []schema.GroupVersionKind) error { } } - b.store[k] = makeSet(gvks) + // protect against empty inputs + gvksSet := makeSet(gvks) + if len(gvksSet) == 0 { + return nil + } + b.store[k] = gvksSet // add reverse links - for _, gvk := range gvks { + for gvk := range gvksSet { if _, found := b.reverseStore[gvk]; !found { b.reverseStore[gvk] = make(map[Key]struct{}) } @@ -148,7 +153,9 @@ func (b *GVKAgreggator) pruneReverseStore(gvks map[schema.GroupVersionKind]struc func makeSet(gvks []schema.GroupVersionKind) map[schema.GroupVersionKind]struct{} { gvkSet := make(map[schema.GroupVersionKind]struct{}) for _, gvk := range gvks { - gvkSet[gvk] = struct{}{} + if !gvk.Empty() { + gvkSet[gvk] = struct{}{} + } } return gvkSet diff --git a/pkg/cachemanager/aggregator/aggregator_test.go b/pkg/cachemanager/aggregator/aggregator_test.go index 5c579c40c89..22dec711065 100644 --- a/pkg/cachemanager/aggregator/aggregator_test.go +++ b/pkg/cachemanager/aggregator/aggregator_test.go @@ -16,6 +16,8 @@ const ( var ( // test gvks. + emptyGVK = schema.GroupVersionKind{Group: "", Version: "", Kind: ""} + g1v1k1 = schema.GroupVersionKind{Group: "group1", Version: "v1", Kind: "Kind1"} g1v1k2 = schema.GroupVersionKind{Group: "group1", Version: "v1", Kind: "Kind2"} @@ -46,6 +48,20 @@ func Test_GVKAggregator_Upsert(t *testing.T) { expectData map[Key]map[schema.GroupVersionKind]struct{} expectRev map[schema.GroupVersionKind]map[Key]struct{} }{ + { + name: "empty GVKs", + keyGVKs: []upsertKeyGVKs{ + { + key: Key{ + Source: syncset, + ID: "foo", + }, + gvks: []schema.GroupVersionKind{emptyGVK, emptyGVK}, + }, + }, + expectData: map[Key]map[schema.GroupVersionKind]struct{}{}, + expectRev: map[schema.GroupVersionKind]map[Key]struct{}{}, + }, { name: "add one key and GVKs", keyGVKs: []upsertKeyGVKs{ @@ -54,7 +70,7 @@ func Test_GVKAggregator_Upsert(t *testing.T) { Source: syncset, ID: "foo", }, - gvks: []schema.GroupVersionKind{g1v1k1, g1v1k2}, + gvks: []schema.GroupVersionKind{g1v1k1, g1v1k2, emptyGVK}, }, }, expectData: map[Key]map[schema.GroupVersionKind]struct{}{