fix golint, refactor test

This commit is contained in:
ymqytw 2018-01-23 12:55:52 -08:00
parent 808a43fad6
commit eec253d14c
8 changed files with 267 additions and 253 deletions

View File

@ -21,8 +21,8 @@ type ByGVKN []GroupVersionKindName
func (a ByGVKN) Len() int { return len(a) } func (a ByGVKN) Len() int { return len(a) }
func (a ByGVKN) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByGVKN) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByGVKN) Less(i, j int) bool { func (a ByGVKN) Less(i, j int) bool {
if a[i].gvk.String() != a[j].gvk.String() { if a[i].GVK.String() != a[j].GVK.String() {
return a[i].gvk.String() < a[j].gvk.String() return a[i].GVK.String() < a[j].GVK.String()
} }
return a[i].name < a[j].name return a[i].Name < a[j].Name
} }

View File

@ -22,35 +22,41 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
) )
type ApplyAdditionalMapOptions struct { // MapTransformationOptions contains a map string->string and path configs
additionalMap map[string]string // The map will be applied to the fields specified in path configs.
pathConfigs []PathConfig type MapTransformationOptions struct {
m map[string]string
pathConfigs []PathConfig
} }
var _ Transformer = &ApplyAdditionalMapOptions{} var _ Transformer = &MapTransformationOptions{}
func (o *ApplyAdditionalMapOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) { // CompleteForLabels fills up the MapTransformationOptions for labels transformation.
o.additionalMap = m func (o *MapTransformationOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) {
o.m = m
if pathConfigs == nil { if pathConfigs == nil {
pathConfigs = DefaultLabelsPathConfigs pathConfigs = DefaultLabelsPathConfigs
} }
o.pathConfigs = pathConfigs o.pathConfigs = pathConfigs
} }
func (o *ApplyAdditionalMapOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) { // CompleteForAnnotations fills up the MapTransformationOptions for annotations transformation.
o.additionalMap = m func (o *MapTransformationOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) {
o.m = m
if pathConfigs == nil { if pathConfigs == nil {
pathConfigs = DefaultAnnotationsPathConfigs pathConfigs = DefaultAnnotationsPathConfigs
} }
o.pathConfigs = pathConfigs o.pathConfigs = pathConfigs
} }
func (o *ApplyAdditionalMapOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error { // Transform apply each <key, value> pair in the MapTransformationOptions to the
// fields specified in MapTransformationOptions.
func (o *MapTransformationOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error {
for gvkn := range m { for gvkn := range m {
obj := m[gvkn] obj := m[gvkn]
objMap := obj.UnstructuredContent() objMap := obj.UnstructuredContent()
for _, path := range o.pathConfigs { for _, path := range o.pathConfigs {
if !SelectByGVK(gvkn.gvk, path.GroupVersionKind) { if !SelectByGVK(gvkn.GVK, path.GroupVersionKind) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addMap) err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addMap)
@ -62,26 +68,12 @@ func (o *ApplyAdditionalMapOptions) Transform(m map[GroupVersionKindName]*unstru
return nil return nil
} }
func (o *ApplyAdditionalMapOptions) TransformBytes(in []byte) ([]byte, error) { func (o *MapTransformationOptions) addMap(in interface{}) (interface{}, error) {
m, err := Decode(in)
if err != nil {
return nil, err
}
err = o.Transform(m)
if err != nil {
return nil, err
}
return Encode(m)
}
func (o *ApplyAdditionalMapOptions) addMap(in interface{}) (interface{}, error) {
m, ok := in.(map[string]interface{}) m, ok := in.(map[string]interface{})
if !ok { if !ok {
return nil, fmt.Errorf("%#v is expectd to be %T", in, m) return nil, fmt.Errorf("%#v is expectd to be %T", in, m)
} }
for k, v := range o.additionalMap { for k, v := range o.m {
m[k] = v m[k] = v
} }
return m, nil return m, nil

View File

@ -24,17 +24,17 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
) )
var labelsOps = ApplyAdditionalMapOptions{ var labelsOps = MapTransformationOptions{
additionalMap: map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"}, m: map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"},
pathConfigs: DefaultLabelsPathConfigs, pathConfigs: DefaultLabelsPathConfigs,
} }
var annotationsOps = ApplyAdditionalMapOptions{ var annotationsOps = MapTransformationOptions{
additionalMap: map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"}, m: map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"},
pathConfigs: DefaultAnnotationsPathConfigs, pathConfigs: DefaultAnnotationsPathConfigs,
} }
func getConfigmap() *unstructured.Unstructured { func makeConfigmap() *unstructured.Unstructured {
return &unstructured.Unstructured{ return &unstructured.Unstructured{
Object: map[string]interface{}{ Object: map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
@ -46,7 +46,7 @@ func getConfigmap() *unstructured.Unstructured {
} }
} }
func getDeployment() *unstructured.Unstructured { func makeDeployment() *unstructured.Unstructured {
return &unstructured.Unstructured{ return &unstructured.Unstructured{
Object: map[string]interface{}{ Object: map[string]interface{}{
"group": "apps", "group": "apps",
@ -76,7 +76,7 @@ func getDeployment() *unstructured.Unstructured {
} }
} }
func getService() *unstructured.Unstructured { func makeService() *unstructured.Unstructured {
return &unstructured.Unstructured{ return &unstructured.Unstructured{
Object: map[string]interface{}{ Object: map[string]interface{}{
"apiVersion": "v1", "apiVersion": "v1",
@ -96,225 +96,243 @@ func getService() *unstructured.Unstructured {
} }
} }
func getTestMap() map[GroupVersionKindName]*unstructured.Unstructured { func makeTestMap() map[GroupVersionKindName]*unstructured.Unstructured {
return map[GroupVersionKindName]*unstructured.Unstructured{ return map[GroupVersionKindName]*unstructured.Unstructured{
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
name: "cm1", Name: "cm1",
}: getConfigmap(), }: makeConfigmap(),
{ {
gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
name: "deploy1", Name: "deploy1",
}: getDeployment(), }: makeDeployment(),
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"},
name: "svc1", Name: "svc1",
}: getService(), }: makeService(),
} }
} }
var labeledObj1 = unstructured.Unstructured{ func makeLabeledConfigMap() *unstructured.Unstructured {
Object: map[string]interface{}{ return &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "ConfigMap", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "ConfigMap",
"name": "cm1", "metadata": map[string]interface{}{
"labels": map[string]interface{}{ "name": "cm1",
"label-key1": "label-value1", "labels": map[string]interface{}{
"label-key2": "label-value2",
},
},
},
}
var labeledObj2 = unstructured.Unstructured{
Object: map[string]interface{}{
"group": "apps",
"apiVersion": "v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
"labels": map[string]interface{}{
"label-key1": "label-value1",
"label-key2": "label-value2",
},
},
"spec": map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"label-key1": "label-value1", "label-key1": "label-value1",
"label-key2": "label-value2", "label-key2": "label-value2",
}, },
}, },
"template": map[string]interface{}{ },
"metadata": map[string]interface{}{ }
"labels": map[string]interface{}{ }
"old-label": "old-value",
func makeLabeledDeployment() *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"group": "apps",
"apiVersion": "v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "deploy1",
"labels": map[string]interface{}{
"label-key1": "label-value1",
"label-key2": "label-value2",
},
},
"spec": map[string]interface{}{
"selector": map[string]interface{}{
"matchLabels": map[string]interface{}{
"label-key1": "label-value1", "label-key1": "label-value1",
"label-key2": "label-value2", "label-key2": "label-value2",
}, },
}, },
"spec": map[string]interface{}{ "template": map[string]interface{}{
"containers": []interface{}{ "metadata": map[string]interface{}{
map[string]interface{}{ "labels": map[string]interface{}{
"name": "nginx", "old-label": "old-value",
"image": "nginx:1.7.9", "label-key1": "label-value1",
"label-key2": "label-value2",
},
},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "nginx",
"image": "nginx:1.7.9",
},
}, },
}, },
}, },
}, },
}, },
}, }
} }
var labeledObj3 = unstructured.Unstructured{ func makeLabeledService() *unstructured.Unstructured {
Object: map[string]interface{}{ return &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "Service", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "Service",
"name": "svc1", "metadata": map[string]interface{}{
"labels": map[string]interface{}{ "name": "svc1",
"label-key1": "label-value1", "labels": map[string]interface{}{
"label-key2": "label-value2", "label-key1": "label-value1",
}, "label-key2": "label-value2",
},
"spec": map[string]interface{}{
"ports": []interface{}{
map[string]interface{}{
"name": "port1",
"port": "12345",
}, },
}, },
"selector": map[string]interface{}{ "spec": map[string]interface{}{
"label-key1": "label-value1", "ports": []interface{}{
"label-key2": "label-value2", map[string]interface{}{
"name": "port1",
"port": "12345",
},
},
"selector": map[string]interface{}{
"label-key1": "label-value1",
"label-key2": "label-value2",
},
}, },
}, },
}, }
} }
var labeledM = map[GroupVersionKindName]*unstructured.Unstructured{ func makeLabeledMap() map[GroupVersionKindName]*unstructured.Unstructured {
{ return map[GroupVersionKindName]*unstructured.Unstructured{
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, {
name: "cm1", GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
}: &labeledObj1, Name: "cm1",
{ }: makeLabeledConfigMap(),
gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, {
name: "deploy1", GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
}: &labeledObj2, Name: "deploy1",
{ }: makeLabeledDeployment(),
gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, {
name: "svc1", GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"},
}: &labeledObj3, Name: "svc1",
}: makeLabeledService(),
}
} }
func TestLabelsRun(t *testing.T) { func TestLabelsRun(t *testing.T) {
m := getTestMap() m := makeTestMap()
err := labelsOps.Transform(m) err := labelsOps.Transform(m)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if !reflect.DeepEqual(m, labeledM) { expected := makeLabeledMap()
err = CompareMap(m, labeledM) if !reflect.DeepEqual(m, expected) {
err = compareMap(m, expected)
t.Fatalf("actual doesn't match expected: %v", err) t.Fatalf("actual doesn't match expected: %v", err)
} }
} }
var annotatedObj1 = unstructured.Unstructured{ func makeAnnotatededConfigMap() *unstructured.Unstructured {
Object: map[string]interface{}{ return &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "ConfigMap", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "ConfigMap",
"name": "cm1", "metadata": map[string]interface{}{
"annotations": map[string]interface{}{ "name": "cm1",
"anno-key1": "anno-value1", "annotations": map[string]interface{}{
"anno-key2": "anno-value2", "anno-key1": "anno-value1",
"anno-key2": "anno-value2",
},
}, },
}, },
}, }
} }
var annotatedObj2 = unstructured.Unstructured{ func makeAnnotatededDeployment() *unstructured.Unstructured {
Object: map[string]interface{}{ return &unstructured.Unstructured{
"group": "apps", Object: map[string]interface{}{
"apiVersion": "v1", "group": "apps",
"kind": "Deployment", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "Deployment",
"name": "deploy1", "metadata": map[string]interface{}{
"annotations": map[string]interface{}{ "name": "deploy1",
"anno-key1": "anno-value1", "annotations": map[string]interface{}{
"anno-key2": "anno-value2", "anno-key1": "anno-value1",
}, "anno-key2": "anno-value2",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": map[string]interface{}{
"anno-key1": "anno-value1",
"anno-key2": "anno-value2",
},
"labels": map[string]interface{}{
"old-label": "old-value",
},
}, },
"spec": map[string]interface{}{ },
"containers": []interface{}{ "spec": map[string]interface{}{
map[string]interface{}{ "template": map[string]interface{}{
"name": "nginx", "metadata": map[string]interface{}{
"image": "nginx:1.7.9", "annotations": map[string]interface{}{
"anno-key1": "anno-value1",
"anno-key2": "anno-value2",
},
"labels": map[string]interface{}{
"old-label": "old-value",
},
},
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "nginx",
"image": "nginx:1.7.9",
},
}, },
}, },
}, },
}, },
}, },
}, }
} }
var annotatedObj3 = unstructured.Unstructured{ func makeAnnotatededService() *unstructured.Unstructured {
Object: map[string]interface{}{ return &unstructured.Unstructured{
"apiVersion": "v1", Object: map[string]interface{}{
"kind": "Service", "apiVersion": "v1",
"metadata": map[string]interface{}{ "kind": "Service",
"name": "svc1", "metadata": map[string]interface{}{
"annotations": map[string]interface{}{ "name": "svc1",
"anno-key1": "anno-value1", "annotations": map[string]interface{}{
"anno-key2": "anno-value2", "anno-key1": "anno-value1",
"anno-key2": "anno-value2",
},
}, },
}, "spec": map[string]interface{}{
"spec": map[string]interface{}{ "ports": []interface{}{
"ports": []interface{}{ map[string]interface{}{
map[string]interface{}{ "name": "port1",
"name": "port1", "port": "12345",
"port": "12345", },
}, },
}, },
}, },
}, }
} }
var annotatedM = map[GroupVersionKindName]*unstructured.Unstructured{ func makeAnnotatedMap() map[GroupVersionKindName]*unstructured.Unstructured {
{ return map[GroupVersionKindName]*unstructured.Unstructured{
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, {
name: "cm1", GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
}: &annotatedObj1, Name: "cm1",
{ }: makeAnnotatededConfigMap(),
gvk: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}, {
name: "deploy1", GVK: schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"},
}: &annotatedObj2, Name: "deploy1",
{ }: makeAnnotatededDeployment(),
gvk: schema.GroupVersionKind{Version: "v1", Kind: "Service"}, {
name: "svc1", GVK: schema.GroupVersionKind{Version: "v1", Kind: "Service"},
}: &annotatedObj3, Name: "svc1",
}: makeAnnotatededService(),
}
} }
func TestAnnotationsRun(t *testing.T) { func TestAnnotationsRun(t *testing.T) {
m := getTestMap() m := makeTestMap()
err := annotationsOps.Transform(m) err := annotationsOps.Transform(m)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if !reflect.DeepEqual(m, annotatedM) { expected := makeAnnotatedMap()
err = CompareMap(m, annotatedM) if !reflect.DeepEqual(m, expected) {
err = compareMap(m, expected)
t.Fatalf("actual doesn't match expected: %v", err) t.Fatalf("actual doesn't match expected: %v", err)
} }
} }

