Skip to content

Commit 8e731f2

Browse files
committed
Do not bypass same version unstructed conversion if it is a list
This give the converter a chance to convert list items to the requested version.
1 parent 40ac701 commit 8e731f2

File tree

1 file changed

+17
-11
lines changed
  • staging/src/k8s.io/apimachinery/pkg/runtime/serializer/versioning

1 file changed

+17
-11
lines changed

staging/src/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package versioning
1919
import (
2020
"io"
2121

22+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2223
"k8s.io/apimachinery/pkg/runtime"
2324
"k8s.io/apimachinery/pkg/runtime/schema"
2425
)
@@ -170,17 +171,22 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
170171
case *runtime.Unknown:
171172
return c.encoder.Encode(obj, w)
172173
case runtime.Unstructured:
173-
// 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)
174-
objGVK := obj.GetObjectKind().GroupVersionKind()
175-
if len(objGVK.Version) == 0 {
176-
return c.encoder.Encode(obj, w)
177-
}
178-
targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
179-
if !ok {
180-
return runtime.NewNotRegisteredGVKErrForTarget(objGVK, c.encodeVersion)
181-
}
182-
if targetGVK == objGVK {
183-
return c.encoder.Encode(obj, w)
174+
// An unstructured list can contain objects of multiple group version kinds. don't short-circuit just
175+
// because the top-level type matches our desired destination type. actually send the object to the converter
176+
// to give it a chance to convert the list items if needed.
177+
if _, ok := obj.(*unstructured.UnstructuredList); !ok {
178+
// 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)
179+
objGVK := obj.GetObjectKind().GroupVersionKind()
180+
if len(objGVK.Version) == 0 {
181+
return c.encoder.Encode(obj, w)
182+
}
183+
targetGVK, ok := c.encodeVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{objGVK})
184+
if !ok {
185+
return runtime.NewNotRegisteredGVKErrForTarget(objGVK, c.encodeVersion)
186+
}
187+
if targetGVK == objGVK {
188+
return c.encoder.Encode(obj, w)
189+
}
184190
}
185191
}
186192

0 commit comments

Comments
 (0)