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

@@ -26,7 +26,7 @@ type ResAccumulator struct {
func MakeEmptyAccumulator() *ResAccumulator {
ra := &ResAccumulator{}
ra.resMap = make(resmap.ResMap)
ra.resMap = resmap.New()
ra.tConfig = &config.TransformerConfig{}
ra.varSet = types.VarSet{}
return ra
@@ -34,11 +34,7 @@ func MakeEmptyAccumulator() *ResAccumulator {
// ResMap returns a copy of the internal resMap.
func (ra *ResAccumulator) ResMap() resmap.ResMap {
result := make(resmap.ResMap)
for k, v := range ra.resMap {
result[k] = v
}
return result
return ra.resMap.ShallowCopy()
}
// Vars returns a copy of underlying vars.
@@ -101,7 +97,7 @@ func (ra *ResAccumulator) makeVarReplacementMap() (map[string]string, error) {
len(matched), v)
}
if len(matched) == 1 {
s, err := ra.resMap[matched[0]].GetFieldValue(v.FieldRef.FieldPath)
s, err := ra.resMap.GetById(matched[0]).GetFieldValue(v.FieldRef.FieldPath)
if err != nil {
return nil, fmt.Errorf(
"field specified in var '%v' "+

View File

@@ -41,54 +41,55 @@ func makeResAccumulator() (*ResAccumulator, *resource.Factory, error) {
}
rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl())
ra.MergeResourcesWithErrorOnIdCollision(resmap.ResMap{
resid.NewResId(
gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"},
"deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"command": []interface{}{
"myserver",
"--somebackendService $(SERVICE_ONE)",
"--yetAnother $(SERVICE_TWO)",
err = ra.MergeResourcesWithErrorOnIdCollision(
resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResId(
gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"},
"deploy1"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"command": []interface{}{
"myserver",
"--somebackendService $(SERVICE_ONE)",
"--yetAnother $(SERVICE_TWO)",
},
},
},
},
},
},
},
}),
resid.NewResId(
gvk.Gvk{Version: "v1", Kind: "Service"},
"backendOne"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendOne",
},
}),
resid.NewResId(
gvk.Gvk{Version: "v1", Kind: "Service"},
"backendTwo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendTwo",
},
}),
})
return ra, rf, nil
}),
resid.NewResId(
gvk.Gvk{Version: "v1", Kind: "Service"},
"backendOne"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendOne",
},
}),
resid.NewResId(
gvk.Gvk{Version: "v1", Kind: "Service"},
"backendTwo"): rf.FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendTwo",
},
}),
}))
return ra, rf, err
}
func TestResolveVarsHappy(t *testing.T) {
@@ -172,7 +173,8 @@ func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
ra.MergeResourcesWithErrorOnIdCollision(resmap.ResMap{
rm0 := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResIdWithPrefixNamespace(
gvk.Gvk{Version: "v1", Kind: "Service"},
"backendOne", "", "fooNamespace"): rf.FromMap(
@@ -180,11 +182,17 @@ func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendOne",
"name": "backendOne",
"namespace": "fooNamespace",
},
}),
})
err = ra.MergeResourcesWithErrorOnIdCollision(rm0)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
err = ra.MergeVars([]types.Var{
{
Name: "SERVICE_ONE",
@@ -263,7 +271,7 @@ func TestResolveVarsUnmappableVar(t *testing.T) {
}
func find(name string, resMap resmap.ResMap) *resource.Resource {
for k, v := range resMap {
for k, v := range resMap.AsMap() {
if k.Name() == name {
return v
}