Skip to content

Commit 5602d77

Browse files
committed
Fix versioning serializer
1 parent 3cbb20c commit 5602d77

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,22 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
199199
return err
200200
}
201201

202+
objectKind := obj.GetObjectKind()
203+
old := objectKind.GroupVersionKind()
204+
// restore the old GVK after encoding
205+
defer objectKind.SetGroupVersionKind(old)
206+
202207
if c.encodeVersion == nil || isUnversioned {
203208
if e, ok := obj.(runtime.NestedObjectEncoder); ok {
204209
if err := e.EncodeNestedObjects(runtime.WithVersionEncoder{Encoder: c.encoder, ObjectTyper: c.typer}); err != nil {
205210
return err
206211
}
207212
}
208-
objectKind := obj.GetObjectKind()
209-
old := objectKind.GroupVersionKind()
210213
objectKind.SetGroupVersionKind(gvks[0])
211-
err = c.encoder.Encode(obj, w)
212-
objectKind.SetGroupVersionKind(old)
213-
return err
214+
return c.encoder.Encode(obj, w)
214215
}
215216

216217
// Perform a conversion if necessary
217-
objectKind := obj.GetObjectKind()
218-
old := objectKind.GroupVersionKind()
219218
out, err := c.convertor.ConvertToVersion(obj, c.encodeVersion)
220219
if err != nil {
221220
return err
@@ -228,10 +227,7 @@ func (c *codec) Encode(obj runtime.Object, w io.Writer) error {
228227
}
229228

230229
// Conversion is responsible for setting the proper group, version, and kind onto the outgoing object
231-
err = c.encoder.Encode(out, w)
232-
// restore the old GVK, in case conversion returned the same object
233-
objectKind.SetGroupVersionKind(old)
234-
return err
230+
return c.encoder.Encode(out, w)
235231
}
236232

237233
// DirectEncoder was moved and renamed to runtime.WithVersionEncoder in 1.15.

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ func TestNestedEncode(t *testing.T) {
102102
}
103103
}
104104

105+
func TestNestedEncodeError(t *testing.T) {
106+
n := &testNestedDecodable{nestedErr: fmt.Errorf("unable to encode")}
107+
gvk1 := schema.GroupVersionKind{Kind: "test", Group: "other", Version: "v1"}
108+
gvk2 := schema.GroupVersionKind{Kind: "test", Group: "other", Version: "v2"}
109+
n.SetGroupVersionKind(gvk1)
110+
codec := NewCodec(
111+
nil, nil,
112+
&mockConvertor{},
113+
nil,
114+
&mockTyper{gvks: []schema.GroupVersionKind{gvk1, gvk2}},
115+
nil,
116+
schema.GroupVersion{Group: "other", Version: "v2"}, nil,
117+
"TestNestedEncodeError",
118+
)
119+
if err := codec.Encode(n, ioutil.Discard); err != n.nestedErr {
120+
t.Errorf("unexpected error: %v", err)
121+
}
122+
if n.GroupVersionKind() != gvk1 {
123+
t.Errorf("unexpected gvk of input object: %v", n.GroupVersionKind())
124+
}
125+
}
126+
105127
func TestDecode(t *testing.T) {
106128
gvk1 := &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"}
107129
decodable1 := &testDecodable{}
@@ -311,6 +333,28 @@ func (c *checkConvertor) ConvertFieldLabel(gvk schema.GroupVersionKind, label, v
311333
return "", "", fmt.Errorf("unexpected call to ConvertFieldLabel")
312334
}
313335

336+
type mockConvertor struct {
337+
}
338+
339+
func (c *mockConvertor) Convert(in, out, context interface{}) error {
340+
return fmt.Errorf("unexpect call to Convert")
341+
}
342+
343+
func (c *mockConvertor) ConvertToVersion(in runtime.Object, outVersion runtime.GroupVersioner) (out runtime.Object, err error) {
344+
objectKind := in.GetObjectKind()
345+
inGVK := objectKind.GroupVersionKind()
346+
if out, ok := outVersion.KindForGroupVersionKinds([]schema.GroupVersionKind{inGVK}); ok {
347+
objectKind.SetGroupVersionKind(out)
348+
} else {
349+
return nil, fmt.Errorf("unexpected conversion")
350+
}
351+
return in, nil
352+
}
353+
354+
func (c *mockConvertor) ConvertFieldLabel(gvk schema.GroupVersionKind, label, value string) (string, string, error) {
355+
return "", "", fmt.Errorf("unexpected call to ConvertFieldLabel")
356+
}
357+
314358
type mockSerializer struct {
315359
err error
316360
obj runtime.Object

0 commit comments

Comments
 (0)