Merge pull request #83583 from wojtek-t/improve_negotiate_media_type
Improve negotiate media type Kubernetes-commit: 09b4787126240489081502e553c466e85a3bd51f
This commit is contained in:
commit
b803d9d0d3
|
@ -316,7 +316,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "github.com/munnerz/goautoneg",
|
||||
"Rev": "a547fc61f48d"
|
||||
"Rev": "a7dc8b61c822"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mwitkow/go-conntrack",
|
||||
|
@ -516,7 +516,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/api",
|
||||
"Rev": "910e671eb668"
|
||||
"Rev": "fbf594f18f80"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/apimachinery",
|
||||
|
@ -524,7 +524,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/client-go",
|
||||
"Rev": "e318746e79ea"
|
||||
"Rev": "172b42569cca"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/component-base",
|
||||
|
|
10
go.mod
10
go.mod
|
@ -26,7 +26,7 @@ require (
|
|||
github.com/grpc-ecosystem/grpc-gateway v1.3.0 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.1
|
||||
github.com/jonboulle/clockwork v0.1.0 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
|
||||
github.com/pborman/uuid v1.2.0
|
||||
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021 // indirect
|
||||
github.com/prometheus/client_golang v0.9.4
|
||||
|
@ -48,9 +48,9 @@ require (
|
|||
gopkg.in/square/go-jose.v2 v2.2.2
|
||||
gopkg.in/yaml.v2 v2.2.4
|
||||
gotest.tools v2.2.0+incompatible // indirect
|
||||
k8s.io/api v0.0.0-20191009075622-910e671eb668
|
||||
k8s.io/api v0.0.0-20191010143144-fbf594f18f80
|
||||
k8s.io/apimachinery v0.0.0-20191006235458-f9f2f3f8ab02
|
||||
k8s.io/client-go v0.0.0-20191009235830-e318746e79ea
|
||||
k8s.io/client-go v0.0.0-20191010200049-172b42569cca
|
||||
k8s.io/component-base v0.0.0-20191010035941-20c2ac7c8c1b
|
||||
k8s.io/klog v1.0.0
|
||||
k8s.io/kube-openapi v0.0.0-20190816220812-743ec37842bf
|
||||
|
@ -67,8 +67,8 @@ replace (
|
|||
golang.org/x/sys => golang.org/x/sys v0.0.0-20190209173611-3b5209105503
|
||||
golang.org/x/text => golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db
|
||||
golang.org/x/time => golang.org/x/time v0.0.0-20161028155119-f51c12702a4d
|
||||
k8s.io/api => k8s.io/api v0.0.0-20191009075622-910e671eb668
|
||||
k8s.io/api => k8s.io/api v0.0.0-20191010143144-fbf594f18f80
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20191006235458-f9f2f3f8ab02
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20191009235830-e318746e79ea
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20191010200049-172b42569cca
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20191010035941-20c2ac7c8c1b
|
||||
)
|
||||
|
|
6
go.sum
6
go.sum
|
@ -158,6 +158,8 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
|
|||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d h1:7PxY7LVfSZm7PEeBTyK1rj1gABdCO2mbri6GKO1cMDs=
|
||||
github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
|
||||
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
|
@ -292,9 +294,9 @@ gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81
|
|||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
k8s.io/api v0.0.0-20191009075622-910e671eb668/go.mod h1:X3kixOyiuC4u4LU6y2BxLg5tsvw+hrMhstfga7LZ4Gw=
|
||||
k8s.io/api v0.0.0-20191010143144-fbf594f18f80/go.mod h1:X3kixOyiuC4u4LU6y2BxLg5tsvw+hrMhstfga7LZ4Gw=
|
||||
k8s.io/apimachinery v0.0.0-20191006235458-f9f2f3f8ab02/go.mod h1:92mWDd8Ji2sw2157KIgino5wCxffA8KSvhW2oY4ypdw=
|
||||
k8s.io/client-go v0.0.0-20191009235830-e318746e79ea/go.mod h1:HOfNoGSBsyr/Qq0+N4lHymVHWb0c0maW9A/0VMUKacI=
|
||||
k8s.io/client-go v0.0.0-20191010200049-172b42569cca/go.mod h1:MgiJwA3vA7iWol68PAbcbkmehvC+G79S1Zwr7IZTVyE=
|
||||
k8s.io/component-base v0.0.0-20191010035941-20c2ac7c8c1b/go.mod h1:+3nUtZ+7B+VJcEJ/Mo+ZXd1y8909WPbEu3Ycz0ACgD4=
|
||||
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
|
||||
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
|
||||
|
|
|
@ -234,13 +234,6 @@ func acceptMediaTypeOptions(params map[string]string, accepts *runtime.Serialize
|
|||
return options, true
|
||||
}
|
||||
|
||||
type candidateMediaType struct {
|
||||
accepted *runtime.SerializerInfo
|
||||
clauses goautoneg.Accept
|
||||
}
|
||||
|
||||
type candidateMediaTypeSlice []candidateMediaType
|
||||
|
||||
// NegotiateMediaTypeOptions returns the most appropriate content type given the accept header and
|
||||
// a list of alternatives along with the accepted media type parameters.
|
||||
func NegotiateMediaTypeOptions(header string, accepted []runtime.SerializerInfo, endpoint EndpointRestrictions) (MediaTypeOptions, bool) {
|
||||
|
@ -250,25 +243,21 @@ func NegotiateMediaTypeOptions(header string, accepted []runtime.SerializerInfo,
|
|||
}, true
|
||||
}
|
||||
|
||||
var candidates candidateMediaTypeSlice
|
||||
clauses := goautoneg.ParseAccept(header)
|
||||
for _, clause := range clauses {
|
||||
for i := range clauses {
|
||||
clause := &clauses[i]
|
||||
for i := range accepted {
|
||||
accepts := &accepted[i]
|
||||
switch {
|
||||
case clause.Type == accepts.MediaTypeType && clause.SubType == accepts.MediaTypeSubType,
|
||||
clause.Type == accepts.MediaTypeType && clause.SubType == "*",
|
||||
clause.Type == "*" && clause.SubType == "*":
|
||||
candidates = append(candidates, candidateMediaType{accepted: accepts, clauses: clause})
|
||||
if retVal, ret := acceptMediaTypeOptions(clause.Params, accepts, endpoint); ret {
|
||||
return retVal, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range candidates {
|
||||
if retVal, ret := acceptMediaTypeOptions(v.clauses.Params, v.accepted, endpoint); ret {
|
||||
return retVal, true
|
||||
}
|
||||
}
|
||||
|
||||
return MediaTypeOptions{}, false
|
||||
}
|
||||
|
|
|
@ -280,3 +280,30 @@ func TestNegotiate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fakeSerializerInfoSlice() []runtime.SerializerInfo {
|
||||
result := make([]runtime.SerializerInfo, 2)
|
||||
result[0] = runtime.SerializerInfo{
|
||||
MediaType: "application/json",
|
||||
MediaTypeType: "application",
|
||||
MediaTypeSubType: "json",
|
||||
}
|
||||
result[1] = runtime.SerializerInfo{
|
||||
MediaType: "application/vnd.kubernetes.protobuf",
|
||||
MediaTypeType: "application",
|
||||
MediaTypeSubType: "vnd.kubernetes.protobuf",
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func BenchmarkNegotiateMediaTypeOptions(b *testing.B) {
|
||||
accepted := fakeSerializerInfoSlice()
|
||||
header := "application/vnd.kubernetes.protobuf,*/*"
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
options, _ := NegotiateMediaTypeOptions(header, accepted, DefaultEndpointRestrictions)
|
||||
if options.Accepted != accepted[1] {
|
||||
b.Errorf("Unexpected result")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue