Skip to content

Commit

Permalink
coll: move replacement map compatibility check to newCollectionLoader
Browse files Browse the repository at this point in the history
We want to make sure to ensure compatibility of all Maps passed in
opts.MapReplacements, not just the ones that get loaded using `loadMap`
later on. This avoids any surprises down the line.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed May 2, 2022
1 parent 6846ff0 commit 9f3cdaa
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,15 @@ func newCollectionLoader(coll *CollectionSpec, opts *CollectionOptions) (*collec
}

// Check for existing MapSpecs in the CollectionSpec for all provided replacement maps.
for name := range opts.MapReplacements {
if _, ok := coll.Maps[name]; !ok {
for name, m := range opts.MapReplacements {
spec, ok := coll.Maps[name]
if !ok {
return nil, fmt.Errorf("replacement map %s not found in CollectionSpec", name)
}

if err := spec.checkCompatibility(m); err != nil {
return nil, fmt.Errorf("using replacement map %s: %w", spec.Name, err)
}
}

return &collectionLoader{
Expand Down Expand Up @@ -451,9 +456,6 @@ func (cl *collectionLoader) loadMap(mapName string) (*Map, error) {
}

if replaceMap, ok := cl.opts.MapReplacements[mapName]; ok {
if err := mapSpec.checkCompatibility(replaceMap); err != nil {
return nil, fmt.Errorf("use replacement map %s: %w", mapSpec.Name, err)
}
// Clone the map to avoid closing user's map later on.
m, err := replaceMap.Clone()
if err != nil {
Expand Down

0 comments on commit 9f3cdaa

Please sign in to comment.