View File

@ -20,6 +20,8 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
) )
// DefaultLabelsPathConfigs is the default configuration for mutating labels and
// selector fields for native k8s APIs.
var DefaultLabelsPathConfigs = []PathConfig{ var DefaultLabelsPathConfigs = []PathConfig{
{ {
Path: []string{"metadata", "labels"}, Path: []string{"metadata", "labels"},
@ -107,6 +109,8 @@ var DefaultLabelsPathConfigs = []PathConfig{
}, },
} }
// DefaultLabelsPathConfigs is the default configuration for mutating annotations
// fields for native k8s APIs.
var DefaultAnnotationsPathConfigs = []PathConfig{ var DefaultAnnotationsPathConfigs = []PathConfig{
{ {
Path: []string{"metadata", "annotations"}, Path: []string{"metadata", "annotations"},

View File

@ -51,7 +51,7 @@ func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.U
obj := m[gvkn] obj := m[gvkn]
objMap := obj.UnstructuredContent() objMap := obj.UnstructuredContent()
for _, path := range o.pathConfigs { for _, path := range o.pathConfigs {
if !SelectByGVK(gvkn.gvk, path.GroupVersionKind) { if !SelectByGVK(gvkn.GVK, path.GroupVersionKind) {
continue continue
} }
err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addPrefix) err := mutateField(objMap, path.Path, path.CreateIfNotPresent, o.addPrefix)
@ -63,20 +63,6 @@ func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.U
return nil return nil
} }
func (o *PrefixNameOptions) TransformBytes(in []byte) ([]byte, error) {
m, err := Decode(in)
if err != nil {
return nil, err
}
err = o.Transform(m)
if err != nil {
return nil, err
}
return Encode(m)
}
func (o *PrefixNameOptions) addPrefix(in interface{}) (interface{}, error) { func (o *PrefixNameOptions) addPrefix(in interface{}) (interface{}, error) {
s, ok := in.(string) s, ok := in.(string)
if !ok { if !ok {

View File

@ -51,12 +51,12 @@ var namePrefixedCm2 = unstructured.Unstructured{
var namePrefixedM = map[GroupVersionKindName]*unstructured.Unstructured{ var namePrefixedM = map[GroupVersionKindName]*unstructured.Unstructured{
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
name: "cm1", Name: "cm1",
}: &namePrefixedCm1, }: &namePrefixedCm1,
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
name: "cm2", Name: "cm2",
}: &namePrefixedCm2, }: &namePrefixedCm2,
} }

View File

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"reflect"
"sort" "sort"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
@ -31,13 +30,18 @@ import (
k8syaml "k8s.io/apimachinery/pkg/util/yaml" k8syaml "k8s.io/apimachinery/pkg/util/yaml"
) )
// GroupVersionKindName contains GroupVersionKind and original name of the resource.
type GroupVersionKindName struct { type GroupVersionKindName struct {
gvk schema.GroupVersionKind // GroupVersionKind of the resource.
// name of the resource. GVK schema.GroupVersionKind
name string // original name of the resource before transformation.
Name string
} }
func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, error) { // Decode decodes a list of objects in byte array format.
// Decoded object will be inserted in `into` if it's not nil. Otherwise, it will
// construct a new map and return it.
func Decode(in []byte, into map[GroupVersionKindName]*unstructured.Unstructured) (map[GroupVersionKindName]*unstructured.Unstructured, error) {
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024) decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewReader(in), 1024)
objs := []*unstructured.Unstructured{} objs := []*unstructured.Unstructured{}
@ -54,7 +58,9 @@ func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, err
return nil, err return nil, err
} }
m := map[GroupVersionKindName]*unstructured.Unstructured{} if into == nil {
into = map[GroupVersionKindName]*unstructured.Unstructured{}
}
for i := range objs { for i := range objs {
metaAccessor, err := meta.Accessor(objs[i]) metaAccessor, err := meta.Accessor(objs[i])
if err != nil { if err != nil {
@ -73,14 +79,15 @@ func Decode(in []byte) (map[GroupVersionKindName]*unstructured.Unstructured, err
} }
gvk := gv.WithKind(kind) gvk := gv.WithKind(kind)
gvkn := GroupVersionKindName{ gvkn := GroupVersionKindName{
gvk: gvk, GVK: gvk,
name: name, Name: name,
} }
m[gvkn] = objs[i] into[gvkn] = objs[i]
} }
return m, nil return into, nil
} }
// Encode encodes the map `in` and output the encoded objects separated by `---`.
func Encode(in map[GroupVersionKindName]*unstructured.Unstructured) ([]byte, error) { func Encode(in map[GroupVersionKindName]*unstructured.Unstructured) ([]byte, error) {
gvknList := []GroupVersionKindName{} gvknList := []GroupVersionKindName{}
for gvkn := range in { for gvkn := range in {
@ -112,7 +119,13 @@ func Encode(in map[GroupVersionKindName]*unstructured.Unstructured) ([]byte, err
return buf.Bytes(), nil return buf.Bytes(), nil
} }
// SelectByGVK returns true if selector is a superset of in; otherwise, false. // SelectByGVK returns true if `selector` selects `in`; otherwise, false.
// If `selector` and `in` are the same, return true.
// If `selector` is nil, it is considered as a wildcard and always return true.
// e.g. selector <Group: "", Version: "", Kind: "Deployemt"> CAN select
// <Group: "extensions", Version: "v1beta1", Kind: "Deployemt">.
// selector <Group: "apps", Version: "", Kind: "Deployemt"> CANNOT select
// <Group: "extensions", Version: "v1beta1", Kind: "Deployemt">.
func SelectByGVK(in schema.GroupVersionKind, selector *schema.GroupVersionKind) bool { func SelectByGVK(in schema.GroupVersionKind, selector *schema.GroupVersionKind) bool {
if selector == nil { if selector == nil {
return true return true
@ -135,30 +148,6 @@ func SelectByGVK(in schema.GroupVersionKind, selector *schema.GroupVersionKind)
return true return true
} }
func CompareMap(m1, m2 map[GroupVersionKindName]*unstructured.Unstructured) error {
if len(m1) != len(m2) {
keySet1 := []GroupVersionKindName{}
keySet2 := []GroupVersionKindName{}
for gvkn := range m1 {
keySet1 = append(keySet1, gvkn)
}
for gvkn := range m1 {
keySet2 = append(keySet2, gvkn)
}
return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2)
}
for gvkn, obj1 := range m1 {
obj2, found := m2[gvkn]
if !found {
return fmt.Errorf("%#v doesn't exist in %#v", gvkn, m2)
}
if !reflect.DeepEqual(obj1, obj2) {
return fmt.Errorf("%#v doesn't match %#v", obj1, obj2)
}
}
return nil
}
type mutateFunc func(interface{}) (interface{}, error) type mutateFunc func(interface{}) (interface{}, error)
func mutateField(m map[string]interface{}, pathToField []string, createIfNotPresent bool, fns ...mutateFunc) error { func mutateField(m map[string]interface{}, pathToField []string, createIfNotPresent bool, fns ...mutateFunc) error {

View File

@ -17,6 +17,7 @@ limitations under the License.
package util package util
import ( import (
"fmt"
"reflect" "reflect"
"testing" "testing"
@ -57,19 +58,19 @@ func createMap() map[GroupVersionKindName]*unstructured.Unstructured {
} }
return map[GroupVersionKindName]*unstructured.Unstructured{ return map[GroupVersionKindName]*unstructured.Unstructured{
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
name: "cm1", Name: "cm1",
}: &cm1, }: &cm1,
{ {
gvk: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}, GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
name: "cm2", Name: "cm2",
}: &cm2, }: &cm2,
} }
} }
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {
expected := createMap() expected := createMap()
m, err := Decode(encoded) m, err := Decode(encoded, nil)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -103,7 +104,7 @@ func TestFilterByGVK(t *testing.T) {
expected: true, expected: true,
}, },
{ {
description: "gvk matches", description: "GVK matches",
in: schema.GroupVersionKind{ in: schema.GroupVersionKind{
Group: "group1", Group: "group1",
Version: "version1", Version: "version1",
@ -195,3 +196,27 @@ func TestFilterByGVK(t *testing.T) {
} }
} }
} }
func compareMap(m1, m2 map[GroupVersionKindName]*unstructured.Unstructured) error {
if len(m1) != len(m2) {
keySet1 := []GroupVersionKindName{}
keySet2 := []GroupVersionKindName{}
for GVKn := range m1 {
keySet1 = append(keySet1, GVKn)
}
for GVKn := range m1 {
keySet2 = append(keySet2, GVKn)
}
return fmt.Errorf("maps has different number of entries: %#v doesn't equals %#v", keySet1, keySet2)
}
for GVKn, obj1 := range m1 {
obj2, found := m2[GVKn]
if !found {
return fmt.Errorf("%#v doesn't exist in %#v", GVKn, m2)
}
if !reflect.DeepEqual(obj1, obj2) {
return fmt.Errorf("%#v doesn't match %#v", obj1, obj2)
}
}
return nil
}