mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 07:27:21 +00:00
Merge pull request #17796 from deads2k/gv-object-typer
Auto commit by PR queue bot
This commit is contained in:
@@ -53,10 +53,11 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
||||
// if we are building an object reference to something not yet persisted, we should fallback to scheme
|
||||
kind := meta.Kind()
|
||||
if kind == "" {
|
||||
_, kind, err = Scheme.ObjectVersionAndKind(obj)
|
||||
gvk, err := Scheme.ObjectKind(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kind = gvk.Kind
|
||||
}
|
||||
|
||||
// if the object referenced is actually persisted, we can also get version from meta
|
||||
|
||||
@@ -111,9 +111,9 @@ func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.Obje
|
||||
if err != nil {
|
||||
return nil, "", errors.NewInternalError(err)
|
||||
}
|
||||
_, kind, err := typer.ObjectVersionAndKind(obj)
|
||||
gvk, err := typer.ObjectKind(obj)
|
||||
if err != nil {
|
||||
return nil, "", errors.NewInternalError(err)
|
||||
}
|
||||
return objectMeta, kind, nil
|
||||
return objectMeta, gvk.Kind, nil
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ func fuzzInternalObject(t *testing.T, forVersion string, item runtime.Object, se
|
||||
func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
|
||||
printer := spew.ConfigState{DisableMethods: true}
|
||||
|
||||
gvk, err := api.Scheme.ObjectKind(item)
|
||||
t.Logf("fully qualified kind for %v is %v with codec %v", reflect.TypeOf(item), gvk, codec)
|
||||
|
||||
name := reflect.TypeOf(item).Elem().Name()
|
||||
data, err := codec.Encode(item)
|
||||
if err != nil {
|
||||
@@ -96,7 +99,7 @@ func roundTrip(t *testing.T, codec runtime.Codec, item runtime.Object) {
|
||||
func roundTripSame(t *testing.T, item runtime.Object, except ...string) {
|
||||
set := sets.NewString(except...)
|
||||
seed := rand.Int63()
|
||||
fuzzInternalObject(t, "", item, seed)
|
||||
fuzzInternalObject(t, testapi.Default.InternalGroupVersion().String(), item, seed)
|
||||
|
||||
version := testapi.Default.VersionUnderTest
|
||||
codecs := []runtime.Codec{}
|
||||
@@ -154,6 +157,7 @@ func TestRoundTripTypes(t *testing.T) {
|
||||
// defer api.Scheme.Log(nil)
|
||||
|
||||
for kind := range api.Scheme.KnownTypes(testapi.Default.InternalGroupVersion()) {
|
||||
t.Logf("working on %v in %v", kind, testapi.Default.InternalGroupVersion())
|
||||
if nonRoundTrippableTypes.Has(kind) {
|
||||
continue
|
||||
}
|
||||
@@ -168,18 +172,18 @@ func TestRoundTripTypes(t *testing.T) {
|
||||
}
|
||||
|
||||
func doRoundTripTest(kind string, t *testing.T) {
|
||||
item, err := api.Scheme.New("", kind)
|
||||
item, err := api.Scheme.New(testapi.Default.InternalGroupVersion().String(), kind)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't make a %v? %v", kind, err)
|
||||
}
|
||||
if _, err := meta.TypeAccessor(item); err != nil {
|
||||
t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableTypes: %v", kind, err)
|
||||
}
|
||||
if api.Scheme.Recognizes(testapi.Default.VersionUnderTest, kind) {
|
||||
if api.Scheme.Recognizes(testapi.Default.GroupVersion().WithKind(kind)) {
|
||||
roundTripSame(t, item, nonRoundTrippableTypesByVersion[kind]...)
|
||||
}
|
||||
if !nonInternalRoundTrippableTypes.Has(kind) {
|
||||
roundTrip(t, api.Codec, fuzzInternalObject(t, "", item, rand.Int63()))
|
||||
roundTrip(t, api.Codec, fuzzInternalObject(t, testapi.Default.InternalGroupVersion().String(), item, rand.Int63()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -214,22 +214,23 @@ func (g TestGroup) RESTMapper() meta.RESTMapper {
|
||||
|
||||
// Get codec based on runtime.Object
|
||||
func GetCodecForObject(obj runtime.Object) (runtime.Codec, error) {
|
||||
_, kind, err := api.Scheme.ObjectVersionAndKind(obj)
|
||||
gvk, err := api.Scheme.ObjectKind(obj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unexpected encoding error: %v", err)
|
||||
}
|
||||
// TODO: caesarxuchao: we should detect which group an object belongs to
|
||||
// by using the version returned by Schem.ObjectVersionAndKind() once we
|
||||
// split the schemes for internal objects.
|
||||
// TODO: caesarxuchao: we should add a map from kind to group in Scheme.
|
||||
|
||||
for _, group := range Groups {
|
||||
if api.Scheme.Recognizes(group.GroupAndVersion(), kind) {
|
||||
if group.GroupVersion().Group != gvk.Group {
|
||||
continue
|
||||
}
|
||||
|
||||
if api.Scheme.Recognizes(gvk) {
|
||||
return group.Codec(), nil
|
||||
}
|
||||
}
|
||||
// Codec used for unversioned types
|
||||
if api.Scheme.Recognizes("", kind) {
|
||||
if api.Scheme.Recognizes(gvk) {
|
||||
return api.Codec, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unexpected kind: %v", kind)
|
||||
return nil, fmt.Errorf("unexpected kind: %v", gvk)
|
||||
}
|
||||
|
||||
@@ -175,8 +175,10 @@ func FuzzerFor(t *testing.T, version string, src rand.Source) *fuzz.Fuzzer {
|
||||
// TODO: uncomment when round trip starts from a versioned object
|
||||
if true { //c.RandBool() {
|
||||
*j = &runtime.Unknown{
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Something", APIVersion: "unknown"},
|
||||
RawJSON: []byte(`{"apiVersion":"unknown","kind":"Something","someKey":"someValue"}`),
|
||||
// apiVersion has rules now. Since it includes <group>/<version> and only `v1` can be bare,
|
||||
// then this must choose a valid format to deserialize
|
||||
TypeMeta: runtime.TypeMeta{Kind: "Something", APIVersion: "unknown.group/unknown"},
|
||||
RawJSON: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
|
||||
}
|
||||
} else {
|
||||
types := []runtime.Object{&api.Pod{}, &api.ReplicationController{}}
|
||||
|
||||
@@ -93,7 +93,7 @@ func (gvk GroupVersionKind) GroupVersion() GroupVersion {
|
||||
return GroupVersion{Group: gvk.Group, Version: gvk.Version}
|
||||
}
|
||||
|
||||
func (gvk *GroupVersionKind) String() string {
|
||||
func (gvk GroupVersionKind) String() string {
|
||||
return gvk.Group + "/" + gvk.Version + ", Kind=" + gvk.Kind
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user