Add originalName field to resource.

This commit is contained in:
Jeffrey Regan
2019-06-12 14:48:17 -07:00
parent 4bb4a85037
commit 5e054c9d31
11 changed files with 465 additions and 425 deletions

View File

@@ -174,10 +174,9 @@ func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)
} }
rm0 := resmap.FromMap(map[resid.ResId]*resource.Resource{ rm0 := resmap.New()
resid.NewResIdWithPrefixNamespace( rm0.Append(
gvk.Gvk{Version: "v1", Kind: "Service"}, rf.FromMap(
"backendOne", "", "fooNamespace"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "Service", "kind": "Service",
@@ -185,9 +184,7 @@ func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
"name": "backendOne", "name": "backendOne",
"namespace": "fooNamespace", "namespace": "fooNamespace",
}, },
}), }))
})
err = ra.AppendAll(rm0) err = ra.AppendAll(rm0)
if err != nil { if err != nil {
t.Fatalf("unexpected err: %v", err) t.Fatalf("unexpected err: %v", err)

View File

@@ -59,14 +59,13 @@ func (f PatchJson6902Factory) makeOnePatchJson6902Transformer(p types.PatchJson6
return nil, fmt.Errorf("must specify the path for a json patch file") return nil, fmt.Errorf("must specify the path for a json patch file")
} }
targetId := resid.NewResIdWithPrefixNamespace( targetId := resid.NewResIdWithNamespace(
gvk.Gvk{ gvk.Gvk{
Group: p.Target.Group, Group: p.Target.Group,
Version: p.Target.Version, Version: p.Target.Version,
Kind: p.Target.Kind, Kind: p.Target.Kind,
}, },
p.Target.Name, p.Target.Name,
"",
p.Target.Namespace, p.Target.Namespace,
) )

View File

@@ -38,6 +38,12 @@ type ResId struct {
Suffix string `json:"suffix,omitempty"` Suffix string `json:"suffix,omitempty"`
} }
// NewResIdWithNamespace creates new resource identifier
// in a given namespace.
func NewResIdWithNamespace(k gvk.Gvk, n, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}}
}
// NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace // NewResIdWithPrefixSuffixNamespace creates new resource identifier with a prefix, suffix and a namespace
func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId { func NewResIdWithPrefixSuffixNamespace(k gvk.Gvk, n, p, s, ns string) ResId {
return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Prefix: p, Suffix: s} return ResId{ItemId: ItemId{Gvk: k, Name: n, Namespace: ns}, Prefix: p, Suffix: s}

View File

@@ -46,24 +46,26 @@ metadata:
if ferr := l.AddFile("/whatever/project/deployment.yaml", []byte(resourceStr)); ferr != nil { if ferr := l.AddFile("/whatever/project/deployment.yaml", []byte(resourceStr)); ferr != nil {
t.Fatalf("Error adding fake file: %v\n", ferr) t.Fatalf("Error adding fake file: %v\n", ferr)
} }
expected := FromMap(map[resid.ResId]*resource.Resource{ expected := New()
resid.NewResId(deploy, "dply1"): rf.FromMap( expected.Append(rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "apps/v1", "apiVersion": "apps/v1",
"kind": "Deployment", "kind": "Deployment",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "dply1", "name": "dply1",
}, },
}), }))
resid.NewResId(deploy, "dply2"): rf.FromMap( expected.Append(rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "apps/v1", "apiVersion": "apps/v1",
"kind": "Deployment", "kind": "Deployment",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "dply2", "name": "dply2",
}, },
}), }))
resid.NewResIdWithPrefixNamespace(deploy, "dply2", "", "test"): rf.FromMap( expected.AppendWithId(
resid.NewResIdWithNamespace(deploy, "dply2", "test"),
rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "apps/v1", "apiVersion": "apps/v1",
"kind": "Deployment", "kind": "Deployment",
@@ -71,8 +73,7 @@ metadata:
"name": "dply2", "name": "dply2",
"namespace": "test", "namespace": "test",
}, },
}), }))
})
m, _ := rmF.FromFile(l, "deployment.yaml") m, _ := rmF.FromFile(l, "deployment.yaml")
if m.Size() != 3 { if m.Size() != 3 {
t.Fatalf("result should contain 3, but got %d", m.Size()) t.Fatalf("result should contain 3, but got %d", m.Size())

View File

@@ -43,30 +43,39 @@ func (rf *Factory) Hasher() ifc.KunstructuredHasher {
// FromMap returns a new instance of Resource. // FromMap returns a new instance of Resource.
func (rf *Factory) FromMap(m map[string]interface{}) *Resource { func (rf *Factory) FromMap(m map[string]interface{}) *Resource {
return &Resource{ return rf.makeOne(rf.kf.FromMap(m), nil)
Kunstructured: rf.kf.FromMap(m),
options: types.NewGenArgs(nil, nil),
} }
// FromMapWithName returns a new instance with the given "original" name.
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource {
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalName(n)
} }
// FromMapAndOption returns a new instance of Resource with given options. // FromMapAndOption returns a new instance of Resource with given options.
func (rf *Factory) FromMapAndOption(m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *Resource { func (rf *Factory) FromMapAndOption(
return &Resource{ m map[string]interface{}, args *types.GeneratorArgs, option *types.GeneratorOptions) *Resource {
Kunstructured: rf.kf.FromMap(m), return rf.makeOne(rf.kf.FromMap(m), types.NewGenArgs(args, option))
options: types.NewGenArgs(args, option),
}
} }
// FromKunstructured returns a new instance of Resource. // FromKunstructured returns a new instance of Resource.
func (rf *Factory) FromKunstructured( func (rf *Factory) FromKunstructured(u ifc.Kunstructured) *Resource {
u ifc.Kunstructured) *Resource { return rf.makeOne(u, nil)
}
// makeOne returns a new instance of Resource.
func (rf *Factory) makeOne(
u ifc.Kunstructured, o *types.GenArgs) *Resource {
if u == nil { if u == nil {
log.Fatal("unstruct ifc must not be null") log.Fatal("unstruct ifc must not be null")
} }
return &Resource{ if o == nil {
Kunstructured: u, o = types.NewGenArgs(nil, nil)
options: types.NewGenArgs(nil, nil),
} }
r := &Resource{
Kunstructured: u,
options: o,
}
return r.setOriginalName(r.GetName())
} }
// SliceFromPatches returns a slice of resources given a patch path // SliceFromPatches returns a slice of resources given a patch path
@@ -88,7 +97,7 @@ func (rf *Factory) SliceFromPatches(
return result, nil return result, nil
} }
// FromBytes unmarshalls bytes into one Resource. // FromBytes unmarshals bytes into one Resource.
func (rf *Factory) FromBytes(in []byte) (*Resource, error) { func (rf *Factory) FromBytes(in []byte) (*Resource, error) {
result, err := rf.SliceFromBytes(in) result, err := rf.SliceFromBytes(in)
if err != nil { if err != nil {
@@ -101,7 +110,7 @@ func (rf *Factory) FromBytes(in []byte) (*Resource, error) {
return result[0], nil return result[0], nil
} }
// SliceFromBytes unmarshalls bytes into a Resource slice. // SliceFromBytes unmarshals bytes into a Resource slice.
func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) { func (rf *Factory) SliceFromBytes(in []byte) ([]*Resource, error) {
kunStructs, err := rf.kf.SliceFromBytes(in) kunStructs, err := rf.kf.SliceFromBytes(in)
if err != nil { if err != nil {
@@ -149,12 +158,11 @@ func (rf *Factory) MakeConfigMap(
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Resource{ return rf.makeOne(
Kunstructured: u, u,
options: types.NewGenArgs( types.NewGenArgs(
&types.GeneratorArgs{Behavior: args.Behavior}, &types.GeneratorArgs{Behavior: args.Behavior},
options), options)), nil
}, nil
} }
// MakeSecret makes an instance of Resource for Secret // MakeSecret makes an instance of Resource for Secret
@@ -166,10 +174,9 @@ func (rf *Factory) MakeSecret(
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Resource{ return rf.makeOne(
Kunstructured: u, u,
options: types.NewGenArgs( types.NewGenArgs(
&types.GeneratorArgs{Behavior: args.Behavior}, &types.GeneratorArgs{Behavior: args.Behavior},
options), options)), nil
}, nil
} }

View File

@@ -31,15 +31,69 @@ import (
// paired with a GenerationBehavior. // paired with a GenerationBehavior.
type Resource struct { type Resource struct {
ifc.Kunstructured ifc.Kunstructured
originalName string
options *types.GenArgs options *types.GenArgs
refBy []resid.ResId refBy []resid.ResId
refVarNames []string refVarNames []string
} }
// DeepCopy returns a new copy of resource
func (r *Resource) DeepCopy() *Resource {
rc := &Resource{
Kunstructured: r.Kunstructured.Copy(),
}
rc.copyOtherFields(r)
return rc
}
// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
r.SetAnnotations(
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
r.SetName(other.GetName())
r.copyOtherFields(other)
}
func (r *Resource) copyOtherFields(other *Resource) {
r.originalName = other.originalName
r.options = other.options
r.refBy = other.copyRefBy()
r.refVarNames = other.copyRefVarNames()
}
// Merge performs merge with other resource.
func (r *Resource) Merge(other *Resource) {
r.Replace(other)
mergeConfigmap(r.Map(), other.Map(), r.Map())
}
func (r *Resource) copyRefBy() []resid.ResId {
s := make([]resid.ResId, len(r.refBy))
copy(s, r.refBy)
return s
}
func (r *Resource) copyRefVarNames() []string {
s := make([]string, len(r.refVarNames))
copy(s, r.refVarNames)
return s
}
func (r *Resource) KunstructEqual(o *Resource) bool { func (r *Resource) KunstructEqual(o *Resource) bool {
return reflect.DeepEqual(r.Kunstructured, o.Kunstructured) return reflect.DeepEqual(r.Kunstructured, o.Kunstructured)
} }
func (r *Resource) GetOriginalName() string {
return r.originalName
}
// Making this public would be bad.
func (r *Resource) setOriginalName(n string) *Resource {
r.originalName = n
return r
}
// String returns resource as JSON. // String returns resource as JSON.
func (r *Resource) String() string { func (r *Resource) String() string {
bs, err := r.MarshalJSON() bs, err := r.MarshalJSON()
@@ -49,25 +103,6 @@ func (r *Resource) String() string {
return strings.TrimSpace(string(bs)) + r.options.String() return strings.TrimSpace(string(bs)) + r.options.String()
} }
// DeepCopy returns a new copy of resource
func (r *Resource) DeepCopy() *Resource {
rc := &Resource{
Kunstructured: r.Kunstructured.Copy(),
options: r.options,
}
if len(r.refBy) > 0 {
refby := make([]resid.ResId, len(r.refBy))
copy(refby, r.refBy)
rc.refBy = refby
}
if len(r.refVarNames) > 0 {
refVarNames := make([]string, len(r.refVarNames))
copy(refVarNames, r.refVarNames)
rc.refVarNames = refVarNames
}
return rc
}
// AsYAML returns the resource in Yaml form. // AsYAML returns the resource in Yaml form.
// Easier to read than JSON. // Easier to read than JSON.
func (r *Resource) AsYAML() ([]byte, error) { func (r *Resource) AsYAML() ([]byte, error) {
@@ -88,10 +123,17 @@ func (r *Resource) NeedHashSuffix() bool {
return r.options != nil && r.options.NeedsHashSuffix() return r.options != nil && r.options.NeedsHashSuffix()
} }
// GetNamespace returns the namespace the resource thinks it's in.
func (r *Resource) GetNamespace() string {
namespace, _ := r.GetFieldValue("metadata.namespace")
// if err, namespace is empty, so no need to check.
return namespace
}
// Id returns the ResId for the resource. // Id returns the ResId for the resource.
func (r *Resource) Id() resid.ResId { func (r *Resource) Id() resid.ResId {
namespace, _ := r.GetFieldValue("metadata.namespace") return resid.NewResIdWithPrefixNamespace(
return resid.NewResIdWithPrefixNamespace(r.GetGvk(), r.GetName(), "", namespace) r.GetGvk(), r.GetOriginalName(), "", r.GetNamespace())
} }
// GetRefBy returns the ResIds that referred to current resource // GetRefBy returns the ResIds that referred to current resource
@@ -114,21 +156,6 @@ func (r *Resource) AppendRefVarName(variable types.Var) {
r.refVarNames = append(r.refVarNames, variable.Name) r.refVarNames = append(r.refVarNames, variable.Name)
} }
// Merge performs merge with other resource.
func (r *Resource) Merge(other *Resource) {
r.Replace(other)
mergeConfigmap(r.Map(), other.Map(), r.Map())
}
// Replace performs replace with other resource.
func (r *Resource) Replace(other *Resource) {
r.SetLabels(mergeStringMaps(other.GetLabels(), r.GetLabels()))
r.SetAnnotations(
mergeStringMaps(other.GetAnnotations(), r.GetAnnotations()))
r.SetName(other.GetName())
r.options = other.options
}
// TODO: Add BinaryData once we sync to new k8s.io/api // TODO: Add BinaryData once we sync to new k8s.io/api
func mergeConfigmap( func mergeConfigmap(
mergedTo map[string]interface{}, mergedTo map[string]interface{},

View File

@@ -98,7 +98,8 @@ func TestResourceId(t *testing.T) {
}{ }{
{ {
in: testConfigMap, in: testConfigMap,
id: resid.NewResIdWithPrefixNamespace(gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "", "hundred-acre-wood"), id: resid.NewResIdWithNamespace(
gvk.Gvk{Version: "v1", Kind: "ConfigMap"}, "winnie", "hundred-acre-wood"),
}, },
{ {
in: testDeployment, in: testDeployment,

View File

@@ -28,20 +28,16 @@ import (
) )
var service = gvk.Gvk{Version: "v1", Kind: "Service"} var service = gvk.Gvk{Version: "v1", Kind: "Service"}
var secret = gvk.Gvk{Version: "v1", Kind: "Secret"}
var cmap = gvk.Gvk{Version: "v1", Kind: "ConfigMap"} var cmap = gvk.Gvk{Version: "v1", Kind: "ConfigMap"}
var ns = gvk.Gvk{Version: "v1", Kind: "Namespace"} var ns = gvk.Gvk{Version: "v1", Kind: "Namespace"}
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"} var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = gvk.Gvk{Group: "apps", Version: "v1", Kind: "StatefulSet"}
var crd = gvk.Gvk{Group: "apiextensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"} var crd = gvk.Gvk{Group: "apiextensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"}
var job = gvk.Gvk{Group: "batch", Version: "v1", Kind: "Job"} var job = gvk.Gvk{Group: "batch", Version: "v1", Kind: "Job"}
var cronjob = gvk.Gvk{Group: "batch", Version: "v1beta1", Kind: "CronJob"} var cronjob = gvk.Gvk{Group: "batch", Version: "v1beta1", Kind: "CronJob"}
var pv = gvk.Gvk{Version: "v1", Kind: "PersistentVolume"} var pv = gvk.Gvk{Version: "v1", Kind: "PersistentVolume"}
var pvc = gvk.Gvk{Version: "v1", Kind: "PersistentVolumeClaim"}
var cr = gvk.Gvk{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"} var cr = gvk.Gvk{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole"}
var crb = gvk.Gvk{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"} var crb = gvk.Gvk{Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding"}
var sa = gvk.Gvk{Version: "v1", Kind: "ServiceAccount"} var sa = gvk.Gvk{Version: "v1", Kind: "ServiceAccount"}
var ingress = gvk.Gvk{Kind: "Ingress"}
var rf = resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()) var rf = resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())
var defaultTransformerConfig = config.MakeDefaultConfig() var defaultTransformerConfig = config.MakeDefaultConfig()

View File

@@ -1,18 +1,5 @@
/* // Copyright 2019 The Kubernetes Authors.
Copyright 2018 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0
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 package transformers

View File

@@ -1,22 +1,10 @@
/* // Copyright 2019 The Kubernetes Authors.
Copyright 2018 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0
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 package transformers
import ( import (
"sigs.k8s.io/kustomize/pkg/gvk"
"strings" "strings"
"testing" "testing"
@@ -26,43 +14,93 @@ import (
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
) )
type rmFactory struct {
t *testing.T
m resmap.ResMap
rf *resource.Factory
}
func NewSeededRmFactory(t *testing.T, rf *resource.Factory, m resmap.ResMap) *rmFactory {
return &rmFactory{t: t, rf: rf, m: m}
}
func NewRmFactory(t *testing.T, rf *resource.Factory) *rmFactory {
return NewSeededRmFactory(t, rf, resmap.New())
}
func (rm *rmFactory) add(m map[string]interface{}) *rmFactory {
err := rm.m.Append(rm.rf.FromMap(m))
if err != nil {
rm.t.Fatalf("test setup failure: %v", err)
}
return rm
}
func (rm *rmFactory) addWithId(id resid.ResId, m map[string]interface{}) *rmFactory {
err := rm.m.AppendWithId(id, rm.rf.FromMap(m))
if err != nil {
rm.t.Fatalf("test setup failure: %v", err)
}
return rm
}
func (rm *rmFactory) addWithName(n string, m map[string]interface{}) *rmFactory {
err := rm.m.Append(rm.rf.FromMapWithName(n, m))
if err != nil {
rm.t.Fatalf("test setup failure: %v", err)
}
return rm
}
func (rm *rmFactory) replaceResource(m map[string]interface{}) *rmFactory {
r := rm.rf.FromMap(m)
err := rm.m.ReplaceResource(r.Id(), r)
if err != nil {
rm.t.Fatalf("test setup failure: %v", err)
}
return rm
}
func (rm *rmFactory) resMap() resmap.ResMap {
return rm.m
}
func TestNameReferenceHappyRun(t *testing.T) { func TestNameReferenceHappyRun(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl()) kunstruct.NewKunstructuredFactoryImpl())
m := resmap.FromMap(map[resid.ResId]*resource.Resource{ m := NewRmFactory(t, rf).addWithName(
resid.NewResId(cmap, "cm1"): rf.FromMap( "cm1",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ConfigMap", "kind": "ConfigMap",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-cm1-somehash", "name": "someprefix-cm1-somehash",
}, },
}), }).addWithName(
resid.NewResId(cmap, "cm2"): rf.FromMap( "cm2",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ConfigMap", "kind": "ConfigMap",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-cm2-somehash", "name": "someprefix-cm2-somehash",
}, },
}), }).addWithName(
resid.NewResId(secret, "secret1"): rf.FromMap( "secret1",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "Secret", "kind": "Secret",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-secret1-somehash", "name": "someprefix-secret1-somehash",
}, },
}), }).addWithName(
resid.NewResId(pvc, "claim1"): rf.FromMap( "claim1",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "PersistentVolumeClaim", "kind": "PersistentVolumeClaim",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-claim1", "name": "someprefix-claim1",
}, },
}), }).add(
resid.NewResId(ingress, "ingress1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "extensions", "group": "extensions",
"apiVersion": "v1beta1", "apiVersion": "v1beta1",
@@ -81,8 +119,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
), ).add(
resid.NewResId(deploy, "deploy1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "apps", "group": "apps",
"apiVersion": "v1", "apiVersion": "v1",
@@ -162,8 +199,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).add(
resid.NewResId(statefulset, "statefulset1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "apps", "group": "apps",
"apiVersion": "v1", "apiVersion": "v1",
@@ -195,8 +231,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).addWithName("sa",
resid.NewResIdWithPrefixNamespace(sa, "sa", "", "test"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ServiceAccount", "kind": "ServiceAccount",
@@ -204,8 +239,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
"name": "someprefix-sa", "name": "someprefix-sa",
"namespace": "test", "namespace": "test",
}, },
}), }).add(
resid.NewResId(crb, "crb"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRoleBinding", "kind": "ClusterRoleBinding",
@@ -219,8 +253,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
"namespace": "test", "namespace": "test",
}, },
}, },
}), }).add(
resid.NewResId(cr, "cr"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole", "kind": "ClusterRole",
@@ -239,8 +272,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).add(
resid.NewResId(cronjob, "cronjob1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "batch/v1beta1", "apiVersion": "batch/v1beta1",
"kind": "CronJob", "kind": "CronJob",
@@ -276,12 +308,9 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).resMap()
})
expected := m.ShallowCopy() expected := NewSeededRmFactory(t, rf, m.ShallowCopy()).replaceResource(
expected.ReplaceResource(resid.NewResId(deploy, "deploy1"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "apps", "group": "apps",
"apiVersion": "v1", "apiVersion": "v1",
@@ -361,8 +390,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
})) }).replaceResource(
expected.ReplaceResource(resid.NewResId(statefulset, "statefulset1"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "apps", "group": "apps",
"apiVersion": "v1", "apiVersion": "v1",
@@ -394,8 +422,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
})) }).replaceResource(
expected.ReplaceResource(resid.NewResId(ingress, "ingress1"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"group": "extensions", "group": "extensions",
"apiVersion": "v1beta1", "apiVersion": "v1beta1",
@@ -413,9 +440,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
"servicePort": "80", "servicePort": "80",
}, },
}, },
}, }).replaceResource(
))
expected.ReplaceResource(resid.NewResId(crb, "crb"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRoleBinding", "kind": "ClusterRoleBinding",
@@ -429,8 +454,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
"namespace": "test", "namespace": "test",
}, },
}, },
})) }).replaceResource(
expected.ReplaceResource(resid.NewResId(cr, "cr"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole", "kind": "ClusterRole",
@@ -449,8 +473,7 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
})) }).replaceResource(
expected.ReplaceResource(resid.NewResId(cronjob, "cronjob1"), rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "batch/v1beta1", "apiVersion": "batch/v1beta1",
"kind": "CronJob", "kind": "CronJob",
@@ -486,7 +509,8 @@ func TestNameReferenceHappyRun(t *testing.T) {
}, },
}, },
}, },
})) }).resMap()
nrt := NewNameReferenceTransformer(defaultTransformerConfig.NameReference) nrt := NewNameReferenceTransformer(defaultTransformerConfig.NameReference)
err := nrt.Transform(m) err := nrt.Transform(m)
if err != nil { if err != nil {
@@ -505,8 +529,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
expectedErr string expectedErr string
}{ }{
{ {
resMap: resmap.FromMap(map[resid.ResId]*resource.Resource{ resMap: NewRmFactory(t, rf).add(
resid.NewResId(cr, "cr"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole", "kind": "ClusterRole",
@@ -523,11 +546,10 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).resMap(),
}),
expectedErr: "is expected to be string"}, expectedErr: "is expected to be string"},
{resMap: resmap.FromMap(map[resid.ResId]*resource.Resource{ {
resid.NewResId(cr, "cr"): rf.FromMap( resMap: NewRmFactory(t, rf).add(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "rbac.authorization.k8s.io/v1", "apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "ClusterRole", "kind": "ClusterRole",
@@ -544,8 +566,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
}, },
}, },
}, },
}), }).resMap(),
}),
expectedErr: "is expected to be either a string or a []interface{}"}, expectedErr: "is expected to be either a string or a []interface{}"},
} }
@@ -566,17 +587,16 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) { func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
rf := resource.NewFactory( rf := resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl()) kunstruct.NewKunstructuredFactoryImpl())
m := resmap.FromMap(map[resid.ResId]*resource.Resource{ m := NewRmFactory(t, rf).addWithName(
resid.NewResId(pv, "volume1"): rf.FromMap( "volume1",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "PersistentVolume", "kind": "PersistentVolume",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-volume1", "name": "someprefix-volume1",
}, },
}), }).addWithName(
"claim1",
resid.NewResId(pvc, "claim1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "PersistentVolumeClaim", "kind": "PersistentVolumeClaim",
@@ -587,20 +607,18 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
"spec": map[string]interface{}{ "spec": map[string]interface{}{
"volumeName": "volume1", "volumeName": "volume1",
}, },
}), }).resMap()
})
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{ expected := NewRmFactory(t, rf).addWithName(
resid.NewResId(pv, "volume1"): rf.FromMap( "volume1",
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "PersistentVolume", "kind": "PersistentVolume",
"metadata": map[string]interface{}{ "metadata": map[string]interface{}{
"name": "someprefix-volume1", "name": "someprefix-volume1",
}, },
}), }).addWithName(
"claim1",
resid.NewResId(pvc, "claim1"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "PersistentVolumeClaim", "kind": "PersistentVolumeClaim",
@@ -611,9 +629,10 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
"spec": map[string]interface{}{ "spec": map[string]interface{}{
"volumeName": "someprefix-volume1", "volumeName": "someprefix-volume1",
}, },
}), }).resMap()
}) expected.GetById(
expected.GetById(resid.NewResId(pv, "volume1")).AppendRefBy(resid.NewResId(pvc, "claim1")) resid.NewResId(gvk.Gvk{Version: "v1", Kind: "PersistentVolume"}, "volume1")).AppendRefBy(
resid.NewResId(gvk.Gvk{Version: "v1", Kind: "PersistentVolumeClaim"}, "claim1"))
nrt := NewNameReferenceTransformer(defaultTransformerConfig.NameReference) nrt := NewNameReferenceTransformer(defaultTransformerConfig.NameReference)
err := nrt.Transform(m) err := nrt.Transform(m)
if err != nil { if err != nil {

View File

@@ -110,7 +110,7 @@ func TestNamespaceRun(t *testing.T) {
}), }),
}) })
expected := resmap.FromMap(map[resid.ResId]*resource.Resource{ expected := resmap.FromMap(map[resid.ResId]*resource.Resource{
resid.NewResIdWithPrefixNamespace(ns, "ns1", "", ""): rf.FromMap( resid.NewResIdWithNamespace(ns, "ns1", ""): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "Namespace", "kind": "Namespace",
@@ -118,7 +118,7 @@ func TestNamespaceRun(t *testing.T) {
"name": "ns1", "name": "ns1",
}, },
}), }),
resid.NewResIdWithPrefixNamespace(cmap, "cm1", "", "test"): rf.FromMap( resid.NewResIdWithNamespace(cmap, "cm1", "test"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ConfigMap", "kind": "ConfigMap",
@@ -127,7 +127,7 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "test", "namespace": "test",
}, },
}), }),
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "", "test"): rf.FromMap( resid.NewResIdWithNamespace(cmap, "cm2", "test"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ConfigMap", "kind": "ConfigMap",
@@ -136,10 +136,10 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "test", "namespace": "test",
}, },
}), }),
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "", "test"): rf.FromMap( resid.NewResIdWithNamespace(cmap, "cm3", "test"): rf.FromMap(
map[string]interface{}{}, map[string]interface{}{},
), ),
resid.NewResIdWithPrefixNamespace(sa, "default", "", "test"): rf.FromMap( resid.NewResIdWithNamespace(sa, "default", "test"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ServiceAccount", "kind": "ServiceAccount",
@@ -148,7 +148,7 @@ func TestNamespaceRun(t *testing.T) {
"namespace": "test", "namespace": "test",
}, },
}), }),
resid.NewResIdWithPrefixNamespace(sa, "service-account", "", "test"): rf.FromMap( resid.NewResIdWithNamespace(sa, "service-account", "test"): rf.FromMap(
map[string]interface{}{ map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
"kind": "ServiceAccount", "kind": "ServiceAccount",