Maintain resources in order loaded.

This commit is contained in:
Jeffrey Regan
2019-06-03 11:22:53 -07:00
parent af57fc3ece
commit 4162dbc2d8
39 changed files with 1074 additions and 617 deletions

View File

@@ -44,7 +44,7 @@ func (tf *transformer) Transform(baseResourceMap resmap.ResMap) error {
}
// Strategic merge the resources exist in both base and patches.
for _, patch := range patches {
for _, patch := range patches.Resources() {
// Merge patches with base resource.
id := patch.Id()
matchedIds := baseResourceMap.GetMatchingIds(id.GvknEquals)
@@ -55,7 +55,7 @@ func (tf *transformer) Transform(baseResourceMap resmap.ResMap) error {
return fmt.Errorf("found multiple objects %#v targeted by patch %#v (ambiguous)", matchedIds, id)
}
id = matchedIds[0]
base := baseResourceMap[id]
base := baseResourceMap.GetById(id)
merged := map[string]interface{}{}
versionedObj, err := scheme.Scheme.New(toSchemaGvk(id.Gvk()))
baseName := base.GetName()
@@ -97,7 +97,7 @@ func (tf *transformer) Transform(baseResourceMap resmap.ResMap) error {
return err
}
}
baseResourceMap[id].SetMap(merged)
baseResourceMap.GetById(id).SetMap(merged)
base.SetName(baseName)
}
return nil
@@ -106,12 +106,12 @@ func (tf *transformer) Transform(baseResourceMap resmap.ResMap) error {
// mergePatches merge and index patches by Id.
// It errors out if there is conflict between patches.
func (tf *transformer) mergePatches() (resmap.ResMap, error) {
rc := resmap.ResMap{}
rc := resmap.New()
for ix, patch := range tf.patches {
id := patch.Id()
existing, found := rc[id]
if !found {
rc[id] = patch
existing := rc.GetById(id)
if existing == nil {
rc.AppendWithId(id, patch)
continue
}
@@ -146,7 +146,7 @@ func (tf *transformer) mergePatches() (resmap.ResMap, error) {
if err != nil {
return nil, err
}
rc[id] = merged
rc.ReplaceResource(id, merged)
}
return rc, nil
}

View File

@@ -21,7 +21,7 @@ var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
var foo = gvk.Gvk{Group: "example.com", Version: "v1", Kind: "Foo"}
func TestOverlayRun(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
@@ -47,7 +47,7 @@ func TestOverlayRun(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "apps/v1",
@@ -81,7 +81,7 @@ func TestOverlayRun(t *testing.T) {
},
),
}
expected := resmap.ResMap{
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
@@ -114,7 +114,7 @@ func TestOverlayRun(t *testing.T) {
},
},
}),
}
})
lt, err := NewTransformer(patch, rf)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -130,7 +130,7 @@ func TestOverlayRun(t *testing.T) {
}
func TestMultiplePatches(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
@@ -151,7 +151,7 @@ func TestMultiplePatches(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "apps/v1",
@@ -209,7 +209,7 @@ func TestMultiplePatches(t *testing.T) {
},
),
}
expected := resmap.ResMap{
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
@@ -244,7 +244,7 @@ func TestMultiplePatches(t *testing.T) {
},
},
}),
}
})
lt, err := NewTransformer(patch, rf)
if err != nil {
t.Fatalf("unexpected error: %v", err)
@@ -260,7 +260,7 @@ func TestMultiplePatches(t *testing.T) {
}
func TestMultiplePatchesWithConflict(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
@@ -281,7 +281,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "apps/v1",
@@ -345,7 +345,7 @@ func TestMultiplePatchesWithConflict(t *testing.T) {
}
func TestNoSchemaOverlayRun(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(foo, "my-foo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -360,7 +360,7 @@ func TestNoSchemaOverlayRun(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -377,7 +377,7 @@ func TestNoSchemaOverlayRun(t *testing.T) {
},
),
}
expected := resmap.ResMap{
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(foo, "my-foo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -392,7 +392,7 @@ func TestNoSchemaOverlayRun(t *testing.T) {
},
},
}),
}
})
lt, err := NewTransformer(patch, rf)
if err != nil {
@@ -408,7 +408,7 @@ func TestNoSchemaOverlayRun(t *testing.T) {
}
func TestNoSchemaMultiplePatches(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(foo, "my-foo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -423,7 +423,7 @@ func TestNoSchemaMultiplePatches(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -457,7 +457,7 @@ func TestNoSchemaMultiplePatches(t *testing.T) {
},
),
}
expected := resmap.ResMap{
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(foo, "my-foo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -476,7 +476,7 @@ func TestNoSchemaMultiplePatches(t *testing.T) {
},
},
}),
}
})
lt, err := NewTransformer(patch, rf)
if err != nil {
@@ -492,7 +492,7 @@ func TestNoSchemaMultiplePatches(t *testing.T) {
}
func TestNoSchemaMultiplePatchesWithConflict(t *testing.T) {
base := resmap.ResMap{
base := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(foo, "my-foo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "example.com/v1",
@@ -507,7 +507,7 @@ func TestNoSchemaMultiplePatchesWithConflict(t *testing.T) {
},
},
}),
}
})
patch := []*resource.Resource{
rf.FromMap(map[string]interface{}{
"apiVersion": "example.com/v1",