@@ -19,6 +19,7 @@ package versioning
1919import (
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