mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 03:57:41 +00:00
Refactor unstructured converter
This commit is contained in:
parent
956acc2bd8
commit
dc1ee493a2
@ -97,8 +97,7 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newUnstr := make(map[string]interface{})
|
newUnstr, err := unstructured.DefaultConverter.ToUnstructured(item)
|
||||||
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
|
||||||
@ -138,8 +137,8 @@ func BenchmarkToFromUnstructured(b *testing.B) {
|
|||||||
size := len(items)
|
size := len(items)
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
unstr := map[string]interface{}{}
|
unstr, err := unstructured.DefaultConverter.ToUnstructured(&items[i%size])
|
||||||
if err := unstructured.DefaultConverter.ToUnstructured(&items[i%size], &unstr); err != nil {
|
if err != nil {
|
||||||
b.Fatalf("unexpected error: %v", err)
|
b.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
obj := v1.Pod{}
|
obj := v1.Pod{}
|
||||||
|
@ -482,8 +482,8 @@ func (u *Unstructured) SetInitializers(initializers *metav1.Initializers) {
|
|||||||
setNestedField(u.Object, nil, "metadata", "initializers")
|
setNestedField(u.Object, nil, "metadata", "initializers")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
out := make(map[string]interface{})
|
out, err := converter.ToUnstructured(initializers)
|
||||||
if err := converter.ToUnstructured(initializers, &out); err != nil {
|
if err != nil {
|
||||||
utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
|
utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
|
||||||
}
|
}
|
||||||
setNestedField(u.Object, out, "metadata", "initializers")
|
setNestedField(u.Object, out, "metadata", "initializers")
|
||||||
|
@ -39,7 +39,7 @@ import (
|
|||||||
// Converter is an interface for converting between interface{}
|
// Converter is an interface for converting between interface{}
|
||||||
// and map[string]interface representation.
|
// and map[string]interface representation.
|
||||||
type Converter interface {
|
type Converter interface {
|
||||||
ToUnstructured(obj interface{}, u *map[string]interface{}) error
|
ToUnstructured(obj interface{}) (map[string]interface{}, error)
|
||||||
FromUnstructured(u map[string]interface{}, obj interface{}) error
|
FromUnstructured(u map[string]interface{}, obj interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,12 +388,13 @@ func interfaceFromUnstructured(sv, dv reflect.Value) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *converterImpl) ToUnstructured(obj interface{}, u *map[string]interface{}) error {
|
func (c *converterImpl) ToUnstructured(obj interface{}) (map[string]interface{}, error) {
|
||||||
t := reflect.TypeOf(obj)
|
t := reflect.TypeOf(obj)
|
||||||
value := reflect.ValueOf(obj)
|
value := reflect.ValueOf(obj)
|
||||||
if t.Kind() != reflect.Ptr || value.IsNil() {
|
if t.Kind() != reflect.Ptr || value.IsNil() {
|
||||||
return fmt.Errorf("ToUnstructured requires a non-nil pointer to an object, got %v", t)
|
return nil, fmt.Errorf("ToUnstructured requires a non-nil pointer to an object, got %v", t)
|
||||||
}
|
}
|
||||||
|
u := &map[string]interface{}{}
|
||||||
err := toUnstructured(value.Elem(), reflect.ValueOf(u).Elem())
|
err := toUnstructured(value.Elem(), reflect.ValueOf(u).Elem())
|
||||||
if c.mismatchDetection {
|
if c.mismatchDetection {
|
||||||
newUnstr := &map[string]interface{}{}
|
newUnstr := &map[string]interface{}{}
|
||||||
@ -405,7 +406,10 @@ func (c *converterImpl) ToUnstructured(obj interface{}, u *map[string]interface{
|
|||||||
glog.Fatalf("ToUnstructured mismatch for %#v, diff: %v", u, diff.ObjectReflectDiff(u, newUnstr))
|
glog.Fatalf("ToUnstructured mismatch for %#v, diff: %v", u, diff.ObjectReflectDiff(u, newUnstr))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return *u, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toUnstructuredViaJSON(obj interface{}, u *map[string]interface{}) error {
|
func toUnstructuredViaJSON(obj interface{}, u *map[string]interface{}) error {
|
||||||
|
@ -121,8 +121,7 @@ func doRoundTrip(t *testing.T, item runtime.Object) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newUnstr := make(map[string]interface{})
|
newUnstr, err := DefaultConverter.ToUnstructured(item)
|
||||||
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
|
||||||
|
@ -87,8 +87,8 @@ func strategicPatchObject(
|
|||||||
objToUpdate runtime.Object,
|
objToUpdate runtime.Object,
|
||||||
versionedObj runtime.Object,
|
versionedObj runtime.Object,
|
||||||
) error {
|
) error {
|
||||||
originalObjMap := make(map[string]interface{})
|
originalObjMap, err := unstructured.DefaultConverter.ToUnstructured(originalObject)
|
||||||
if err := unstructured.DefaultConverter.ToUnstructured(originalObject, &originalObjMap); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -694,8 +694,8 @@ func patchResource(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Capture the original object map and patch for possible retries.
|
// Capture the original object map and patch for possible retries.
|
||||||
originalMap := make(map[string]interface{})
|
originalMap, err := unstructured.DefaultConverter.ToUnstructured(currentVersionedObject)
|
||||||
if err := unstructured.DefaultConverter.ToUnstructured(currentVersionedObject, &originalMap); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := strategicPatchObject(codec, defaulter, currentVersionedObject, patchJS, versionedObjToUpdate, versionedObj); err != nil {
|
if err := strategicPatchObject(codec, defaulter, currentVersionedObject, patchJS, versionedObjToUpdate, versionedObj); err != nil {
|
||||||
@ -734,15 +734,14 @@ func patchResource(
|
|||||||
// 3. ensure no conflicts between the two patches
|
// 3. ensure no conflicts between the two patches
|
||||||
// 4. apply the #1 patch to the currentJS object
|
// 4. apply the #1 patch to the currentJS object
|
||||||
|
|
||||||
currentObjMap := make(map[string]interface{})
|
|
||||||
|
|
||||||
// Since the patch is applied on versioned objects, we need to convert the
|
// Since the patch is applied on versioned objects, we need to convert the
|
||||||
// current object to versioned representation first.
|
// current object to versioned representation first.
|
||||||
currentVersionedObject, err := unsafeConvertor.ConvertToVersion(currentObject, kind.GroupVersion())
|
currentVersionedObject, err := unsafeConvertor.ConvertToVersion(currentObject, kind.GroupVersion())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := unstructured.DefaultConverter.ToUnstructured(currentVersionedObject, ¤tObjMap); err != nil {
|
currentObjMap, err := unstructured.DefaultConverter.ToUnstructured(currentVersionedObject)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user