mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Unified unstructured converter interface and setup
This commit is contained in:
parent
975db6ff9a
commit
924e9a5b3a
@ -98,14 +98,14 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
newUnstr := make(map[string]interface{})
|
newUnstr := make(map[string]interface{})
|
||||||
err = unstructured.Converter.ToUnstructured(item, &newUnstr)
|
err = unstructured.DefaultConverter.ToUnstructured(item, &newUnstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ToUnstructured failed: %v", err)
|
t.Errorf("ToUnstructured failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
||||||
err = unstructured.Converter.FromUnstructured(newUnstr, newObj)
|
err = unstructured.DefaultConverter.FromUnstructured(newUnstr, newObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("FromUnstructured failed: %v", err)
|
t.Errorf("FromUnstructured failed: %v", err)
|
||||||
return
|
return
|
||||||
@ -139,11 +139,11 @@ func BenchmarkToFromUnstructured(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
unstr := map[string]interface{}{}
|
unstr := map[string]interface{}{}
|
||||||
if err := unstructured.Converter.ToUnstructured(&items[i%size], &unstr); err != nil {
|
if err := unstructured.DefaultConverter.ToUnstructured(&items[i%size], &unstr); err != nil {
|
||||||
b.Fatalf("unexpected error: %v", err)
|
b.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
obj := v1.Pod{}
|
obj := v1.Pod{}
|
||||||
if err := unstructured.Converter.FromUnstructured(unstr, &obj); err != nil {
|
if err := unstructured.DefaultConverter.FromUnstructured(unstr, &obj); err != nil {
|
||||||
b.Fatalf("unexpected error: %v", err)
|
b.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,13 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Converter is an interface for converting between runtime.Object
|
||||||
|
// and map[string]interface representation.
|
||||||
|
type Converter interface {
|
||||||
|
ToUnstructured(obj runtime.Object, u *map[string]interface{}) error
|
||||||
|
FromUnstructured(u map[string]interface{}, obj runtime.Object) error
|
||||||
|
}
|
||||||
|
|
||||||
type structField struct {
|
type structField struct {
|
||||||
structType reflect.Type
|
structType reflect.Type
|
||||||
field int
|
field int
|
||||||
@ -69,27 +76,33 @@ var (
|
|||||||
float64Type = reflect.TypeOf(float64(0))
|
float64Type = reflect.TypeOf(float64(0))
|
||||||
boolType = reflect.TypeOf(bool(false))
|
boolType = reflect.TypeOf(bool(false))
|
||||||
fieldCache = newFieldsCache()
|
fieldCache = newFieldsCache()
|
||||||
Converter = newConverter()
|
DefaultConverter = NewConverter(parseBool(os.Getenv("KUBE_PATCH_CONVERSION_DETECTOR")))
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func parseBool(key string) bool {
|
||||||
|
value, err := strconv.ParseBool(key)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't parse %s as bool", key)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
// ConverterImpl knows how to convert betweek runtime.Object and
|
// ConverterImpl knows how to convert betweek runtime.Object and
|
||||||
// Unstructured in both ways.
|
// Unstructured in both ways.
|
||||||
type ConverterImpl struct {
|
type converterImpl struct {
|
||||||
// If true, we will be additionally running conversion via json
|
// If true, we will be additionally running conversion via json
|
||||||
// to ensure that the result is true.
|
// to ensure that the result is true.
|
||||||
// This is supposed to be set only in tests.
|
// This is supposed to be set only in tests.
|
||||||
mismatchDetection bool
|
mismatchDetection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConverter() *ConverterImpl {
|
func NewConverter(mismatchDetection bool) Converter {
|
||||||
mismatchDetection, _ := strconv.ParseBool(os.Getenv("KUBE_PATCH_CONVERSION_DETECTOR"))
|
return &converterImpl{
|
||||||
return &ConverterImpl{
|
|
||||||
mismatchDetection: mismatchDetection,
|
mismatchDetection: mismatchDetection,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConverterImpl) FromUnstructured(u map[string]interface{}, obj runtime.Object) error {
|
func (c *converterImpl) FromUnstructured(u map[string]interface{}, obj runtime.Object) error {
|
||||||
err := fromUnstructured(reflect.ValueOf(u), reflect.ValueOf(obj).Elem())
|
err := fromUnstructured(reflect.ValueOf(u), reflect.ValueOf(obj).Elem())
|
||||||
if c.mismatchDetection {
|
if c.mismatchDetection {
|
||||||
newObj := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
|
newObj := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
|
||||||
@ -362,7 +375,7 @@ func interfaceFromUnstructured(sv, dv reflect.Value) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConverterImpl) ToUnstructured(obj runtime.Object, u *map[string]interface{}) error {
|
func (c *converterImpl) ToUnstructured(obj runtime.Object, u *map[string]interface{}) error {
|
||||||
err := toUnstructured(reflect.ValueOf(obj).Elem(), reflect.ValueOf(u).Elem())
|
err := toUnstructured(reflect.ValueOf(obj).Elem(), reflect.ValueOf(u).Elem())
|
||||||
if c.mismatchDetection {
|
if c.mismatchDetection {
|
||||||
newUnstr := &map[string]interface{}{}
|
newUnstr := &map[string]interface{}{}
|
||||||
|
@ -122,14 +122,14 @@ func doRoundTrip(t *testing.T, item runtime.Object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
newUnstr := make(map[string]interface{})
|
newUnstr := make(map[string]interface{})
|
||||||
err = Converter.ToUnstructured(item, &newUnstr)
|
err = DefaultConverter.ToUnstructured(item, &newUnstr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ToUnstructured failed: %v", err)
|
t.Errorf("ToUnstructured failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
||||||
err = Converter.FromUnstructured(newUnstr, newObj)
|
err = DefaultConverter.FromUnstructured(newUnstr, newObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("FromUnstructured failed: %v", err)
|
t.Errorf("FromUnstructured failed: %v", err)
|
||||||
return
|
return
|
||||||
@ -239,7 +239,7 @@ func doUnrecognized(t *testing.T, jsonData string, item runtime.Object, expected
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
|
||||||
err = Converter.FromUnstructured(unstr, newObj)
|
err = DefaultConverter.FromUnstructured(unstr, newObj)
|
||||||
if (err != nil) != (expectedErr != nil) {
|
if (err != nil) != (expectedErr != nil) {
|
||||||
t.Errorf("Unexpected error in FromUnstructured: %v, expected: %v", err, expectedErr)
|
t.Errorf("Unexpected error in FromUnstructured: %v, expected: %v", err, expectedErr)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user