Skip to content

Commit 0489d0b

Browse files
Unify runtime.SerializerInfo with negotiate.AcceptedMediaTypes
There was no reason to have two types and this avoids ~10% of allocations on the GET code path. ``` BenchmarkGet-12 100000 109045 ns/op 17608 B/op 146 allocs/op BenchmarkGet-12 100000 108850 ns/op 15942 B/op 132 allocs/op ```
1 parent 59b4f47 commit 0489d0b

File tree

9 files changed

+100
-57
lines changed

9 files changed

+100
-57
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.Serial
644644
return []runtime.SerializerInfo{
645645
{
646646
MediaType: "application/json",
647+
MediaTypeType: "application",
648+
MediaTypeSubType: "json",
647649
EncodesAsText: true,
648650
Serializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, false),
649651
PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, true),
@@ -654,9 +656,11 @@ func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.Serial
654656
},
655657
},
656658
{
657-
MediaType: "application/yaml",
658-
EncodesAsText: true,
659-
Serializer: json.NewYAMLSerializer(json.DefaultMetaFactory, s.creator, s.typer),
659+
MediaType: "application/yaml",
660+
MediaTypeType: "application",
661+
MediaTypeSubType: "yaml",
662+
EncodesAsText: true,
663+
Serializer: json.NewYAMLSerializer(json.DefaultMetaFactory, s.creator, s.typer),
660664
},
661665
}
662666
}

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme/scheme.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.Serial
5151
return []runtime.SerializerInfo{
5252
{
5353
MediaType: "application/json",
54+
MediaTypeType: "application",
55+
MediaTypeSubType: "json",
5456
EncodesAsText: true,
5557
Serializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, false),
5658
PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, s.creator, s.typer, true),
@@ -61,9 +63,11 @@ func (s unstructuredNegotiatedSerializer) SupportedMediaTypes() []runtime.Serial
6163
},
6264
},
6365
{
64-
MediaType: "application/yaml",
65-
EncodesAsText: true,
66-
Serializer: json.NewYAMLSerializer(json.DefaultMetaFactory, s.creator, s.typer),
66+
MediaType: "application/yaml",
67+
MediaTypeType: "application",
68+
MediaTypeSubType: "yaml",
69+
EncodesAsText: true,
70+
Serializer: json.NewYAMLSerializer(json.DefaultMetaFactory, s.creator, s.typer),
6771
},
6872
}
6973
}

