kustomize namespace

This commit is contained in:
Seth Pollack
2018-06-04 22:30:03 -04:00
parent df2407ad3f
commit 6e54814b6d
4 changed files with 134 additions and 0 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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