Skip to content

Commit f21a2da

Browse files
committed
Avoid duplicate entries when same GVK is used
Signed-off-by: John Hunkins <[email protected]>
1 parent 7f7d2cc commit f21a2da

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

pkg/lib/catalogsource/image_template.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"reflect"
78
"regexp"
89
"strconv"
910
"strings"
@@ -237,9 +238,34 @@ func InitializeCatalogSourceTemplates(catalogSource *v1alpha1.CatalogSource) []G
237238
}
238239
jsonPath := subGroupMap[cap_subgrp_jsonpath]
239240

240-
foundGVKs = append(foundGVKs, key)
241+
// see if we've already added this key (don't add duplicates)
242+
gvkPresent := false
243+
for _, existingGVK := range foundGVKs {
244+
if reflect.DeepEqual(existingGVK, key) {
245+
gvkPresent = true
246+
}
247+
}
248+
if !gvkPresent {
249+
foundGVKs = append(foundGVKs, key)
250+
}
251+
241252
// add this entry to the map and append the jsonPath to the array
242-
gvkToJSONPathMap[key] = append(gvkToJSONPathMap[key], jsonPath)
253+
if existingJsonPaths, ok := gvkToJSONPathMap[key]; ok {
254+
// map already has this key, now find out if we've already added this path
255+
foundEntry := false
256+
for _, existingJsonPath := range existingJsonPaths {
257+
if jsonPath == existingJsonPath {
258+
foundEntry = true
259+
}
260+
}
261+
// if we did not find a jsonpath entry then add it now
262+
if !foundEntry {
263+
gvkToJSONPathMap[key] = append(gvkToJSONPathMap[key], jsonPath)
264+
}
265+
} else {
266+
gvkToJSONPathMap[key] = append(gvkToJSONPathMap[key], jsonPath)
267+
}
268+
243269
}
244270
}
245271
return foundGVKs

pkg/lib/catalogsource/image_template_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,41 @@ func TestImageTemplateFlow(t *testing.T) {
363363
expectedCatalogTemplate: "foo/v1/myimage",
364364
expectedUnresolvedTemplates: []string{},
365365
},
366+
{
367+
description: "multiple gvk template - reusing same gvk",
368+
catsrc: v1alpha1.CatalogSource{
369+
ObjectMeta: v1.ObjectMeta{
370+
Annotations: map[string]string{
371+
CatalogImageTemplateAnnotation: fmt.Sprintf("foo/v%s/%s", "{group:,version:v1,kind:Pod,name:fooA,namespace:bar,jsonpath:{.metadata.annotations.fooA}}", "{group:,version:v1,kind:Pod,name:fooA,namespace:bar,jsonpath:{.metadata.annotations.fooA}}"),
372+
},
373+
},
374+
},
375+
serverVersion: &defaultServerVersion,
376+
gvks: []*v1.PartialObjectMetadata{
377+
{
378+
TypeMeta: v1.TypeMeta{
379+
APIVersion: "v1",
380+
Kind: "Pod",
381+
},
382+
ObjectMeta: v1.ObjectMeta{
383+
Name: "fooA",
384+
Namespace: "bar",
385+
Annotations: map[string]string{
386+
"fooA": "1",
387+
},
388+
},
389+
},
390+
},
391+
expectedGVKKeys: []GVK_Key{
392+
{
393+
GroupVersionKind: schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"},
394+
name: "fooA",
395+
namespace: "bar",
396+
},
397+
},
398+
expectedCatalogTemplate: "foo/v1/1",
399+
expectedUnresolvedTemplates: []string{},
400+
},
366401
{
367402
description: "gvk template with missing object",
368403
catsrc: v1alpha1.CatalogSource{

0 commit comments

Comments
 (0)