staging/src/k8s.io/apimachinery/pkg/runtime/interfaces.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ type Framer interface {
9191
type SerializerInfo struct {
9292
// MediaType is the value that represents this serializer over the wire.
9393
MediaType string
94+
// MediaTypeType is the first part of the MediaType ("application" in "application/json").
95+
MediaTypeType string
96+
// MediaTypeSubType is the second part of the MediaType ("json" in "application/json").
97+
MediaTypeSubType string
9498
// EncodesAsText indicates this serializer can be encoded to UTF-8 safely.
9599
EncodesAsText bool
96100
// Serializer is the individual object serializer for this media type.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package serializer
1818

1919
import (
20+
"mime"
21+
"strings"
22+
2023
"k8s.io/apimachinery/pkg/runtime"
2124
"k8s.io/apimachinery/pkg/runtime/schema"
2225
"k8s.io/apimachinery/pkg/runtime/serializer/json"
@@ -120,6 +123,15 @@ func newCodecFactory(scheme *runtime.Scheme, serializers []serializerType) Codec
120123
Serializer: d.Serializer,
121124
PrettySerializer: d.PrettySerializer,
122125
}
126+
127+
mediaType, _, err := mime.ParseMediaType(info.MediaType)
128+
if err != nil {
129+
panic(err)
130+
}
131+
parts := strings.SplitN(mediaType, "/", 2)
132+
info.MediaTypeType = parts[0]
133+
info.MediaTypeSubType = parts[1]
134+
123135
if d.StreamSerializer != nil {
124136
info.StreamSerializer = &runtime.StreamSerializerInfo{
125137
Serializer: d.StreamSerializer,

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/negotiation/negotiate.go

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ func MediaTypesForSerializer(ns runtime.NegotiatedSerializer) (mediaTypes, strea
4343
// NegotiateOutputMediaType negotiates the output structured media type and a serializer, or
4444
// returns an error.
4545
func NegotiateOutputMediaType(req *http.Request, ns runtime.NegotiatedSerializer, restrictions EndpointRestrictions) (MediaTypeOptions, runtime.SerializerInfo, error) {
46-
mediaType, ok := NegotiateMediaTypeOptions(req.Header.Get("Accept"), AcceptedMediaTypesForEndpoint(ns), restrictions)
46+
mediaType, ok := NegotiateMediaTypeOptions(req.Header.Get("Accept"), ns.SupportedMediaTypes(), restrictions)
4747
if !ok {
4848
supported, _ := MediaTypesForSerializer(ns)
4949
return mediaType, runtime.SerializerInfo{}, NewNotAcceptableError(supported)
5050
}
5151
// TODO: move into resthandler
52-
info := mediaType.Accepted.Serializer
52+
info := mediaType.Accepted
5353
if (mediaType.Pretty || isPrettyPrint(req)) && info.PrettySerializer != nil {
5454
info.Serializer = info.PrettySerializer
5555
}
@@ -58,12 +58,12 @@ func NegotiateOutputMediaType(req *http.Request, ns runtime.NegotiatedSerializer
5858

5959
// NegotiateOutputMediaTypeStream returns a stream serializer for the given request.
6060
func NegotiateOutputMediaTypeStream(req *http.Request, ns runtime.NegotiatedSerializer, restrictions EndpointRestrictions) (runtime.SerializerInfo, error) {
61-
mediaType, ok := NegotiateMediaTypeOptions(req.Header.Get("Accept"), AcceptedMediaTypesForEndpoint(ns), restrictions)
62-
if !ok || mediaType.Accepted.Serializer.StreamSerializer == nil {
61+
mediaType, ok := NegotiateMediaTypeOptions(req.Header.Get("Accept"), ns.SupportedMediaTypes(), restrictions)
62+
if !ok || mediaType.Accepted.StreamSerializer == nil {
6363
_, supported := MediaTypesForSerializer(ns)
6464
return runtime.SerializerInfo{}, NewNotAcceptableError(supported)
6565
}
66-
return mediaType.Accepted.Serializer, nil
66+
return mediaType.Accepted, nil
6767
}
6868

6969
// NegotiateInputSerializer returns the input serializer for the provided request.
@@ -99,10 +99,13 @@ func NegotiateInputSerializerForMediaType(mediaType string, streaming bool, ns r
9999
func isPrettyPrint(req *http.Request) bool {
100100
// DEPRECATED: should be part of the content type
101101
if req.URL != nil {
102-
pp := req.URL.Query().Get("pretty")
103-
if len(pp) > 0 {
104-
pretty, _ := strconv.ParseBool(pp)
105-
return pretty
102+
// avoid an allocation caused by parsing the URL query
103+
if strings.Contains(req.URL.RawQuery, "pretty") {
104+
pp := req.URL.Query().Get("pretty")
105+
if len(pp) > 0 {
106+
pretty, _ := strconv.ParseBool(pp)
107+
return pretty
108+
}
106109
}
107110
}
108111
userAgent := req.UserAgent()
@@ -139,17 +142,6 @@ func (emptyEndpointRestrictions) AllowsConversion(schema.GroupVersionKind, strin
139142
func (emptyEndpointRestrictions) AllowsServerVersion(string) bool { return false }
140143
func (emptyEndpointRestrictions) AllowsStreamSchema(s string) bool { return s == "watch" }
141144

142-
// AcceptedMediaType contains information about a valid media type that the
143-
// server can serialize.
144-
type AcceptedMediaType struct {
145-
// Type is the first part of the media type ("application")
146-
Type string
147-
// SubType is the second part of the media type ("json")
148-
SubType string
149-
// Serializer is the serialization info this object accepts
150-
Serializer runtime.SerializerInfo
151-
}
152-
153145
// MediaTypeOptions describes information for a given media type that may alter
154146
// the server response
155147
type MediaTypeOptions struct {
@@ -176,13 +168,13 @@ type MediaTypeOptions struct {
176168
Unrecognized []string
177169

178170
// the accepted media type from the client
179-
Accepted *AcceptedMediaType
171+
Accepted runtime.SerializerInfo
180172
}
181173

182174
// acceptMediaTypeOptions returns an options object that matches the provided media type params. If
183175
// it returns false, the provided options are not allowed and the media type must be skipped. These
184176
// parameters are unversioned and may not be changed.
185-
func acceptMediaTypeOptions(params map[string]string, accepts *AcceptedMediaType, endpoint EndpointRestrictions) (MediaTypeOptions, bool) {
177+
func acceptMediaTypeOptions(params map[string]string, accepts *runtime.SerializerInfo, endpoint EndpointRestrictions) (MediaTypeOptions, bool) {
186178
var options MediaTypeOptions
187179

188180
// extract all known parameters
@@ -208,7 +200,7 @@ func acceptMediaTypeOptions(params map[string]string, accepts *AcceptedMediaType
208200

209201
// controls the streaming schema
210202
case "stream":
211-
if len(v) > 0 && (accepts.Serializer.StreamSerializer == nil || !endpoint.AllowsStreamSchema(v)) {
203+
if len(v) > 0 && (accepts.StreamSerializer == nil || !endpoint.AllowsStreamSchema(v)) {
212204
return MediaTypeOptions{}, false
213205
}
214206
options.Stream = v
@@ -236,27 +228,27 @@ func acceptMediaTypeOptions(params map[string]string, accepts *AcceptedMediaType
236228
}
237229
}
238230

239-
if options.Convert != nil && !endpoint.AllowsConversion(*options.Convert, accepts.Type, accepts.SubType) {
231+
if options.Convert != nil && !endpoint.AllowsConversion(*options.Convert, accepts.MediaTypeType, accepts.MediaTypeSubType) {
240232
return MediaTypeOptions{}, false
241233
}
242234

243-
options.Accepted = accepts
235+
options.Accepted = *accepts
244236
return options, true
245237
}
246238

247239
type candidateMediaType struct {
248-
accepted *AcceptedMediaType
240+
accepted *runtime.SerializerInfo
249241
clauses goautoneg.Accept
250242
}
251243

252244
type candidateMediaTypeSlice []candidateMediaType
253245

254246
// NegotiateMediaTypeOptions returns the most appropriate content type given the accept header and
255247
// a list of alternatives along with the accepted media type parameters.
256-
func NegotiateMediaTypeOptions(header string, accepted []AcceptedMediaType, endpoint EndpointRestrictions) (MediaTypeOptions, bool) {
248+
func NegotiateMediaTypeOptions(header string, accepted []runtime.SerializerInfo, endpoint EndpointRestrictions) (MediaTypeOptions, bool) {
257249
if len(header) == 0 && len(accepted) > 0 {
258250
return MediaTypeOptions{
259-
Accepted: &accepted[0],
251+
Accepted: accepted[0],
260252
}, true
261253
}
262254

@@ -266,8 +258,8 @@ func NegotiateMediaTypeOptions(header string, accepted []AcceptedMediaType, endp
266258
for i := range accepted {
267259
accepts := &accepted[i]
268260
switch {
269-
case clause.Type == accepts.Type && clause.SubType == accepts.SubType,
270-
clause.Type == accepts.Type && clause.SubType == "*",
261+
case clause.Type == accepts.MediaTypeType && clause.SubType == accepts.MediaTypeSubType,
262+
clause.Type == accepts.MediaTypeType && clause.SubType == "*",
271263
clause.Type == "*" && clause.SubType == "*":
272264
candidates = append(candidates, candidateMediaType{accepted: accepts, clauses: clause})
273265
}
@@ -282,22 +274,3 @@ func NegotiateMediaTypeOptions(header string, accepted []AcceptedMediaType, endp
282274

283275
return MediaTypeOptions{}, false
284276
}
285-
286-
// AcceptedMediaTypesForEndpoint returns an array of structs that are used to efficiently check which
287-
// allowed media types the server exposes.
288-
func AcceptedMediaTypesForEndpoint(ns runtime.NegotiatedSerializer) []AcceptedMediaType {
289-
var acceptedMediaTypes []AcceptedMediaType
290-
for _, info := range ns.SupportedMediaTypes() {
291-
segments := strings.SplitN(info.MediaType, "/", 2)
292-
if len(segments) == 1 {
293-
segments = append(segments, "*")
294-
}
295-
t := AcceptedMediaType{
296-
Type: segments[0],
297-
SubType: segments[1],
298-
Serializer: info,
299-
}
300-
acceptedMediaTypes = append(acceptedMediaTypes, t)
301-
}
302-
return acceptedMediaTypes
303-
}

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/negotiation/negotiate_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package negotiation
1818

1919
import (
20+
"mime"
2021
"net/http"
2122
"net/url"
23+
"strings"
2224
"testing"
2325

2426
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -39,7 +41,23 @@ type fakeNegotiater struct {
3941
func (n *fakeNegotiater) SupportedMediaTypes() []runtime.SerializerInfo {
4042
var out []runtime.SerializerInfo
4143
for _, s := range n.types {
42-
info := runtime.SerializerInfo{Serializer: n.serializer, MediaType: s, EncodesAsText: true}
44+
mediaType, _, err := mime.ParseMediaType(s)
45+
if err != nil {
46+
panic(err)
47+
}
48+
parts := strings.SplitN(mediaType, "/", 2)
49+
if len(parts) == 1 {
50+
// this is an error on the server side
51+
parts = append(parts, "")
52+
}
53+
54+
info := runtime.SerializerInfo{
55+
Serializer: n.serializer,
56+
MediaType: s,
57+
MediaTypeType: parts[0],
58+
MediaTypeSubType: parts[1],
59+
EncodesAsText: true,
60+
}
4361
for _, t := range n.streamTypes {
4462
if t == s {
4563
info.StreamSerializer = &runtime.StreamSerializerInfo{

staging/src/k8s.io/apiserver/pkg/endpoints/installer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag
512512
//
513513
// test/integration/auth_test.go is currently the most comprehensive status code test
514514

515+
for _, s := range a.group.Serializer.SupportedMediaTypes() {
516+
if len(s.MediaTypeSubType) == 0 || len(s.MediaTypeType) == 0 {
517+
return nil, fmt.Errorf("all serializers in the group Serializer must have MediaTypeType and MediaTypeSubType set: %s", s.MediaType)
518+
}
519+
}
515520
mediaTypes, streamMediaTypes := negotiation.MediaTypesForSerializer(a.group.Serializer)
516521
allMediaTypes := append(mediaTypes, streamMediaTypes...)
517522
ws.Produces(allMediaTypes...)

staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package storage
1818

1919
import (
20+
"mime"
2021
"reflect"
22+
"strings"
2123
"testing"
2224

2325
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -60,7 +62,24 @@ type fakeNegotiater struct {
6062
func (n *fakeNegotiater) SupportedMediaTypes() []runtime.SerializerInfo {
6163
var out []runtime.SerializerInfo
6264
for _, s := range n.types {
63-
info := runtime.SerializerInfo{Serializer: n.serializer, MediaType: s, EncodesAsText: true}
65+
mediaType, _, err := mime.ParseMediaType(s)
66+
if err != nil {
67+
panic(err)
68+
}
69+
parts := strings.SplitN(mediaType, "/", 2)
70+
if len(parts) == 1 {
71+
// this is an error on the server side
72+
parts = append(parts, "")
73+
}
74+
75+
info := runtime.SerializerInfo{
76+
Serializer: n.serializer,
77+
MediaType: s,
78+
MediaTypeType: parts[0],
79+
MediaTypeSubType: parts[1],
80+
EncodesAsText: true,
81+
}
82+
6483
for _, t := range n.streamTypes {
6584
if t == s {
6685
info.StreamSerializer = &runtime.StreamSerializerInfo{

staging/src/k8s.io/client-go/dynamic/scheme.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func init() {
4343

4444
var watchJsonSerializerInfo = runtime.SerializerInfo{
4545
MediaType: "application/json",
46+
MediaTypeType: "application",
47+
MediaTypeSubType: "json",
4648
EncodesAsText: true,
4749
Serializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, false),
4850
PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, watchScheme, watchScheme, true),
@@ -77,6 +79,8 @@ func (s basicNegotiatedSerializer) SupportedMediaTypes() []runtime.SerializerInf
7779
return []runtime.SerializerInfo{
7880
{
7981
MediaType: "application/json",
82+
MediaTypeType: "application",
83+
MediaTypeSubType: "json",
8084
EncodesAsText: true,
8185
Serializer: json.NewSerializer(json.DefaultMetaFactory, basicScheme, basicScheme, false),
8286
PrettySerializer: json.NewSerializer(json.DefaultMetaFactory, basicScheme, basicScheme, true),

0 commit comments

Comments
 (0)