diff --git a/pkg/app/application.go b/pkg/app/application.go index 13e0cb2ef..464a37bc6 100644 --- a/pkg/app/application.go +++ b/pkg/app/application.go @@ -208,6 +208,8 @@ func (a *applicationImpl) getTransformer(patches []*resource.Resource) (transfor } ts = append(ts, ot) + ts = append(ts, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace))) + npt, err := transformers.NewDefaultingNamePrefixTransformer(string(a.kustomization.NamePrefix)) if err != nil { return nil, err diff --git a/pkg/transformers/namespace.go b/pkg/transformers/namespace.go new file mode 100644 index 000000000..3d118ce38 --- /dev/null +++ b/pkg/transformers/namespace.go @@ -0,0 +1,43 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package transformers + +import ( + "github.com/kubernetes-sigs/kustomize/pkg/resmap" +) + +type namespaceTransformer struct { + namespace string +} + +var _ Transformer = &namespaceTransformer{} + +// NewNamespaceTransformer construct a namespaceTransformer. +func NewNamespaceTransformer(ns string) Transformer { + if len(ns) == 0 { + return NewNoOpTransformer() + } + return &namespaceTransformer{namespace: ns} +} + +// Transform adds the namespace. +func (o *namespaceTransformer) Transform(m resmap.ResMap) error { + for _, obj := range m { + obj.Unstruct().SetNamespace(o.namespace) + } + return nil +} diff --git a/pkg/transformers/namespace_test.go b/pkg/transformers/namespace_test.go new file mode 100644 index 000000000..cb70474e3 --- /dev/null +++ b/pkg/transformers/namespace_test.go @@ -0,0 +1,86 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package transformers + +import ( + "reflect" + "testing" + + "github.com/kubernetes-sigs/kustomize/pkg/resmap" + "github.com/kubernetes-sigs/kustomize/pkg/resource" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func TestNamespaceRun(t *testing.T) { + m := resmap.ResMap{ + resource.NewResId(cmap, "cm1"): resource.NewBehaviorlessResource( + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm1", + }, + }, + }), + resource.NewResId(cmap, "cm2"): resource.NewBehaviorlessResource( + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm2", + "namespace": "foo", + }, + }, + }), + } + expected := resmap.ResMap{ + resource.NewResId(cmap, "cm1"): resource.NewBehaviorlessResource( + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm1", + "namespace": "test", + }, + }, + }), + resource.NewResId(cmap, "cm2"): resource.NewBehaviorlessResource( + &unstructured.Unstructured{ + Object: map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "cm2", + "namespace": "test", + }, + }, + }), + } + + nst := NewNamespaceTransformer("test") + err := nst.Transform(m) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !reflect.DeepEqual(m, expected) { + err = expected.ErrorIfNotEqual(m) + t.Fatalf("actual doesn't match expected: %v", err) + } +} diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 6cc23e6de..f389ee09a 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -22,6 +22,9 @@ type Kustomization struct { // file including generated configmaps and secrets. NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"` + // Namespace to add to all objects. + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + // Labels to add to all objects and selectors. // These labels would also be used to form the selector for apply --prune // Named differently than “labels” to avoid confusion with metadata for