Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of strategic merge patches #637

Merged
merged 5 commits into from Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion k8sdeps/kunstruct/factory.go
Expand Up @@ -101,8 +101,9 @@ func (kf *KunstructuredFactoryImpl) Set(fs fs.FileSystem, ldr ifc.Loader) {
}

// validate validates that u has kind and name
// except for kind `List`, which doesn't require a name
func (kf *KunstructuredFactoryImpl) validate(u unstructured.Unstructured) error {
if u.GetName() == "" {
if u.GetName() == "" && u.GetKind() != "List" {
hyww marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("missing metadata.name in object %v", u)
}
if u.GetKind() == "" {
Expand Down
27 changes: 27 additions & 0 deletions k8sdeps/kunstruct/factory_test.go
Expand Up @@ -33,6 +33,15 @@ func TestSliceFromBytes(t *testing.T) {
"name": "winnie",
},
})
testList := factory.FromMap(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test to cover the usage in #638

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one in pkg/resource/factory_test.go

map[string]interface{}{
"apiVersion": "v1",
"kind": "List",
"items": []interface{}{
testConfigMap.Map(),
testConfigMap.Map(),
},
})

tests := []struct {
name string
Expand Down Expand Up @@ -112,6 +121,24 @@ metadata:
expectedOut: nil,
expectedErr: true,
},
{
name: "List",
input: []byte(`
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
`),
expectedOut: []ifc.Kunstructured{testList},
expectedErr: false,
},
}

for _, test := range tests {
Expand Down
28 changes: 27 additions & 1 deletion pkg/resource/factory.go
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package resource

import (
"encoding/json"
"fmt"
"log"

"sigs.k8s.io/kustomize/pkg/fs"
Expand Down Expand Up @@ -66,7 +68,31 @@ func (rf *Factory) SliceFromPatches(
if err != nil {
return nil, internal.Handler(err, string(path))
}
result = append(result, res...)
for len(res) > 0 {
hyww marked this conversation as resolved.
Show resolved Hide resolved
r := res[0]
res = res[1:]
if r.GetKind() == "List" {
items := r.Map()["items"]
itemsSlice, ok := items.([]interface{})
if !ok {
return nil, fmt.Errorf("items in List is type %T, expected array.", items)
}
for _, item := range itemsSlice {
itemJSON, err := json.Marshal(item)
if err != nil {
return nil, err
}
innerRes, err := rf.SliceFromBytes(itemJSON)
if err != nil {
return nil, err
}
// append innerRes to res so nested Lists can be handled
res = append(res, innerRes...)
}
} else {
result = append(result, r)
}
}
}
return result, nil
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/resource/factory_test.go
Expand Up @@ -48,11 +48,27 @@ metadata:
patchBad := patch.StrategicMerge("patch3.yaml")
patch3 := `
WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT: woot
`
patchList := patch.StrategicMerge("patch4.yaml")
patch4 := `
apiVersion: v1
kind: List
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
name: pooh
- apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
namespace: hundred-acre-wood
`
l := loadertest.NewFakeLoader("/")
l.AddFile("/"+string(patchGood1), []byte(patch1))
l.AddFile("/"+string(patchGood2), []byte(patch2))
l.AddFile("/"+string(patchBad), []byte(patch3))
l.AddFile("/"+string(patchList), []byte(patch4))

tests := []struct {
name string
Expand All @@ -78,6 +94,12 @@ WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOT: woot
expectedOut: []*Resource{},
expectedErr: true,
},
{
name: "listOfPatches",
input: []patch.StrategicMerge{patchList},
expectedOut: []*Resource{testDeployment, testConfigMap},
expectedErr: false,
},
}
for _, test := range tests {
rs, err := factory.SliceFromPatches(l, test.input)
Expand Down