From fdba7df3c15e8a81b8bfda86d164b40407ff1f6e Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Tue, 12 Feb 2019 12:28:08 -0800 Subject: [PATCH 1/3] if the kind matches '*List$', treat it as a list --- k8sdeps/kunstruct/factory.go | 2 +- pkg/resource/factory.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/k8sdeps/kunstruct/factory.go b/k8sdeps/kunstruct/factory.go index 09bb2bbba..cfd3ad309 100644 --- a/k8sdeps/kunstruct/factory.go +++ b/k8sdeps/kunstruct/factory.go @@ -108,7 +108,7 @@ func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error kind := u.GetKind() if kind == "" { return fmt.Errorf("missing kind in object %v", u) - } else if kind == "List" { + } else if strings.HasSuffix(kind, "List") { return nil } if u.GetName() == "" { diff --git a/pkg/resource/factory.go b/pkg/resource/factory.go index b016678a5..148323dd6 100644 --- a/pkg/resource/factory.go +++ b/pkg/resource/factory.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "log" + "strings" "sigs.k8s.io/kustomize/pkg/ifc" internal "sigs.k8s.io/kustomize/pkg/internal/error" @@ -94,10 +95,14 @@ func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) { for len(kunStructs) > 0 { u := kunStructs[0] kunStructs = kunStructs[1:] - if u.GetKind() == "List" { + if strings.HasSuffix(u.GetKind(), "List") { items := u.Map()["items"] itemsSlice, ok := items.([]interface{}) if !ok { + if items == nil { + // an empty list + continue + } return nil, fmt.Errorf("items in List is type %T, expected array", items) } for _, item := range itemsSlice { From 3118ccfd05eafd5d3eba8b290a3d1735b8e88c8a Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Tue, 12 Feb 2019 12:37:36 -0800 Subject: [PATCH 2/3] add tests for *List kinds and empty lists --- k8sdeps/kunstruct/factory_test.go | 27 +++++++++++++++++++++++++++ pkg/resource/factory_test.go | 15 ++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/k8sdeps/kunstruct/factory_test.go b/k8sdeps/kunstruct/factory_test.go index 51e2be793..4fe710934 100644 --- a/k8sdeps/kunstruct/factory_test.go +++ b/k8sdeps/kunstruct/factory_test.go @@ -42,6 +42,15 @@ func TestSliceFromBytes(t *testing.T) { testConfigMap.Map(), }, }) + testConfigMapList := factory.FromMap( + map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMapList", + "items": []interface{}{ + testConfigMap.Map(), + testConfigMap.Map(), + }, + }) tests := []struct { name string @@ -151,6 +160,24 @@ items: expectedOut: []ifc.Kunstructured{testList}, expectedErr: false, }, + { + name: "ConfigMapList", + input: []byte(` +apiVersion: v1 +kind: ConfigMapList +items: +- apiVersion: v1 + kind: ConfigMap + metadata: + name: winnie +- apiVersion: v1 + kind: ConfigMap + metadata: + name: winnie +`), + expectedOut: []ifc.Kunstructured{testConfigMapList}, + expectedErr: false, + }, } for _, test := range tests { diff --git a/pkg/resource/factory_test.go b/pkg/resource/factory_test.go index 45394f11d..b8138676f 100644 --- a/pkg/resource/factory_test.go +++ b/pkg/resource/factory_test.go @@ -68,7 +68,7 @@ items: patchList2 := patch.StrategicMerge("patch5.yaml") patch5 := ` apiVersion: v1 -kind: List +kind: DeploymentList items: - apiVersion: apps/v1 kind: Deployment @@ -87,6 +87,12 @@ items: name: deployment-b spec: <<: *hostAliases +` + patchList3 := patch.StrategicMerge("patch6.yaml") + patch6 := ` +apiVersion: v1 +kind: List +items: ` testDeploymentSpec := map[string]interface{}{ "template": map[string]interface{}{ @@ -126,6 +132,7 @@ items: l.AddFile("/"+string(patchBad), []byte(patch3)) l.AddFile("/"+string(patchList), []byte(patch4)) l.AddFile("/"+string(patchList2), []byte(patch5)) + l.AddFile("/"+string(patchList3), []byte(patch6)) tests := []struct { name string @@ -163,6 +170,12 @@ items: expectedOut: []*Resource{testDeploymentA, testDeploymentB}, expectedErr: false, }, + { + name: "listWithNoEntries", + input: []patch.StrategicMerge{patchList3}, + expectedOut: []*Resource{}, + expectedErr: false, + }, } for _, test := range tests { rs, err := factory.SliceFromPatches(l, test.input) From d72b16235accbcf5147c50b3cf39ee257bc5f20a Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Tue, 12 Feb 2019 15:07:46 -0800 Subject: [PATCH 3/3] add a test for a list with no 'items:' provided --- pkg/resource/factory_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/resource/factory_test.go b/pkg/resource/factory_test.go index b8138676f..8d9af62a3 100644 --- a/pkg/resource/factory_test.go +++ b/pkg/resource/factory_test.go @@ -93,6 +93,11 @@ items: apiVersion: v1 kind: List items: +` + patchList4 := patch.StrategicMerge("patch7.yaml") + patch7 := ` +apiVersion: v1 +kind: List ` testDeploymentSpec := map[string]interface{}{ "template": map[string]interface{}{ @@ -133,6 +138,7 @@ items: l.AddFile("/"+string(patchList), []byte(patch4)) l.AddFile("/"+string(patchList2), []byte(patch5)) l.AddFile("/"+string(patchList3), []byte(patch6)) + l.AddFile("/"+string(patchList4), []byte(patch7)) tests := []struct { name string @@ -176,6 +182,12 @@ items: expectedOut: []*Resource{}, expectedErr: false, }, + { + name: "listWithNo'items:'", + input: []patch.StrategicMerge{patchList4}, + expectedOut: []*Resource{}, + expectedErr: false, + }, } for _, test := range tests { rs, err := factory.SliceFromPatches(l, test.input)