mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Merge pull request #81 from sethpollack/skip
refactor namespaces to use PathConfig
This commit is contained in:
@@ -169,8 +169,7 @@ func TestResources1(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",
|
||||
},
|
||||
|
||||
@@ -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"}
|
||||
|
||||
@@ -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{}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user