Set GVK when decoding protobuf

k3s-v1.15.3
Jordan Liggitt 2019-05-29 10:45:29 -04:00
parent 0c0caefd86
commit 8f26874577
4 changed files with 8 additions and 8 deletions

View File

@ -40,7 +40,7 @@ import (
)
func TestUniversalDeserializer(t *testing.T) {
expected := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test"}}
expected := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test"}, TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}}
d := legacyscheme.Codecs.UniversalDeserializer()
for _, mediaType := range []string{"application/json", "application/yaml", "application/vnd.kubernetes.protobuf"} {
info, ok := runtime.SerializerInfoForMediaType(legacyscheme.Codecs.SupportedMediaTypes(), mediaType)

View File

@ -141,12 +141,7 @@ func RoundTripExternalTypes(t *testing.T, scheme *runtime.Scheme, codecFactory r
continue
}
t.Run(gvk.Group+"."+gvk.Version+"."+gvk.Kind, func(t *testing.T) {
// FIXME: this is explicitly testing w/o protobuf which was failing if enabled
// the reason for that is that protobuf is not setting Kind and APIVersion fields
// during obj2 decode, the same then applies to DecodeInto obj3. My guess is we
// should be setting these two fields accordingly when protobuf is passed as codec
// to roundTrip method.
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
})
}
}

View File

@ -405,6 +405,9 @@ func unmarshalToObject(typer runtime.ObjectTyper, creater runtime.ObjectCreater,
if err := proto.Unmarshal(data, pb); err != nil {
return nil, actual, err
}
if actual != nil {
obj.GetObjectKind().SetGroupVersionKind(*actual)
}
return obj, actual, nil
}

View File

@ -320,13 +320,15 @@ func TestDecodeObjects(t *testing.T) {
wire1 = append([]byte{0x6b, 0x38, 0x73, 0x00}, wire1...)
obj1WithKind := obj1.DeepCopyObject()
obj1WithKind.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Carp"})
testCases := []struct {
obj runtime.Object
data []byte
errFn func(error) bool
}{
{
obj: obj1,
obj: obj1WithKind,
data: wire1,
},
}