Skip to content

Commit b6abd76

Browse files
committed
add namespace in ResId
1 parent 3b3a272 commit b6abd76

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

pkg/app/application_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var svc = schema.GroupVersionKind{Version: "v1", Kind: "Service"}
9191

9292
func TestResources1(t *testing.T) {
9393
expected := resmap.ResMap{
94-
resource.NewResIdWithPrefix(deploy, "dply1", "foo-"): resource.NewResourceFromMap(
94+
resource.NewResIdWithPrefixNamespace(deploy, "dply1", "foo-", "ns1"): resource.NewResourceFromMap(
9595
map[string]interface{}{
9696
"apiVersion": "apps/v1",
9797
"kind": "Deployment",
@@ -123,7 +123,7 @@ func TestResources1(t *testing.T) {
123123
},
124124
},
125125
}),
126-
resource.NewResIdWithPrefix(cmap, "literalConfigMap", "foo-"): resource.NewResourceFromMap(
126+
resource.NewResIdWithPrefixNamespace(cmap, "literalConfigMap", "foo-", "ns1"): resource.NewResourceFromMap(
127127
map[string]interface{}{
128128
"apiVersion": "v1",
129129
"kind": "ConfigMap",
@@ -143,7 +143,7 @@ func TestResources1(t *testing.T) {
143143
"DB_PASSWORD": "somepw",
144144
},
145145
}).SetBehavior(resource.BehaviorCreate),
146-
resource.NewResIdWithPrefix(secret, "secret", "foo-"): resource.NewResourceFromMap(
146+
resource.NewResIdWithPrefixNamespace(secret, "secret", "foo-", "ns1"): resource.NewResourceFromMap(
147147
map[string]interface{}{
148148
"apiVersion": "v1",
149149
"kind": "Secret",
@@ -164,7 +164,7 @@ func TestResources1(t *testing.T) {
164164
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
165165
},
166166
}).SetBehavior(resource.BehaviorCreate),
167-
resource.NewResIdWithPrefix(ns, "ns1", "foo-"): resource.NewResourceFromMap(
167+
resource.NewResIdWithPrefixNamespace(ns, "ns1", "foo-", ""): resource.NewResourceFromMap(
168168
map[string]interface{}{
169169
"apiVersion": "v1",
170170
"kind": "Namespace",

pkg/resource/resid.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ type ResId struct {
3232
// an untransformed resource has no prefix, fully transformed resource has an arbitrary number of prefixes
3333
// concatenated together.
3434
prefix string
35+
// namespace the resource belongs to
36+
// an untransformed resource has no namespace, fully transformed resource has the namespace from
37+
// the top most overlay
38+
namespace string
39+
}
40+
41+
// NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
42+
func NewResIdWithPrefixNamespace(g schema.GroupVersionKind, n, p, ns string) ResId {
43+
return ResId{gvk: g, name: n, prefix: p, namespace: ns}
3544
}
3645

3746
// NewResIdWithPrefix creates new resource identifier with a prefix
@@ -46,7 +55,7 @@ func NewResId(g schema.GroupVersionKind, n string) ResId {
4655

4756
// String of ResId based on GVK, name and prefix
4857
func (n ResId) String() string {
49-
fields := []string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.prefix, n.name}
58+
fields := []string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.namespace, n.prefix, n.name}
5059
return strings.Join(fields, "_") + ".yaml"
5160
}
5261

@@ -81,7 +90,17 @@ func (n ResId) Prefix() string {
8190
return n.prefix
8291
}
8392

93+
// Namespace returns resource namespace.
94+
func (n ResId) Namespace() string {
95+
return n.namespace
96+
}
97+
8498
// CopyWithNewPrefix make a new copy from current ResId and append a new prefix
8599
func (n ResId) CopyWithNewPrefix(p string) ResId {
86-
return ResId{gvk: n.gvk, name: n.name, prefix: p + n.prefix}
100+
return ResId{gvk: n.gvk, name: n.name, prefix: p + n.prefix, namespace: n.namespace}
101+
}
102+
103+
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
104+
func (n ResId) CopyWithNewNamespace(ns string) ResId {
105+
return ResId{gvk: n.gvk, name: n.name, prefix: n.prefix, namespace: ns}
87106
}

pkg/transformers/namespace.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
7777
mf := resmap.ResMap{}
7878

7979
for id := range m {
80-
mf[id] = m[id]
80+
found := false
8181
for _, path := range o.skipPathConfigs {
8282
if selectByGVK(id.Gvk(), path.GroupVersionKind) {
83-
delete(mf, id)
83+
found = true
8484
break
8585
}
8686
}
87+
if !found {
88+
mf[id] = m[id]
89+
delete(m, id)
90+
}
8791
}
8892

8993
for id := range mf {
90-
objMap := m[id].UnstructuredContent()
94+
objMap := mf[id].UnstructuredContent()
9195
for _, path := range o.pathConfigs {
9296
if !selectByGVK(id.Gvk(), path.GroupVersionKind) {
9397
continue
@@ -99,6 +103,8 @@ func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
99103
if err != nil {
100104
return err
101105
}
106+
newid := id.CopyWithNewNamespace(o.namespace)
107+
m[newid] = mf[id]
102108
}
103109

104110
}

pkg/transformers/namespace_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ func TestNamespaceRun(t *testing.T) {
104104
}),
105105
}
106106
expected := resmap.ResMap{
107-
resource.NewResId(ns, "ns1"): resource.NewResourceFromMap(
107+
resource.NewResIdWithPrefixNamespace(ns, "ns1", "", ""): resource.NewResourceFromMap(
108108
map[string]interface{}{
109109
"apiVersion": "v1",
110110
"kind": "Namespace",
111111
"metadata": map[string]interface{}{
112112
"name": "ns1",
113113
},
114114
}),
115-
resource.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
115+
resource.NewResIdWithPrefixNamespace(cmap, "cm1", "", "test"): resource.NewResourceFromMap(
116116
map[string]interface{}{
117117
"apiVersion": "v1",
118118
"kind": "ConfigMap",
@@ -121,7 +121,7 @@ func TestNamespaceRun(t *testing.T) {
121121
"namespace": "test",
122122
},
123123
}),
124-
resource.NewResId(cmap, "cm2"): resource.NewResourceFromMap(
124+
resource.NewResIdWithPrefixNamespace(cmap, "cm2", "", "test"): resource.NewResourceFromMap(
125125
map[string]interface{}{
126126
"apiVersion": "v1",
127127
"kind": "ConfigMap",
@@ -130,7 +130,7 @@ func TestNamespaceRun(t *testing.T) {
130130
"namespace": "test",
131131
},
132132
}),
133-
resource.NewResId(sa, "default"): resource.NewResourceFromMap(
133+
resource.NewResIdWithPrefixNamespace(sa, "default", "", "test"): resource.NewResourceFromMap(
134134
map[string]interface{}{
135135
"apiVersion": "v1",
136136
"kind": "ServiceAccount",
@@ -139,7 +139,7 @@ func TestNamespaceRun(t *testing.T) {
139139
"namespace": "test",
140140
},
141141
}),
142-
resource.NewResId(sa, "service-account"): resource.NewResourceFromMap(
142+
resource.NewResIdWithPrefixNamespace(sa, "service-account", "", "test"): resource.NewResourceFromMap(
143143
map[string]interface{}{
144144
"apiVersion": "v1",
145145
"kind": "ServiceAccount",

0 commit comments

Comments
 (0)