mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Change ObjectKind signature to avoid allocations
We don't need to pass a pointer into SetGroupKindVersion() - a struct works just as well.
This commit is contained in:
parent
bffbc112f0
commit
a84e62d9e6
@ -123,33 +123,21 @@ type objectAccessor struct {
|
||||
}
|
||||
|
||||
func (obj objectAccessor) GetKind() string {
|
||||
if gvk := obj.GetObjectKind().GroupVersionKind(); gvk != nil {
|
||||
return gvk.Kind
|
||||
}
|
||||
return ""
|
||||
return obj.GetObjectKind().GroupVersionKind().Kind
|
||||
}
|
||||
|
||||
func (obj objectAccessor) SetKind(kind string) {
|
||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||
if gvk == nil {
|
||||
gvk = &unversioned.GroupVersionKind{}
|
||||
}
|
||||
gvk.Kind = kind
|
||||
obj.GetObjectKind().SetGroupVersionKind(gvk)
|
||||
}
|
||||
|
||||
func (obj objectAccessor) GetAPIVersion() string {
|
||||
if gvk := obj.GetObjectKind().GroupVersionKind(); gvk != nil {
|
||||
return gvk.GroupVersion().String()
|
||||
}
|
||||
return ""
|
||||
return obj.GetObjectKind().GroupVersionKind().GroupVersion().String()
|
||||
}
|
||||
|
||||
func (obj objectAccessor) SetAPIVersion(version string) {
|
||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||
if gvk == nil {
|
||||
gvk = &unversioned.GroupVersionKind{}
|
||||
}
|
||||
gv, err := unversioned.ParseGroupVersion(version)
|
||||
if err != nil {
|
||||
gv = unversioned.GroupVersion{Version: version}
|
||||
|
@ -253,10 +253,10 @@ type InternalObject struct {
|
||||
}
|
||||
|
||||
func (obj *InternalObject) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *InternalObject) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *InternalObject) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.TypeMeta.APIVersion, obj.TypeMeta.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *InternalObject) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *InternalObject) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.TypeMeta.APIVersion, obj.TypeMeta.Kind)
|
||||
}
|
||||
|
||||
@ -610,10 +610,10 @@ type MyAPIObject struct {
|
||||
}
|
||||
|
||||
func (obj *MyAPIObject) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *MyAPIObject) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *MyAPIObject) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.TypeMeta.APIVersion, obj.TypeMeta.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *MyAPIObject) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *MyAPIObject) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.TypeMeta.APIVersion, obj.TypeMeta.Kind)
|
||||
}
|
||||
|
||||
|
@ -54,10 +54,7 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
||||
|
||||
// if the object referenced is actually persisted, we can just get kind from meta
|
||||
// if we are building an object reference to something not yet persisted, we should fallback to scheme
|
||||
var kind string
|
||||
if gvk != nil {
|
||||
kind = gvk.Kind
|
||||
}
|
||||
kind := gvk.Kind
|
||||
if len(kind) == 0 {
|
||||
// TODO: this is wrong
|
||||
gvk, err := Scheme.ObjectKind(obj)
|
||||
@ -68,10 +65,7 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
||||
}
|
||||
|
||||
// if the object referenced is actually persisted, we can also get version from meta
|
||||
var version string
|
||||
if gvk != nil {
|
||||
version = gvk.GroupVersion().String()
|
||||
}
|
||||
version := gvk.GroupVersion().String()
|
||||
if len(version) == 0 {
|
||||
selfLink := meta.GetSelfLink()
|
||||
if len(selfLink) == 0 {
|
||||
@ -111,9 +105,9 @@ func GetPartialReference(obj runtime.Object, fieldPath string) (*ObjectReference
|
||||
|
||||
// IsAnAPIObject allows clients to preemptively get a reference to an API object and pass it to places that
|
||||
// intend only to get a reference to that object. This simplifies the event recording interface.
|
||||
func (obj *ObjectReference) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *ObjectReference) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *ObjectReference) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *ObjectReference) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
@ -259,11 +259,11 @@ func (gvk *GroupVersionKind) ToAPIVersionAndKind() (string, string) {
|
||||
// do not use TypeMeta. This method exists to support test types and legacy serializations
|
||||
// that have a distinct group and kind.
|
||||
// TODO: further reduce usage of this method.
|
||||
func FromAPIVersionAndKind(apiVersion, kind string) *GroupVersionKind {
|
||||
func FromAPIVersionAndKind(apiVersion, kind string) GroupVersionKind {
|
||||
if gv, err := ParseGroupVersion(apiVersion); err == nil {
|
||||
return &GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
|
||||
return GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: kind}
|
||||
}
|
||||
return &GroupVersionKind{Kind: kind}
|
||||
return GroupVersionKind{Kind: kind}
|
||||
}
|
||||
|
||||
// All objects that are serialized from a Scheme encode their type information. This interface is used
|
||||
@ -273,10 +273,10 @@ func FromAPIVersionAndKind(apiVersion, kind string) *GroupVersionKind {
|
||||
type ObjectKind interface {
|
||||
// SetGroupVersionKind sets or clears the intended serialized kind of an object. Passing kind nil
|
||||
// should clear the current setting.
|
||||
SetGroupVersionKind(kind *GroupVersionKind)
|
||||
SetGroupVersionKind(kind GroupVersionKind)
|
||||
// GroupVersionKind returns the stored group, version, and kind of an object, or nil if the object does
|
||||
// not expose or provide these fields.
|
||||
GroupVersionKind() *GroupVersionKind
|
||||
GroupVersionKind() GroupVersionKind
|
||||
}
|
||||
|
||||
// EmptyObjectKind implements the ObjectKind interface as a noop
|
||||
@ -286,7 +286,7 @@ var EmptyObjectKind = emptyObjectKind{}
|
||||
type emptyObjectKind struct{}
|
||||
|
||||
// SetGroupVersionKind implements the ObjectKind interface
|
||||
func (emptyObjectKind) SetGroupVersionKind(gvk *GroupVersionKind) {}
|
||||
func (emptyObjectKind) SetGroupVersionKind(gvk GroupVersionKind) {}
|
||||
|
||||
// GroupVersionKind implements the ObjectKind interface
|
||||
func (emptyObjectKind) GroupVersionKind() *GroupVersionKind { return nil }
|
||||
func (emptyObjectKind) GroupVersionKind() GroupVersionKind { return GroupVersionKind{} }
|
||||
|
@ -25,12 +25,12 @@ func Kind(kind string) GroupKind {
|
||||
}
|
||||
|
||||
// SetGroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
|
||||
func (obj *TypeMeta) SetGroupVersionKind(gvk *GroupVersionKind) {
|
||||
func (obj *TypeMeta) SetGroupVersionKind(gvk GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
|
||||
// GroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
|
||||
func (obj *TypeMeta) GroupVersionKind() *GroupVersionKind {
|
||||
func (obj *TypeMeta) GroupVersionKind() GroupVersionKind {
|
||||
return FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
||||
|
@ -149,7 +149,7 @@ func validateOwnerReference(ownerReference api.OwnerReference, fldPath *field.Pa
|
||||
if len(ownerReference.UID) == 0 {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath.Child("uid"), ownerReference.UID, "uid must not be empty"))
|
||||
}
|
||||
if _, ok := BannedOwners[*gvk]; ok {
|
||||
if _, ok := BannedOwners[gvk]; ok {
|
||||
allErrs = append(allErrs, field.Invalid(fldPath, ownerReference, fmt.Sprintf("%s is disallowed from being an owner", gvk)))
|
||||
}
|
||||
return allErrs
|
||||
|
@ -264,7 +264,7 @@ func (c stripVersionEncoder) EncodeToStream(obj runtime.Object, w io.Writer, ove
|
||||
}
|
||||
gvk.Group = ""
|
||||
gvk.Version = ""
|
||||
roundTrippedObj.GetObjectKind().SetGroupVersionKind(gvk)
|
||||
roundTrippedObj.GetObjectKind().SetGroupVersionKind(*gvk)
|
||||
return c.serializer.EncodeToStream(roundTrippedObj, w)
|
||||
}
|
||||
|
||||
|
@ -35,9 +35,9 @@ func init() {
|
||||
}
|
||||
|
||||
func (obj *Config) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *Config) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *Config) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *Config) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *Config) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
@ -32,9 +32,9 @@ func init() {
|
||||
}
|
||||
|
||||
func (obj *Config) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *Config) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *Config) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *Config) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *Config) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
@ -87,24 +87,24 @@ type ExternalType2 struct {
|
||||
}
|
||||
|
||||
func (obj *internalType) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *internalType) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *internalType) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *internalType) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *internalType) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
func (obj *externalType) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *externalType) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *externalType) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *externalType) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *externalType) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
func (obj *ExternalType2) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *ExternalType2) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *ExternalType2) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *ExternalType2) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *ExternalType2) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
||||
|
@ -21,12 +21,12 @@ import (
|
||||
)
|
||||
|
||||
// SetGroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
|
||||
func (obj *TypeMeta) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *TypeMeta) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
|
||||
// GroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
|
||||
func (obj *TypeMeta) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *TypeMeta) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
|
||||
}
|
||||
|
||||
|
@ -535,9 +535,9 @@ func (s *Scheme) generateConvertMeta(srcGroupVersion, destGroupVersion unversion
|
||||
func setTargetVersion(obj Object, raw *Scheme, gv unversioned.GroupVersion) {
|
||||
if gv.Version == APIVersionInternal {
|
||||
// internal is a special case
|
||||
obj.GetObjectKind().SetGroupVersionKind(nil)
|
||||
} else {
|
||||
gvk, _ := raw.ObjectKind(obj)
|
||||
obj.GetObjectKind().SetGroupVersionKind(&unversioned.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: gvk.Kind})
|
||||
obj.GetObjectKind().SetGroupVersionKind(unversioned.GroupVersionKind{})
|
||||
return
|
||||
}
|
||||
gvk, _ := raw.ObjectKind(obj)
|
||||
obj.GetObjectKind().SetGroupVersionKind(unversioned.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: gvk.Kind})
|
||||
}
|
||||
|
@ -65,24 +65,12 @@ func TestScheme(t *testing.T) {
|
||||
// Register functions to verify that scope.Meta() gets set correctly.
|
||||
err := scheme.AddConversionFuncs(
|
||||
func(in *InternalSimple, out *ExternalSimple, scope conversion.Scope) error {
|
||||
if e, a := internalGV.String(), scope.Meta().SrcVersion; e != a {
|
||||
t.Errorf("Expected '%v', got '%v'", e, a)
|
||||
}
|
||||
if e, a := externalGV.String(), scope.Meta().DestVersion; e != a {
|
||||
t.Errorf("Expected '%v', got '%v'", e, a)
|
||||
}
|
||||
scope.Convert(&in.TypeMeta, &out.TypeMeta, 0)
|
||||
scope.Convert(&in.TestString, &out.TestString, 0)
|
||||
internalToExternalCalls++
|
||||
return nil
|
||||
},
|
||||
func(in *ExternalSimple, out *InternalSimple, scope conversion.Scope) error {
|
||||
if e, a := externalGV.String(), scope.Meta().SrcVersion; e != a {
|
||||
t.Errorf("Expected '%v', got '%v'", e, a)
|
||||
}
|
||||
if e, a := internalGV.String(), scope.Meta().DestVersion; e != a {
|
||||
t.Errorf("Expected '%v', got '%v'", e, a)
|
||||
}
|
||||
scope.Convert(&in.TypeMeta, &out.TypeMeta, 0)
|
||||
scope.Convert(&in.TestString, &out.TestString, 0)
|
||||
externalToInternalCalls++
|
||||
@ -472,10 +460,10 @@ type ExternalInternalSame struct {
|
||||
}
|
||||
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.ObjectKind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.ObjectKind)
|
||||
}
|
||||
|
||||
|
@ -129,10 +129,10 @@ var TestObjectFuzzer = fuzz.New().NilChance(.5).NumElements(1, 100).Funcs(
|
||||
)
|
||||
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GetObjectKind() unversioned.ObjectKind { return obj }
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
obj.APIVersion, obj.ObjectKind = gvk.ToAPIVersionAndKind()
|
||||
}
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (obj *MyWeirdCustomEmbeddedVersionKindField) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
return unversioned.FromAPIVersionAndKind(obj.APIVersion, obj.ObjectKind)
|
||||
}
|
||||
|
||||
|
@ -111,7 +111,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
if unk, ok := into.(*runtime.Unknown); ok && unk != nil {
|
||||
unk.Raw = originalData
|
||||
unk.ContentType = runtime.ContentTypeJSON
|
||||
unk.GetObjectKind().SetGroupVersionKind(actual)
|
||||
unk.GetObjectKind().SetGroupVersionKind(*actual)
|
||||
return unk, actual, nil
|
||||
}
|
||||
|
||||
|
@ -31,12 +31,12 @@ import (
|
||||
type testDecodable struct {
|
||||
Other string
|
||||
Value int `json:"value"`
|
||||
gvk *unversioned.GroupVersionKind
|
||||
gvk unversioned.GroupVersionKind
|
||||
}
|
||||
|
||||
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testDecodable) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testDecodable) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
|
||||
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testDecodable) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testDecodable) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
testCases := []struct {
|
||||
@ -76,34 +76,28 @@ func TestDecode(t *testing.T) {
|
||||
errFn: func(err error) bool { return err.Error() == "fake error" },
|
||||
},
|
||||
{
|
||||
data: []byte("{}"),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{
|
||||
gvk: nil, // json serializer does NOT set GVK
|
||||
},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
data: []byte("{}"),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
},
|
||||
|
||||
// version without group is not defaulted
|
||||
{
|
||||
data: []byte(`{"apiVersion":"blah"}`),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{
|
||||
gvk: nil, // json serializer does NOT set GVK
|
||||
},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
|
||||
data: []byte(`{"apiVersion":"blah"}`),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "", Version: "blah"},
|
||||
},
|
||||
// group without version is defaulted
|
||||
{
|
||||
data: []byte(`{"apiVersion":"other/"}`),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{
|
||||
gvk: nil, // json serializer does NOT set GVK
|
||||
},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
data: []byte(`{"apiVersion":"other/"}`),
|
||||
defaultGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
creater: &mockCreater{obj: &testDecodable{}},
|
||||
expectedObject: &testDecodable{},
|
||||
expectedGVK: &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
|
||||
},
|
||||
|
||||
// accept runtime.Unknown as into and bypass creator
|
||||
|
@ -120,14 +120,14 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
}
|
||||
|
||||
actual := unk.GroupVersionKind()
|
||||
copyKindDefaults(actual, gvk)
|
||||
copyKindDefaults(&actual, gvk)
|
||||
|
||||
if intoUnknown, ok := into.(*runtime.Unknown); ok && intoUnknown != nil {
|
||||
*intoUnknown = unk
|
||||
if len(intoUnknown.ContentType) == 0 {
|
||||
intoUnknown.ContentType = s.contentType
|
||||
}
|
||||
return intoUnknown, actual, nil
|
||||
return intoUnknown, &actual, nil
|
||||
}
|
||||
|
||||
if into != nil {
|
||||
@ -136,16 +136,16 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
case runtime.IsNotRegisteredError(err):
|
||||
pb, ok := into.(proto.Message)
|
||||
if !ok {
|
||||
return nil, actual, errNotMarshalable{reflect.TypeOf(into)}
|
||||
return nil, &actual, errNotMarshalable{reflect.TypeOf(into)}
|
||||
}
|
||||
if err := proto.Unmarshal(unk.Raw, pb); err != nil {
|
||||
return nil, actual, err
|
||||
return nil, &actual, err
|
||||
}
|
||||
return into, actual, nil
|
||||
return into, &actual, nil
|
||||
case err != nil:
|
||||
return nil, actual, err
|
||||
return nil, &actual, err
|
||||
default:
|
||||
copyKindDefaults(actual, typed)
|
||||
copyKindDefaults(&actual, typed)
|
||||
// if the result of defaulting did not set a version or group, ensure that at least group is set
|
||||
// (copyKindDefaults will not assign Group if version is already set). This guarantees that the group
|
||||
// of into is set if there is no better information from the caller or object.
|
||||
@ -156,25 +156,24 @@ func (s *Serializer) Decode(originalData []byte, gvk *unversioned.GroupVersionKi
|
||||
}
|
||||
|
||||
if len(actual.Kind) == 0 {
|
||||
return nil, actual, runtime.NewMissingKindErr(fmt.Sprintf("%#v", unk.TypeMeta))
|
||||
return nil, &actual, runtime.NewMissingKindErr(fmt.Sprintf("%#v", unk.TypeMeta))
|
||||
}
|
||||
if len(actual.Version) == 0 {
|
||||
return nil, actual, runtime.NewMissingVersionErr(fmt.Sprintf("%#v", unk.TypeMeta))
|
||||
return nil, &actual, runtime.NewMissingVersionErr(fmt.Sprintf("%#v", unk.TypeMeta))
|
||||
}
|
||||
|
||||
return unmarshalToObject(s.typer, s.creater, actual, into, unk.Raw)
|
||||
return unmarshalToObject(s.typer, s.creater, &actual, into, unk.Raw)
|
||||
}
|
||||
|
||||
// EncodeToStream serializes the provided object to the given writer. Overrides is ignored.
|
||||
func (s *Serializer) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
var unk runtime.Unknown
|
||||
if kind := obj.GetObjectKind().GroupVersionKind(); kind != nil {
|
||||
unk = runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{
|
||||
Kind: kind.Kind,
|
||||
APIVersion: kind.GroupVersion().String(),
|
||||
},
|
||||
}
|
||||
kind := obj.GetObjectKind().GroupVersionKind()
|
||||
unk = runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{
|
||||
Kind: kind.Kind,
|
||||
APIVersion: kind.GroupVersion().String(),
|
||||
},
|
||||
}
|
||||
|
||||
prefixSize := uint64(len(s.prefix))
|
||||
@ -334,7 +333,7 @@ func (s *RawSerializer) Decode(originalData []byte, gvk *unversioned.GroupVersio
|
||||
intoUnknown.Raw = data
|
||||
intoUnknown.ContentEncoding = ""
|
||||
intoUnknown.ContentType = s.contentType
|
||||
intoUnknown.SetGroupVersionKind(actual)
|
||||
intoUnknown.SetGroupVersionKind(*actual)
|
||||
return intoUnknown, actual, nil
|
||||
}
|
||||
|
||||
|
@ -34,12 +34,12 @@ import (
|
||||
)
|
||||
|
||||
type testObject struct {
|
||||
gvk *unversioned.GroupVersionKind
|
||||
gvk unversioned.GroupVersionKind
|
||||
}
|
||||
|
||||
func (d *testObject) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testObject) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testObject) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
|
||||
func (d *testObject) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testObject) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testObject) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
|
||||
|
||||
type testMarshalable struct {
|
||||
testObject
|
||||
@ -106,7 +106,7 @@ func TestEncode(t *testing.T) {
|
||||
0x22, 0x00, // content-encoding
|
||||
}
|
||||
obj2 := &testMarshalable{
|
||||
testObject: testObject{gvk: &unversioned.GroupVersionKind{Kind: "test", Group: "other", Version: "version"}},
|
||||
testObject: testObject{gvk: unversioned.GroupVersionKind{Kind: "test", Group: "other", Version: "version"}},
|
||||
data: []byte{0x01, 0x02, 0x03},
|
||||
}
|
||||
wire2 := []byte{
|
||||
|
@ -227,10 +227,12 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
|
||||
}
|
||||
|
||||
if (c.encodeVersion == nil && len(overrides) == 0) || isUnversioned {
|
||||
old := obj.GetObjectKind().GroupVersionKind()
|
||||
obj.GetObjectKind().SetGroupVersionKind(gvk)
|
||||
defer obj.GetObjectKind().SetGroupVersionKind(old)
|
||||
return c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
objectKind := obj.GetObjectKind()
|
||||
old := objectKind.GroupVersionKind()
|
||||
objectKind.SetGroupVersionKind(*gvk)
|
||||
err = c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
objectKind.SetGroupVersionKind(old)
|
||||
return err
|
||||
}
|
||||
|
||||
targetGV, ok := c.encodeVersion[gvk.Group]
|
||||
@ -270,13 +272,14 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
|
||||
} else {
|
||||
obj = out
|
||||
}
|
||||
} else {
|
||||
old := obj.GetObjectKind().GroupVersionKind()
|
||||
defer obj.GetObjectKind().SetGroupVersionKind(old)
|
||||
obj.GetObjectKind().SetGroupVersionKind(&unversioned.GroupVersionKind{Group: targetGV.Group, Version: targetGV.Version, Kind: gvk.Kind})
|
||||
}
|
||||
|
||||
return c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
objectKind := obj.GetObjectKind()
|
||||
old := objectKind.GroupVersionKind()
|
||||
objectKind.SetGroupVersionKind(unversioned.GroupVersionKind{Group: targetGV.Group, Version: targetGV.Version, Kind: gvk.Kind})
|
||||
err = c.encoder.EncodeToStream(obj, w, overrides...)
|
||||
objectKind.SetGroupVersionKind(old)
|
||||
return err
|
||||
}
|
||||
|
||||
// promoteOrPrependGroupVersion finds the group version in the provided group versions that has the same group as target.
|
||||
|
@ -30,12 +30,12 @@ import (
|
||||
type testDecodable struct {
|
||||
Other string
|
||||
Value int `json:"value"`
|
||||
gvk *unversioned.GroupVersionKind
|
||||
gvk unversioned.GroupVersionKind
|
||||
}
|
||||
|
||||
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testDecodable) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testDecodable) GroupVersionKind() *unversioned.GroupVersionKind { return d.gvk }
|
||||
func (d *testDecodable) GetObjectKind() unversioned.ObjectKind { return d }
|
||||
func (d *testDecodable) SetGroupVersionKind(gvk unversioned.GroupVersionKind) { d.gvk = gvk }
|
||||
func (d *testDecodable) GroupVersionKind() unversioned.GroupVersionKind { return d.gvk }
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
gvk1 := &unversioned.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}
|
||||
|
@ -301,18 +301,18 @@ func (u *Unstructured) SetAnnotations(annotations map[string]string) {
|
||||
u.setNestedMap(annotations, "metadata", "annotations")
|
||||
}
|
||||
|
||||
func (u *Unstructured) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (u *Unstructured) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
u.SetAPIVersion(gvk.GroupVersion().String())
|
||||
u.SetKind(gvk.Kind)
|
||||
}
|
||||
|
||||
func (u *Unstructured) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (u *Unstructured) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
gv, err := unversioned.ParseGroupVersion(u.GetAPIVersion())
|
||||
if err != nil {
|
||||
return nil
|
||||
return unversioned.GroupVersionKind{}
|
||||
}
|
||||
gvk := gv.WithKind(u.GetKind())
|
||||
return &gvk
|
||||
return gvk
|
||||
}
|
||||
|
||||
// UnstructuredList allows lists that do not have Golang structs
|
||||
@ -364,18 +364,18 @@ func (u *UnstructuredList) SetSelfLink(selfLink string) {
|
||||
u.setNestedField(selfLink, "metadata", "selfLink")
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) SetGroupVersionKind(gvk *unversioned.GroupVersionKind) {
|
||||
func (u *UnstructuredList) SetGroupVersionKind(gvk unversioned.GroupVersionKind) {
|
||||
u.SetAPIVersion(gvk.GroupVersion().String())
|
||||
u.SetKind(gvk.Kind)
|
||||
}
|
||||
|
||||
func (u *UnstructuredList) GroupVersionKind() *unversioned.GroupVersionKind {
|
||||
func (u *UnstructuredList) GroupVersionKind() unversioned.GroupVersionKind {
|
||||
gv, err := unversioned.ParseGroupVersion(u.GetAPIVersion())
|
||||
if err != nil {
|
||||
return nil
|
||||
return unversioned.GroupVersionKind{}
|
||||
}
|
||||
gvk := gv.WithKind(u.GetKind())
|
||||
return &gvk
|
||||
return gvk
|
||||
}
|
||||
|
||||
// VersionedObjects is used by Decoders to give callers a way to access all versions
|
||||
|
@ -45,10 +45,10 @@ func (s unstructuredJSONScheme) Decode(data []byte, _ *unversioned.GroupVersionK
|
||||
|
||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||
if len(gvk.Kind) == 0 {
|
||||
return nil, gvk, NewMissingKindErr(string(data))
|
||||
return nil, &gvk, NewMissingKindErr(string(data))
|
||||
}
|
||||
|
||||
return obj, gvk, nil
|
||||
return obj, &gvk, nil
|
||||
}
|
||||
|
||||
func (unstructuredJSONScheme) EncodeToStream(obj Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
||||
|
Loading…
Reference in New Issue
Block a user