add MapTransformer and constructor for labels and annotations

This commit is contained in:
ymqytw 2018-01-23 16:58:01 -08:00
parent eec253d14c
commit 9ab47fd44f
3 changed files with 33 additions and 37 deletions

View File

@ -17,41 +17,45 @@ limitations under the License.
package util package util
import ( import (
"errors"
"fmt" "fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
) )
// MapTransformationOptions contains a map string->string and path configs // MapTransformer contains a map string->string and path configs
// The map will be applied to the fields specified in path configs. // The map will be applied to the fields specified in path configs.
type MapTransformationOptions struct { type MapTransformer struct {
m map[string]string m map[string]string
pathConfigs []PathConfig pathConfigs []PathConfig
} }
var _ Transformer = &MapTransformationOptions{} var _ Transformer = &MapTransformer{}
// CompleteForLabels fills up the MapTransformationOptions for labels transformation. // NewDefaultingLabelsMapTransformer construct a MapTransformer with defaultLabelsPathConfigs.
func (o *MapTransformationOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) { func NewDefaultingLabelsMapTransformer(m map[string]string) (Transformer, error) {
o.m = m return NewMapTransformer(defaultLabelsPathConfigs, m)
if pathConfigs == nil {
pathConfigs = DefaultLabelsPathConfigs
}
o.pathConfigs = pathConfigs
} }
// CompleteForAnnotations fills up the MapTransformationOptions for annotations transformation. // NewDefaultingAnnotationsMapTransformer construct a MapTransformer with defaultAnnotationsPathConfigs.
func (o *MapTransformationOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) { func NewDefaultingAnnotationsMapTransformer(m map[string]string) (Transformer, error) {
o.m = m return NewMapTransformer(defaultAnnotationsPathConfigs, m)
if pathConfigs == nil {
pathConfigs = DefaultAnnotationsPathConfigs
}
o.pathConfigs = pathConfigs
} }
// Transform apply each <key, value> pair in the MapTransformationOptions to the // NewMapTransformer construct a MapTransformer.
// fields specified in MapTransformationOptions. func NewMapTransformer(pc []PathConfig, m map[string]string) (Transformer, error) {
func (o *MapTransformationOptions) Transform(m map[GroupVersionKindName]*unstructured.Unstructured) error { if m == nil {
return nil, errors.New("map is not expected to be nil")
}
if pc == nil {
return nil, errors.New("pathConfigs is not expected to be nil")
}
return &MapTransformer{pathConfigs: pc, m: m}, nil
}
// Transform apply each <key, value> pair in the MapTransformer to the
// fields specified in MapTransformer.
func (o *MapTransformer) 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()
@ -68,7 +72,7 @@ func (o *MapTransformationOptions) Transform(m map[GroupVersionKindName]*unstruc
return nil return nil
} }
func (o *MapTransformationOptions) addMap(in interface{}) (interface{}, error) { func (o *MapTransformer) 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)

View File

@ -24,16 +24,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
) )
var labelsOps = MapTransformationOptions{
m: map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"},
pathConfigs: DefaultLabelsPathConfigs,
}
var annotationsOps = MapTransformationOptions{
m: map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"},
pathConfigs: DefaultAnnotationsPathConfigs,
}
func makeConfigmap() *unstructured.Unstructured { func makeConfigmap() *unstructured.Unstructured {
return &unstructured.Unstructured{ return &unstructured.Unstructured{
Object: map[string]interface{}{ Object: map[string]interface{}{
@ -218,7 +208,8 @@ func makeLabeledMap() map[GroupVersionKindName]*unstructured.Unstructured {
func TestLabelsRun(t *testing.T) { func TestLabelsRun(t *testing.T) {
m := makeTestMap() m := makeTestMap()
err := labelsOps.Transform(m) lt, err := NewDefaultingLabelsMapTransformer(map[string]string{"label-key1": "label-value1", "label-key2": "label-value2"})
err = lt.Transform(m)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
@ -326,7 +317,8 @@ func makeAnnotatedMap() map[GroupVersionKindName]*unstructured.Unstructured {
func TestAnnotationsRun(t *testing.T) { func TestAnnotationsRun(t *testing.T) {
m := makeTestMap() m := makeTestMap()
err := annotationsOps.Transform(m) at, err := NewDefaultingAnnotationsMapTransformer(map[string]string{"anno-key1": "anno-value1", "anno-key2": "anno-value2"})
err = at.Transform(m)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }

View File

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