Skip to content

Commit 2ae00db

Browse files
author
Ivan Shvedunov
committed
Don't crash on resource load errors
1 parent f9577ab commit 2ae00db

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

pkg/app/application_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package app
1919
import (
2020
"encoding/base64"
2121
"reflect"
22+
"strings"
2223
"testing"
2324

2425
"github.com/kubernetes-sigs/kustomize/pkg/constants"
@@ -197,6 +198,27 @@ func TestResources1(t *testing.T) {
197198
}
198199
}
199200

201+
func TestResourceNotFound(t *testing.T) {
202+
l := loadertest.NewFakeLoader("/testpath")
203+
err := l.AddFile("/testpath/"+constants.KustomizationFileName, []byte(kustomizationContent1))
204+
if err != nil {
205+
t.Fatalf("Failed to setup fake ldr.")
206+
}
207+
fakeFs := fs.MakeFakeFS()
208+
fakeFs.Mkdir("/")
209+
app, err := NewApplication(l, fakeFs)
210+
if err != nil {
211+
t.Fatalf("Unexpected construction error %v", err)
212+
}
213+
_, err = app.MakeCustomizedResMap()
214+
if err == nil {
215+
t.Fatalf("Didn't get the expected error for an unknown resource")
216+
}
217+
if !strings.Contains(err.Error(), `cannot read file "/testpath/deployment.yaml"`) {
218+
t.Fatalf("Unpexpected error message %q", err)
219+
}
220+
}
221+
200222
func TestRawResources1(t *testing.T) {
201223
expected := resmap.ResMap{
202224
resource.NewResId(deploy, "dply1"): resource.NewResourceFromMap(

pkg/resmap/resmap.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ func newResourceSliceFromBytes(in []byte) ([]*resource.Resource, error) {
211211
return result, nil
212212
}
213213

214-
// MergeWithoutOverride combines multiple ResMap instances, failing on key collision.
214+
// MergeWithoutOverride combines multiple ResMap instances, failing on key collision
215+
// and skipping nil maps. In case if all of the maps are nil, an empty ResMap is returned.
215216
func MergeWithoutOverride(maps ...ResMap) (ResMap, error) {
216217
result := ResMap{}
217218
for _, m := range maps {
219+
if m == nil {
220+
continue
221+
}
218222
for id, res := range m {
219223
if _, found := result[id]; found {
220224
return nil, fmt.Errorf("id '%q' already used", id)
@@ -226,14 +230,21 @@ func MergeWithoutOverride(maps ...ResMap) (ResMap, error) {
226230
}
227231

228232
// MergeWithOverride combines multiple ResMap instances, allowing and sometimes
229-
// demanding certain collisions.
233+
// demanding certain collisions and skipping nil maps.
234+
// In case if all of the maps are nil, an empty ResMap is returned.
230235
// When looping over the instances to combine them, if a resource id for resource X
231236
// is found to be already in the combined map, then the behavior field for X
232237
// must be BehaviorMerge or BehaviorReplace. If X is not in the map, then it's
233238
// behavior cannot be merge or replace.
234239
func MergeWithOverride(maps ...ResMap) (ResMap, error) {
235240
result := maps[0]
241+
if result == nil {
242+
result = ResMap{}
243+
}
236244
for _, m := range maps[1:] {
245+
if m == nil {
246+
continue
247+
}
237248
for id, r := range m {
238249
matchedId := result.FindByGVKN(id)
239250
if len(matchedId) == 1 {

pkg/resmap/resmap_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ func TestMergeWithoutOverride(t *testing.T) {
202202
if !reflect.DeepEqual(merged, expected) {
203203
t.Fatalf("%#v doesn't equal expected %#v", merged, expected)
204204
}
205+
input3 := []ResMap{merged, nil}
206+
merged1, err := MergeWithoutOverride(input3...)
207+
if err != nil {
208+
t.Fatalf("unexpected error: %v", err)
209+
}
210+
if !reflect.DeepEqual(merged1, expected) {
211+
t.Fatalf("%#v doesn't equal expected %#v", merged1, expected)
212+
}
213+
input4 := []ResMap{nil, merged}
214+
merged2, err := MergeWithoutOverride(input4...)
215+
if err != nil {
216+
t.Fatalf("unexpected error: %v", err)
217+
}
218+
if !reflect.DeepEqual(merged2, expected) {
219+
t.Fatalf("%#v doesn't equal expected %#v", merged2, expected)
220+
}
205221
}
206222

207223
func TestMergeWithOverride(t *testing.T) {
@@ -262,4 +278,20 @@ func TestMergeWithOverride(t *testing.T) {
262278
if !reflect.DeepEqual(merged, expected) {
263279
t.Fatalf("%#v doesn't equal expected %#v", merged, expected)
264280
}
281+
input3 := []ResMap{merged, nil}
282+
merged1, err := MergeWithOverride(input3...)
283+
if err != nil {
284+
t.Fatalf("unexpected error: %v", err)
285+
}
286+
if !reflect.DeepEqual(merged1, expected) {
287+
t.Fatalf("%#v doesn't equal expected %#v", merged1, expected)
288+
}
289+
input4 := []ResMap{nil, merged}
290+
merged2, err := MergeWithOverride(input4...)
291+
if err != nil {
292+
t.Fatalf("unexpected error: %v", err)
293+
}
294+
if !reflect.DeepEqual(merged2, expected) {
295+
t.Fatalf("%#v doesn't equal expected %#v", merged2, expected)
296+
}
265297
}

0 commit comments

Comments
 (0)