add MapTransformer and constructor for labels and annotations
This commit is contained in:
parent
eec253d14c
commit
9ab47fd44f
|
|
@ -17,41 +17,45 @@ limitations under the License.
|
|||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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.
|
||||
type MapTransformationOptions struct {
|
||||
type MapTransformer struct {
|
||||
m map[string]string
|
||||
pathConfigs []PathConfig
|
||||
}
|
||||
|
||||
var _ Transformer = &MapTransformationOptions{}
|
||||
var _ Transformer = &MapTransformer{}
|
||||
|
||||
// CompleteForLabels fills up the MapTransformationOptions for labels transformation.
|
||||
func (o *MapTransformationOptions) CompleteForLabels(m map[string]string, pathConfigs []PathConfig) {
|
||||
o.m = m
|
||||
if pathConfigs == nil {
|
||||
pathConfigs = DefaultLabelsPathConfigs
|
||||
}
|
||||
o.pathConfigs = pathConfigs
|
||||
// NewDefaultingLabelsMapTransformer construct a MapTransformer with defaultLabelsPathConfigs.
|
||||
func NewDefaultingLabelsMapTransformer(m map[string]string) (Transformer, error) {
|
||||
return NewMapTransformer(defaultLabelsPathConfigs, m)
|
||||
}
|
||||
|
||||
// CompleteForAnnotations fills up the MapTransformationOptions for annotations transformation.
|
||||
func (o *MapTransformationOptions) CompleteForAnnotations(m map[string]string, pathConfigs []PathConfig) {
|
||||
o.m = m
|
||||
if pathConfigs == nil {
|
||||
pathConfigs = DefaultAnnotationsPathConfigs
|
||||
}
|
||||
o.pathConfigs = pathConfigs
|
||||
// NewDefaultingAnnotationsMapTransformer construct a MapTransformer with defaultAnnotationsPathConfigs.
|
||||
func NewDefaultingAnnotationsMapTransformer(m map[string]string) (Transformer, error) {
|
||||
return NewMapTransformer(defaultAnnotationsPathConfigs, m)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// NewMapTransformer construct a MapTransformer.
|
||||
func NewMapTransformer(pc []PathConfig, m map[string]string) (Transformer, 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 {
|
||||
obj := m[gvkn]
|
||||
objMap := obj.UnstructuredContent()
|
||||
|
|
@ -68,7 +72,7 @@ func (o *MapTransformationOptions) Transform(m map[GroupVersionKindName]*unstruc
|
|||
return nil
|
||||
}
|
||||
|
||||
func (o *MapTransformationOptions) addMap(in interface{}) (interface{}, error) {
|
||||
func (o *MapTransformer) addMap(in interface{}) (interface{}, error) {
|
||||
m, ok := in.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%#v is expectd to be %T", in, m)
|
||||
|
|
|
|||
|
|
@ -24,16 +24,6 @@ import (
|
|||
"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 {
|
||||
return &unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
|
|
@ -218,7 +208,8 @@ func makeLabeledMap() map[GroupVersionKindName]*unstructured.Unstructured {
|
|||
|
||||
func TestLabelsRun(t *testing.T) {
|
||||
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 {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
@ -326,7 +317,8 @@ func makeAnnotatedMap() map[GroupVersionKindName]*unstructured.Unstructured {
|
|||
|
||||
func TestAnnotationsRun(t *testing.T) {
|
||||
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 {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,9 +20,9 @@ import (
|
|||
"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.
|
||||
var DefaultLabelsPathConfigs = []PathConfig{
|
||||
var defaultLabelsPathConfigs = []PathConfig{
|
||||
{
|
||||
Path: []string{"metadata", "labels"},
|
||||
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.
|
||||
var DefaultAnnotationsPathConfigs = []PathConfig{
|
||||
var defaultAnnotationsPathConfigs = []PathConfig{
|
||||
{
|
||||
Path: []string{"metadata", "annotations"},
|
||||
CreateIfNotPresent: true,
|
||||
|
|
|
|||
Loading…
Reference in New Issue