Skip to content

Commit

Permalink
add namespace to id when create resmap from files
Browse files Browse the repository at this point in the history
  • Loading branch information
Liujingfang1 committed Oct 17, 2018
1 parent bad3ccd commit 14fc54e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
18 changes: 16 additions & 2 deletions pkg/resmap/factory_test.go
Expand Up @@ -45,6 +45,11 @@ metadata:
---
# some comment
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dply2
namespace: test
---
`

Expand All @@ -68,12 +73,21 @@ metadata:
"name": "dply2",
},
}),
resid.NewResIdWithPrefixNamespace(deploy, "dply2", "", "test"): rf.FromMap(
map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "dply2",
"namespace": "test",
},
}),
}

m, _ := rmF.FromFiles(
l, []string{"/home/seans/project/deployment.yaml"})
if len(m) != 2 {
t.Fatalf("%#v should contain 2 appResource, but got %d", m, len(m))
if len(m) != 3 {
t.Fatalf("%#v should contain 3 appResource, but got %d", m, len(m))
}

if err := expected.ErrorIfNotEqual(m); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/resource/factory_test.go
Expand Up @@ -39,6 +39,7 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: winnie
namespace: hundred-acre-wood
---
# some comment
---
Expand Down
3 changes: 2 additions & 1 deletion pkg/resource/resource.go
Expand Up @@ -58,7 +58,8 @@ func (r *Resource) IsGenerated() bool {

// Id returns the ResId for the resource.
func (r *Resource) Id() resid.ResId {
return resid.NewResId(r.GetGvk(), r.GetName())
namespace, _ := r.GetFieldValue("metadata.namespace")
return resid.NewResIdWithPrefixNamespace(r.GetGvk(), r.GetName(), "", namespace)
}

// Merge performs merge with other resource.
Expand Down
28 changes: 26 additions & 2 deletions pkg/resource/resource_test.go
Expand Up @@ -20,6 +20,8 @@ import (
"testing"

"sigs.k8s.io/kustomize/internal/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
)

var factory = NewFactory(
Expand All @@ -30,11 +32,12 @@ var testConfigMap = factory.FromMap(
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "winnie",
"name": "winnie",
"namespace": "hundred-acre-wood",
},
})

const testConfigMapString = `unspecified:{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie"}}`
const testConfigMapString = `unspecified:{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}`

var testDeployment = factory.FromMap(
map[string]interface{}{
Expand Down Expand Up @@ -67,3 +70,24 @@ func TestResourceString(t *testing.T) {
}
}
}

func TestResourceId(t *testing.T) {
tests := []struct {
in *Resource
id resid.ResId
}{
{
in: testConfigMap,
id: resid.NewResIdWithPrefixNamespace(gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "", "hundred-acre-wood"),
},
{
in: testDeployment,
id: resid.NewResId(gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}, "pooh"),
},
}
for _, test := range tests {
if test.in.Id() != test.id {
t.Fatalf("Expected %v, but got %v\n", test.id, test.in.Id())
}
}
}

0 comments on commit 14fc54e

Please sign in to comment.