apimachinery duct tape: handle empty unstructured GV in versioning codec gracefully

This commit is contained in:
Dr. Stefan Schimanski 2018-04-05 13:47:44 +02:00
parent 556f8ccbdd
commit ca9d1f728b

View File

@ -171,8 +171,11 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
case *runtime.Unknown:
return c.encoder.Encode(obj, w)
case runtime.Unstructured:
// avoid conversion roundtrip if GVK is the right one already
// avoid conversion roundtrip if GVK is the right one already or is empty (yes, this is a hack, but the old behaviour we rely on in kubectl)
objGVK := obj.GetObjectKind().GroupVersionKind()
if len(objGVK.Version) == 0 {
return c.encoder.Encode(obj, w)
}
targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
if !ok {
return runtime.NewNotRegisteredErrForTarget(reflect.TypeOf(obj).Elem(), c.encodeVersion)