mirror of
https://github.com/kubernetes/sample-controller.git
synced 2026-04-12 00:00:26 +08:00
Merge pull request #55650 from smarterclayton/make_unstructured_conversion
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Move unstructured conversion into pkg/runtime Scheme conversion should support unstructured conversion natively to allow going from unstructured to typed and back. It is not a higher level responsibility to do that conversion because the scheme is the only one who knows what types it supports. @liggitt @kubernetes/sig-cli-api-reviews I am going to make Scheme support unstructured in ConvertToVersion and Convert, which means resource.Builder and the CLI can get simpler for all existing use cases where versioned and unstructured need to coexist. Kubernetes-commit: a67abac7654cc3e05618d41191d71730cf9565ac
This commit is contained in:
-1
@@ -32,7 +32,6 @@ go_library(
|
||||
importpath = "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured",
|
||||
deps = [
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/conversion/unstructured:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
|
||||
+4
-7
@@ -24,7 +24,6 @@ import (
|
||||
"strings"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/conversion/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -39,7 +38,7 @@ func NestedFieldCopy(obj map[string]interface{}, fields ...string) (interface{},
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return unstructured.DeepCopyJSONValue(val), true
|
||||
return runtime.DeepCopyJSONValue(val), true
|
||||
}
|
||||
|
||||
func nestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool) {
|
||||
@@ -131,7 +130,7 @@ func NestedSlice(obj map[string]interface{}, fields ...string) ([]interface{}, b
|
||||
return nil, false
|
||||
}
|
||||
if _, ok := val.([]interface{}); ok {
|
||||
return unstructured.DeepCopyJSONValue(val).([]interface{}), true
|
||||
return runtime.DeepCopyJSONValue(val).([]interface{}), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
@@ -165,7 +164,7 @@ func NestedMap(obj map[string]interface{}, fields ...string) (map[string]interfa
|
||||
return nil, false
|
||||
}
|
||||
if m, ok := val.(map[string]interface{}); ok {
|
||||
return unstructured.DeepCopyJSON(m), true
|
||||
return runtime.DeepCopyJSON(m), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
@@ -173,7 +172,7 @@ func NestedMap(obj map[string]interface{}, fields ...string) (map[string]interfa
|
||||
// SetNestedField sets the value of a nested field to a deep copy of the value provided.
|
||||
// Returns false if value cannot be set because one of the nesting levels is not a map[string]interface{}.
|
||||
func SetNestedField(obj map[string]interface{}, value interface{}, fields ...string) bool {
|
||||
return setNestedFieldNoCopy(obj, unstructured.DeepCopyJSONValue(value), fields...)
|
||||
return setNestedFieldNoCopy(obj, runtime.DeepCopyJSONValue(value), fields...)
|
||||
}
|
||||
|
||||
func setNestedFieldNoCopy(obj map[string]interface{}, value interface{}, fields ...string) bool {
|
||||
@@ -288,8 +287,6 @@ func setOwnerReference(src metav1.OwnerReference) map[string]interface{} {
|
||||
return ret
|
||||
}
|
||||
|
||||
var converter = unstructured.NewConverter(false)
|
||||
|
||||
// UnstructuredJSONScheme is capable of converting JSON data into the Unstructured
|
||||
// type, which can be used for generic access to objects without a predefined scheme.
|
||||
// TODO: move into serializer/json.
|
||||
|
||||
+7
-6
@@ -21,7 +21,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/conversion/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -50,8 +49,6 @@ var _ runtime.Unstructured = &Unstructured{}
|
||||
|
||||
func (obj *Unstructured) GetObjectKind() schema.ObjectKind { return obj }
|
||||
|
||||
func (obj *Unstructured) IsUnstructuredObject() {}
|
||||
|
||||
func (obj *Unstructured) IsList() bool {
|
||||
if obj.Object != nil {
|
||||
_, ok := obj.Object["items"]
|
||||
@@ -91,6 +88,10 @@ func (obj *Unstructured) UnstructuredContent() map[string]interface{} {
|
||||
return obj.Object
|
||||
}
|
||||
|
||||
func (obj *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
|
||||
obj.Object = content
|
||||
}
|
||||
|
||||
// MarshalJSON ensures that the unstructured object produces proper
|
||||
// JSON when passed to Go's standard JSON library.
|
||||
func (u *Unstructured) MarshalJSON() ([]byte, error) {
|
||||
@@ -112,7 +113,7 @@ func (in *Unstructured) DeepCopy() *Unstructured {
|
||||
}
|
||||
out := new(Unstructured)
|
||||
*out = *in
|
||||
out.Object = unstructured.DeepCopyJSON(in.Object)
|
||||
out.Object = runtime.DeepCopyJSON(in.Object)
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -348,7 +349,7 @@ func (u *Unstructured) GetInitializers() *metav1.Initializers {
|
||||
return nil
|
||||
}
|
||||
out := &metav1.Initializers{}
|
||||
if err := converter.FromUnstructured(obj, out); err != nil {
|
||||
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, out); err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
|
||||
}
|
||||
return out
|
||||
@@ -359,7 +360,7 @@ func (u *Unstructured) SetInitializers(initializers *metav1.Initializers) {
|
||||
RemoveNestedField(u.Object, "metadata", "initializers")
|
||||
return
|
||||
}
|
||||
out, err := converter.ToUnstructured(initializers)
|
||||
out, err := runtime.DefaultUnstructuredConverter.ToUnstructured(initializers)
|
||||
if err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("unable to retrieve initializers for object: %v", err))
|
||||
}
|
||||
|
||||
+28
-4
@@ -20,7 +20,6 @@ import (
|
||||
"bytes"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/conversion/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
@@ -42,8 +41,6 @@ type UnstructuredList struct {
|
||||
|
||||
func (u *UnstructuredList) GetObjectKind() schema.ObjectKind { return u }
|
||||
|
||||
func (u *UnstructuredList) IsUnstructuredObject() {}
|
||||
|
||||
func (u *UnstructuredList) IsList() bool { return true }
|
||||
|
||||
func (u *UnstructuredList) EachListItem(fn func(runtime.Object) error) error {
|
||||
@@ -74,13 +71,40 @@ func (u *UnstructuredList) UnstructuredContent() map[string]interface{} {
|
||||
return out
|
||||
}
|
||||
|
||||
// SetUnstructuredContent obeys the conventions of List and keeps Items and the items
|
||||
// array in sync. If items is not an array of objects in the incoming map, then any
|
||||
// mismatched item will be removed.
|
||||
func (obj *UnstructuredList) SetUnstructuredContent(content map[string]interface{}) {
|
||||
obj.Object = content
|
||||
if content == nil {
|
||||
obj.Items = nil
|
||||
return
|
||||
}
|
||||
items, ok := obj.Object["items"].([]interface{})
|
||||
if !ok || items == nil {
|
||||
items = []interface{}{}
|
||||
}
|
||||
unstructuredItems := make([]Unstructured, 0, len(items))
|
||||
newItems := make([]interface{}, 0, len(items))
|
||||
for _, item := range items {
|
||||
o, ok := item.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
unstructuredItems = append(unstructuredItems, Unstructured{Object: o})
|
||||
newItems = append(newItems, o)
|
||||
}
|
||||
obj.Items = unstructuredItems
|
||||
obj.Object["items"] = newItems
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) DeepCopy() *UnstructuredList {
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(UnstructuredList)
|
||||
*out = *u
|
||||
out.Object = unstructured.DeepCopyJSON(u.Object)
|
||||
out.Object = runtime.DeepCopyJSON(u.Object)
|
||||
out.Items = make([]Unstructured, len(u.Items))
|
||||
for i := range u.Items {
|
||||
u.Items[i].DeepCopyInto(&out.Items[i])
|
||||
|
||||
Reference in New Issue
Block a user