Skip to content

Commit

Permalink
Merge pull request kubernetes#222 from mengqiy/format_nameprefix
Browse files Browse the repository at this point in the history
fix golink and refactor test
  • Loading branch information
k8s-ci-robot authored Jan 24, 2018
2 parents 66eb14f + 331dee4 commit f9dd21c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 72 deletions.
7 changes: 7 additions & 0 deletions pkg/kinflate/util/gvkn_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ limitations under the License.

package util

import (
"sort"
)

// ByGVKN implements the sort interface.
type ByGVKN []GroupVersionKindName

var _ sort.Interface = ByGVKN{}

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) Less(i, j int) bool {
Expand Down
6 changes: 6 additions & 0 deletions pkg/kinflate/util/labelsandannotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func makeLabeledMap() map[GroupVersionKindName]*unstructured.Unstructured {
func TestLabelsRun(t *testing.T) {
m := makeTestMap()
lt, err := NewDefaultingLabelsMapTransformer(map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = lt.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -318,6 +321,9 @@ func makeAnnotatedMap() map[GroupVersionKindName]*unstructured.Unstructured {
func TestAnnotationsRun(t *testing.T) {
m := makeTestMap()
at, err := NewDefaultingAnnotationsMapTransformer(map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = at.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/kinflate/util/pathconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
)

// PathConfig contains the configuration of a field, including the GVK it ties to,
// path to the field, etc.
type PathConfig struct {
// If true, it will create the path if it is not found.
CreateIfNotPresent bool
Expand Down
29 changes: 18 additions & 11 deletions pkg/kinflate/util/prefixname.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,43 @@ limitations under the License.
package util

import (
"errors"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// PrefixNameOptions contains the prefix and the path config for each field that
// NamePrefixTransformer contains the prefix and the path config for each field that
// the name prefix will be applied.
type PrefixNameOptions struct {
type NamePrefixTransformer struct {
prefix string
pathConfigs []PathConfig
}

var _ Transformer = &PrefixNameOptions{}
var _ Transformer = &NamePrefixTransformer{}

var DefaultNamePrefixPathConfigs = []PathConfig{
var defaultNamePrefixPathConfigs = []PathConfig{
{
Path: []string{"metadata", "name"},
CreateIfNotPresent: false,
},
}

func (o *PrefixNameOptions) Complete(prefix string, pathConfigs []PathConfig) {
o.prefix = prefix
if pathConfigs == nil {
pathConfigs = DefaultNamePrefixPathConfigs
// NewDefaultingNamePrefixTransformer construct a NamePrefixTransformer with defaultNamePrefixPathConfigs.
func NewDefaultingNamePrefixTransformer(nameprefix string) (Transformer, error) {
return NewNamePrefixTransformer(defaultNamePrefixPathConfigs, nameprefix)
}

// NewNamePrefixTransformer construct a NamePrefixTransformer.
func NewNamePrefixTransformer(pc []PathConfig, np string) (Transformer, error) {
if pc == nil {
return nil, errors.New("pathConfigs is not expected to be nil")
}
o.pathConfigs = pathConfigs
return &NamePrefixTransformer{pathConfigs: pc, prefix: np}, nil
}

func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error {
// Transform prepends the name prefix.
func (o *NamePrefixTransformer) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error {
for gvkn := range m {
obj := m[gvkn]
objMap := obj.UnstructuredContent()
Expand All @@ -63,7 +70,7 @@ func (o *PrefixNameOptions) Transform(m map[GroupVersionKindName]*unstructured.U
return nil
}

func (o *PrefixNameOptions) addPrefix(in interface{}) (interface{}, error) {
func (o *NamePrefixTransformer) addPrefix(in interface{}) (interface{}, error) {
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expectd to be %T", in, s)
Expand Down
53 changes: 10 additions & 43 deletions pkg/kinflate/util/prefixname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,21 @@ package util
import (
"reflect"
"testing"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var prefixNameOps = PrefixNameOptions{
prefix: "someprefix-",
pathConfigs: DefaultNamePrefixPathConfigs,
}

var namePrefixedCm1 = unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm1",
},
},
}

var namePrefixedCm2 = unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "someprefix-cm2",
},
},
}

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

func TestPrefixNameRun(t *testing.T) {
m := createMap()
err := prefixNameOps.Transform(m)
m := makeConfigMaps("cm1", "cm2", "cm1", "cm2")
npt, err := NewDefaultingNamePrefixTransformer("someprefix-")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = npt.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(m, namePrefixedM) {
t.Fatalf("%s doesn't match expected %s", m, namePrefixedM)
expected := makeConfigMaps("cm1", "cm2", "someprefix-cm1", "someprefix-cm2")
if !reflect.DeepEqual(m, expected) {
err = compareMap(m, expected)
t.Fatalf("actual doesn't match expected: %v", err)
}
}
1 change: 1 addition & 0 deletions pkg/kinflate/util/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// Transformer can transform objects.
type Transformer interface {
// Transform modifies objects in a map, e.g. add prefixes or additional labels.
Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error
Expand Down
31 changes: 13 additions & 18 deletions pkg/kinflate/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,35 @@ metadata:
name: cm2
`)

func createMap() map[GroupVersionKindName]*unstructured.Unstructured {
cm1 := unstructured.Unstructured{
func makeConfigMap(name string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
"name": name,
},
},
}
}

cm2 := unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm2",
},
},
}
func makeConfigMaps(name1InGVKN, name2InGVKN, name1InObj, name2InObj string) map[GroupVersionKindName]*unstructured.Unstructured {
cm1 := makeConfigMap(name1InObj)
cm2 := makeConfigMap(name2InObj)
return map[GroupVersionKindName]*unstructured.Unstructured{
{
GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
Name: "cm1",
}: &cm1,
Name: name1InGVKN,
}: cm1,
{
GVK: schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"},
Name: "cm2",
}: &cm2,
Name: name2InGVKN,
}: cm2,
}
}

func TestDecode(t *testing.T) {
expected := createMap()
expected := makeConfigMaps("cm1", "cm2", "cm1", "cm2")
m, err := Decode(encoded, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -80,7 +75,7 @@ func TestDecode(t *testing.T) {
}

func TestEncode(t *testing.T) {
out, err := Encode(createMap())
out, err := Encode(makeConfigMaps("cm1", "cm2", "cm1", "cm2"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down

0 comments on commit f9dd21c

Please sign in to comment.