@@ -17,9 +17,6 @@ limitations under the License.
17
17
package serializer
18
18
19
19
import (
20
- "mime"
21
- "strings"
22
-
23
20
"k8s.io/apimachinery/pkg/runtime"
24
21
"k8s.io/apimachinery/pkg/runtime/schema"
25
22
"k8s.io/apimachinery/pkg/runtime/serializer/json"
@@ -28,41 +25,26 @@ import (
28
25
"k8s.io/apimachinery/pkg/runtime/serializer/versioning"
29
26
)
30
27
31
- // serializerExtensions are for serializers that are conditionally compiled in
32
- var serializerExtensions = []func (* runtime.Scheme ) (serializerType , bool ){}
33
-
34
- type serializerType struct {
35
- AcceptContentTypes []string
36
- ContentType string
37
- FileExtensions []string
38
- // EncodesAsText should be true if this content type can be represented safely in UTF-8
39
- EncodesAsText bool
40
-
41
- Serializer runtime.Serializer
42
- PrettySerializer runtime.Serializer
43
- StrictSerializer runtime.Serializer
44
-
45
- AcceptStreamContentTypes []string
46
- StreamContentType string
47
-
48
- Framer runtime.Framer
49
- StreamSerializer runtime.Serializer
50
- }
51
-
52
- func newSerializersForScheme (scheme * runtime.Scheme , mf json.MetaFactory , options CodecFactoryOptions ) []serializerType {
28
+ func newSerializersForScheme (scheme * runtime.Scheme , mf json.MetaFactory , options CodecFactoryOptions ) []runtime.SerializerInfo {
53
29
jsonSerializer := json .NewSerializerWithOptions (
54
30
mf , scheme , scheme ,
55
31
json.SerializerOptions {Yaml : false , Pretty : false , Strict : options .Strict },
56
32
)
57
- jsonSerializerType := serializerType {
58
- AcceptContentTypes : []string {runtime .ContentTypeJSON },
59
- ContentType : runtime .ContentTypeJSON ,
60
- FileExtensions : []string {"json" },
61
- EncodesAsText : true ,
62
- Serializer : jsonSerializer ,
63
-
64
- Framer : json .Framer ,
65
- StreamSerializer : jsonSerializer ,
33
+ jsonSerializerType := runtime.SerializerInfo {
34
+ MediaType : runtime .ContentTypeJSON ,
35
+ MediaTypeType : "application" ,
36
+ MediaTypeSubType : "json" ,
37
+ EncodesAsText : true ,
38
+ Serializer : jsonSerializer ,
39
+ StrictSerializer : json .NewSerializerWithOptions (
40
+ mf , scheme , scheme ,
41
+ json.SerializerOptions {Yaml : false , Pretty : false , Strict : true },
42
+ ),
43
+ StreamSerializer : & runtime.StreamSerializerInfo {
44
+ EncodesAsText : true ,
45
+ Serializer : jsonSerializer ,
46
+ Framer : json .Framer ,
47
+ },
66
48
}
67
49
if options .Pretty {
68
50
jsonSerializerType .PrettySerializer = json .NewSerializerWithOptions (
@@ -71,12 +53,6 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory, option
71
53
)
72
54
}
73
55
74
- strictJSONSerializer := json .NewSerializerWithOptions (
75
- mf , scheme , scheme ,
76
- json.SerializerOptions {Yaml : false , Pretty : false , Strict : true },
77
- )
78
- jsonSerializerType .StrictSerializer = strictJSONSerializer
79
-
80
56
yamlSerializer := json .NewSerializerWithOptions (
81
57
mf , scheme , scheme ,
82
58
json.SerializerOptions {Yaml : true , Pretty : false , Strict : options .Strict },
@@ -88,35 +64,31 @@ func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory, option
88
64
protoSerializer := protobuf .NewSerializer (scheme , scheme )
89
65
protoRawSerializer := protobuf .NewRawSerializer (scheme , scheme )
90
66
91
- serializers := []serializerType {
67
+ serializers := []runtime. SerializerInfo {
92
68
jsonSerializerType ,
93
69
{
94
- AcceptContentTypes : [] string { runtime .ContentTypeYAML } ,
95
- ContentType : runtime . ContentTypeYAML ,
96
- FileExtensions : [] string { "yaml" } ,
97
- EncodesAsText : true ,
98
- Serializer : yamlSerializer ,
99
- StrictSerializer : strictYAMLSerializer ,
70
+ MediaType : runtime .ContentTypeYAML ,
71
+ MediaTypeType : "application" ,
72
+ MediaTypeSubType : "yaml" ,
73
+ EncodesAsText : true ,
74
+ Serializer : yamlSerializer ,
75
+ StrictSerializer : strictYAMLSerializer ,
100
76
},
101
77
{
102
- AcceptContentTypes : [] string { runtime .ContentTypeProtobuf } ,
103
- ContentType : runtime . ContentTypeProtobuf ,
104
- FileExtensions : [] string { "pb" } ,
105
- Serializer : protoSerializer ,
78
+ MediaType : runtime .ContentTypeProtobuf ,
79
+ MediaTypeType : "application" ,
80
+ MediaTypeSubType : "vnd.kubernetes.protobuf" ,
81
+ Serializer : protoSerializer ,
106
82
// note, strict decoding is unsupported for protobuf,
107
83
// fall back to regular serializing
108
84
StrictSerializer : protoSerializer ,
109
-
110
- Framer : protobuf .LengthDelimitedFramer ,
111
- StreamSerializer : protoRawSerializer ,
85
+ StreamSerializer : & runtime.StreamSerializerInfo {
86
+ Serializer : protoRawSerializer ,
87
+ Framer : protobuf .LengthDelimitedFramer ,
88
+ },
112
89
},
113
90
}
114
91
115
- for _ , fn := range serializerExtensions {
116
- if serializer , ok := fn (scheme ); ok {
117
- serializers = append (serializers , serializer )
118
- }
119
- }
120
92
return serializers
121
93
}
122
94
@@ -184,46 +156,28 @@ func NewCodecFactory(scheme *runtime.Scheme, mutators ...CodecFactoryOptionsMuta
184
156
}
185
157
186
158
// newCodecFactory is a helper for testing that allows a different metafactory to be specified.
187
- func newCodecFactory (scheme * runtime.Scheme , serializers []serializerType ) CodecFactory {
159
+ func newCodecFactory (scheme * runtime.Scheme , serializers []runtime. SerializerInfo ) CodecFactory {
188
160
decoders := make ([]runtime.Decoder , 0 , len (serializers ))
189
161
var accepts []runtime.SerializerInfo
190
162
alreadyAccepted := make (map [string ]struct {})
191
163
192
164
var legacySerializer runtime.Serializer
193
165
for _ , d := range serializers {
194
166
decoders = append (decoders , d .Serializer )
195
- for _ , mediaType := range d .AcceptContentTypes {
196
- if _ , ok := alreadyAccepted [mediaType ]; ok {
197
- continue
198
- }
199
- alreadyAccepted [mediaType ] = struct {}{}
200
- info := runtime.SerializerInfo {
201
- MediaType : d .ContentType ,
202
- EncodesAsText : d .EncodesAsText ,
203
- Serializer : d .Serializer ,
204
- PrettySerializer : d .PrettySerializer ,
205
- StrictSerializer : d .StrictSerializer ,
206
- }
207
-
208
- mediaType , _ , err := mime .ParseMediaType (info .MediaType )
209
- if err != nil {
210
- panic (err )
211
- }
212
- parts := strings .SplitN (mediaType , "/" , 2 )
213
- info .MediaTypeType = parts [0 ]
214
- info .MediaTypeSubType = parts [1 ]
215
-
216
- if d .StreamSerializer != nil {
217
- info .StreamSerializer = & runtime.StreamSerializerInfo {
218
- Serializer : d .StreamSerializer ,
219
- EncodesAsText : d .EncodesAsText ,
220
- Framer : d .Framer ,
221
- }
222
- }
223
- accepts = append (accepts , info )
224
- if mediaType == runtime .ContentTypeJSON {
225
- legacySerializer = d .Serializer
226
- }
167
+ if _ , ok := alreadyAccepted [d .MediaType ]; ok {
168
+ continue
169
+ }
170
+ alreadyAccepted [d .MediaType ] = struct {}{}
171
+
172
+ acceptedSerializerShallowCopy := d
173
+ if d .StreamSerializer != nil {
174
+ cloned := * d .StreamSerializer
175
+ acceptedSerializerShallowCopy .StreamSerializer = & cloned
176
+ }
177
+ accepts = append (accepts , acceptedSerializerShallowCopy )
178
+
179
+ if d .MediaType == runtime .ContentTypeJSON {
180
+ legacySerializer = d .Serializer
227
181
}
228
182
}
229
183
if legacySerializer == nil {
0 commit comments