Skip to content

Commit

Permalink
refactor namespaces to use PathConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
sethpollack committed Jun 11, 2018
1 parent ba45b13 commit f774172
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pkg/app/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ func TestResources(t *testing.T) {
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "foo-ns1",
"namespace": "ns1",
"name": "foo-ns1",
"labels": map[string]interface{}{
"app": "nginx",
},
Expand Down
1 change: 1 addition & 0 deletions pkg/transformers/labelsandannotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
var service = schema.GroupVersionKind{Version: "v1", Kind: "Service"}
var secret = schema.GroupVersionKind{Version: "v1", Kind: "Secret"}
var cmap = schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}
var ns = schema.GroupVersionKind{Version: "v1", Kind: "Namespace"}
var deploy = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
var foo = schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "Foo"}
Expand Down
66 changes: 62 additions & 4 deletions pkg/transformers/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,38 @@ package transformers

import (
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type namespaceTransformer struct {
namespace string
namespace string
pathConfigs []PathConfig
skipPathConfigs []PathConfig
}

var namespacePathConfigs = []PathConfig{
{
Path: []string{"metadata", "namespace"},
CreateIfNotPresent: true,
},
}

var skipNamespacePathConfigs = []PathConfig{
{
GroupVersionKind: &schema.GroupVersionKind{
Kind: "Namespace",
},
},
{
GroupVersionKind: &schema.GroupVersionKind{
Kind: "ClusterRoleBinding",
},
},
{
GroupVersionKind: &schema.GroupVersionKind{
Kind: "ClusterRole",
},
},
}

var _ Transformer = &namespaceTransformer{}
Expand All @@ -31,13 +59,43 @@ func NewNamespaceTransformer(ns string) Transformer {
if len(ns) == 0 {
return NewNoOpTransformer()
}
return &namespaceTransformer{namespace: ns}

return &namespaceTransformer{
namespace: ns,
pathConfigs: namespacePathConfigs,
skipPathConfigs: skipNamespacePathConfigs,
}
}

// Transform adds the namespace.
func (o *namespaceTransformer) Transform(m resmap.ResMap) error {
for _, res := range m {
res.SetNamespace(o.namespace)
mf := resmap.ResMap{}

for id := range m {
mf[id] = m[id]
for _, path := range o.skipPathConfigs {
if selectByGVK(id.Gvk(), path.GroupVersionKind) {
delete(mf, id)
break
}
}
}

for id := range mf {
objMap := m[id].UnstructuredContent()
for _, path := range o.pathConfigs {
if !selectByGVK(id.Gvk(), path.GroupVersionKind) {
continue
}

err := mutateField(objMap, path.Path, path.CreateIfNotPresent, func(_ interface{}) (interface{}, error) {
return o.namespace, nil
})
if err != nil {
return err
}
}

}
return nil
}
16 changes: 16 additions & 0 deletions pkg/transformers/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,24 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "foo",
},
}),
resource.NewResId(ns, "ns1"): resource.NewResourceFromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "ns1",
},
}),
}
expected := resmap.ResMap{
resource.NewResId(ns, "ns1"): resource.NewResourceFromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": map[string]interface{}{
"name": "ns1",
},
}),
resource.NewResId(cmap, "cm1"): resource.NewResourceFromMap(
map[string]interface{}{
"apiVersion": "v1",
Expand Down

0 comments on commit f774172

Please sign in to comment.