Skip to content

Commit 840a63c

Browse files
benluddyk8s-publishing-bot
authored andcommitted
Add CBOR serializer option to disable JSON transcoding of raw types.
Kubernetes-commit: d638d64572e1e0086c4e762aee95c6e7a5db1a9e
1 parent 02c4999 commit 840a63c

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

pkg/runtime/serializer/cbor/cbor.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,30 @@ type Serializer interface {
6868
var _ Serializer = &serializer{}
6969

7070
type options struct {
71-
strict bool
71+
strict bool
72+
transcode bool
7273
}
7374

7475
type Option func(*options)
7576

77+
// Strict configures a serializer to return a strict decoding error when it encounters map keys that
78+
// do not correspond to a field in the target object of a decode operation. This option is disabled
79+
// by default.
7680
func Strict(s bool) Option {
7781
return func(opts *options) {
7882
opts.strict = s
7983
}
8084
}
8185

86+
// Transcode configures a serializer to transcode the "raw" bytes of a decoded runtime.RawExtension
87+
// or metav1.FieldsV1 object to JSON. This is enabled by default to support existing programs that
88+
// depend on the assumption that objects of either type contain valid JSON.
89+
func Transcode(s bool) Option {
90+
return func(opts *options) {
91+
opts.transcode = s
92+
}
93+
}
94+
8295
type serializer struct {
8396
metaFactory metaFactory
8497
creater runtime.ObjectCreater
@@ -88,6 +101,8 @@ type serializer struct {
88101

89102
func (serializer) private() {}
90103

104+
// NewSerializer creates and returns a serializer configured with the provided options. The default
105+
// options are equivalent to explicitly passing Strict(false) and Transcode(true).
91106
func NewSerializer(creater runtime.ObjectCreater, typer runtime.ObjectTyper, options ...Option) Serializer {
92107
return newSerializer(&defaultMetaFactory{}, creater, typer, options...)
93108
}
@@ -98,6 +113,7 @@ func newSerializer(metaFactory metaFactory, creater runtime.ObjectCreater, typer
98113
creater: creater,
99114
typer: typer,
100115
}
116+
s.options.transcode = true
101117
for _, o := range options {
102118
o(&s.options)
103119
}
@@ -337,9 +353,10 @@ func (s *serializer) Decode(data []byte, gvk *schema.GroupVersionKind, into runt
337353
return nil, actual, err
338354
}
339355

340-
// TODO: Make possible to disable this behavior.
341-
if err := transcodeRawTypes(obj); err != nil {
342-
return nil, actual, err
356+
if s.options.transcode {
357+
if err := transcodeRawTypes(obj); err != nil {
358+
return nil, actual, err
359+
}
343360
}
344361

345362
return obj, actual, strict

pkg/runtime/serializer/cbor/cbor_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,33 @@ func TestDecode(t *testing.T) {
345345
}
346346
},
347347
},
348+
{
349+
name: "raw types not transcoded",
350+
options: []Option{Transcode(false)},
351+
data: []byte{0xa4, 0x41, 'f', 0xa1, 0x41, 'a', 0x01, 0x42, 'f', 'p', 0xa1, 0x41, 'z', 0x02, 0x41, 'r', 0xa1, 0x41, 'b', 0x03, 0x42, 'r', 'p', 0xa1, 0x41, 'y', 0x04},
352+
gvk: &schema.GroupVersionKind{},
353+
metaFactory: stubMetaFactory{gvk: &schema.GroupVersionKind{}},
354+
typer: stubTyper{gvks: []schema.GroupVersionKind{{Group: "x", Version: "y", Kind: "z"}}},
355+
into: &structWithRawFields{},
356+
expectedObj: &structWithRawFields{
357+
FieldsV1: metav1.FieldsV1{Raw: []byte{0xa1, 0x41, 'a', 0x01}},
358+
FieldsV1Pointer: &metav1.FieldsV1{Raw: []byte{0xa1, 0x41, 'z', 0x02}},
359+
// RawExtension's UnmarshalCBOR ensures the self-described CBOR tag
360+
// is present in the result so that there is never any ambiguity in
361+
// distinguishing CBOR from JSON or Protobuf. It is unnecessary for
362+
// FieldsV1 to do the same because the initial byte is always
363+
// sufficient to distinguish a valid JSON-encoded FieldsV1 from a
364+
// valid CBOR-encoded FieldsV1.
365+
RawExtension: runtime.RawExtension{Raw: []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x41, 'b', 0x03}},
366+
RawExtensionPointer: &runtime.RawExtension{Raw: []byte{0xd9, 0xd9, 0xf7, 0xa1, 0x41, 'y', 0x04}},
367+
},
368+
expectedGVK: &schema.GroupVersionKind{Group: "x", Version: "y", Kind: "z"},
369+
assertOnError: func(t *testing.T, err error) {
370+
if err != nil {
371+
t.Errorf("expected nil error, got: %v", err)
372+
}
373+
},
374+
},
348375
{
349376
name: "object with embedded typemeta and objectmeta",
350377
data: []byte("\xa2\x48metadata\xa1\x44name\x43foo\x44spec\xa0"), // {"metadata": {"name": "foo"}}

0 commit comments

Comments
 (0)