Merge pull request #76175 from stealthybox/refactor-mediatype-loops
Refactor loops over `SupportedMediaTypes()` where mediaType is used to match a single SerializerInfo{}
Kubernetes-commit: 84b561033eea9a4dac5b04df4f387efa7a7e9940
This commit is contained in:
commit
5a10509a60
|
|
@ -452,7 +452,7 @@
|
|||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/client-go",
|
||||
"Rev": "95d8a92c4875"
|
||||
"Rev": "89e9ab5330ad"
|
||||
},
|
||||
{
|
||||
"ImportPath": "k8s.io/component-base",
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -66,7 +66,7 @@ require (
|
|||
gotest.tools v2.2.0+incompatible // indirect
|
||||
k8s.io/api v0.0.0-20190405092450-f54612431266
|
||||
k8s.io/apimachinery v0.0.0-20190405092352-bd3bd0cdce9a
|
||||
k8s.io/client-go v0.0.0-20190405092624-95d8a92c4875
|
||||
k8s.io/client-go v0.0.0-20190405092625-89e9ab5330ad
|
||||
k8s.io/component-base v0.0.0-20190405092941-79a63e28c077
|
||||
k8s.io/klog v0.0.0-20190306015804-8e90cee79f82
|
||||
k8s.io/kube-openapi v0.0.0-20190228160746-b3a7cee44a30
|
||||
|
|
@ -330,7 +330,7 @@ replace (
|
|||
gotest.tools => gotest.tools v2.2.0+incompatible
|
||||
k8s.io/api => k8s.io/api v0.0.0-20190405092450-f54612431266
|
||||
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20190405092352-bd3bd0cdce9a
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20190405092624-95d8a92c4875
|
||||
k8s.io/client-go => k8s.io/client-go v0.0.0-20190405092625-89e9ab5330ad
|
||||
k8s.io/component-base => k8s.io/component-base v0.0.0-20190405092941-79a63e28c077
|
||||
k8s.io/gengo => k8s.io/gengo v0.0.0-20181106084056-51747d6e00da
|
||||
k8s.io/heapster => k8s.io/heapster v1.2.0-beta.1
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -197,7 +197,7 @@ gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
|||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
k8s.io/api v0.0.0-20190405092450-f54612431266/go.mod h1:4yeJ/+SLLj5auGPBMr34HAUxAJspsaG1o8vMqVg46ig=
|
||||
k8s.io/apimachinery v0.0.0-20190405092352-bd3bd0cdce9a/go.mod h1:hePg3H6BsTpYcS0Q8mso5Ms770hzaiB9f58j4CZz5Bg=
|
||||
k8s.io/client-go v0.0.0-20190405092624-95d8a92c4875/go.mod h1:PCZxKDhcYCtRYwheBzaWB0zzWz6AaLNqBq4BS+5HLqQ=
|
||||
k8s.io/client-go v0.0.0-20190405092625-89e9ab5330ad/go.mod h1:PCZxKDhcYCtRYwheBzaWB0zzWz6AaLNqBq4BS+5HLqQ=
|
||||
k8s.io/component-base v0.0.0-20190405092941-79a63e28c077/go.mod h1:YSec6hGXgma+689ojmMSm6gsaC8cfuMJRnhky9OEhU0=
|
||||
k8s.io/klog v0.0.0-20190306015804-8e90cee79f82 h1:SHucoAy7lRb+w5oC/hbXyZg+zX+Wftn6hD4tGzHCVqA=
|
||||
k8s.io/klog v0.0.0-20190306015804-8e90cee79f82/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
|
||||
|
|
|
|||
|
|
@ -197,10 +197,13 @@ func LogResponseObject(ae *auditinternal.Event, obj runtime.Object, gv schema.Gr
|
|||
}
|
||||
|
||||
func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime.NegotiatedSerializer) (*runtime.Unknown, error) {
|
||||
supported := serializer.SupportedMediaTypes()
|
||||
for i := range supported {
|
||||
if supported[i].MediaType == "application/json" {
|
||||
enc := serializer.EncoderForVersion(supported[i].Serializer, gv)
|
||||
const mediaType = runtime.ContentTypeJSON
|
||||
info, ok := runtime.SerializerInfoForMediaType(serializer.SupportedMediaTypes(), mediaType)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
|
||||
}
|
||||
|
||||
enc := serializer.EncoderForVersion(info.Serializer, gv)
|
||||
var buf bytes.Buffer
|
||||
if err := enc.Encode(obj, &buf); err != nil {
|
||||
return nil, fmt.Errorf("encoding failed: %v", err)
|
||||
|
|
@ -211,9 +214,6 @@ func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime
|
|||
ContentType: runtime.ContentTypeJSON,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no json encoder found")
|
||||
}
|
||||
|
||||
// LogAnnotation fills in the Annotations according to the key value pair.
|
||||
func LogAnnotation(ae *auditinternal.Event, key, value string) {
|
||||
|
|
|
|||
|
|
@ -79,10 +79,7 @@ func NegotiateInputSerializerForMediaType(mediaType string, streaming bool, ns r
|
|||
mediaType = mediaTypes[0].MediaType
|
||||
}
|
||||
if mediaType, _, err := mime.ParseMediaType(mediaType); err == nil {
|
||||
for _, info := range mediaTypes {
|
||||
if info.MediaType != mediaType {
|
||||
continue
|
||||
}
|
||||
if info, ok := runtime.SerializerInfoForMediaType(mediaTypes, mediaType); ok {
|
||||
return info, nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,17 +94,12 @@ func serveWatch(watcher watch.Interface, scope *RequestScope, mediaTypeOptions n
|
|||
var embeddedEncoder runtime.Encoder
|
||||
contentKind, contentSerializer, transform := targetEncodingForTransform(scope, mediaTypeOptions, req)
|
||||
if transform {
|
||||
var embedded runtime.Serializer
|
||||
for _, supported := range contentSerializer.SupportedMediaTypes() {
|
||||
if supported.MediaType == serializer.MediaType {
|
||||
embedded = supported.Serializer
|
||||
}
|
||||
}
|
||||
if embedded == nil {
|
||||
info, ok := runtime.SerializerInfoForMediaType(contentSerializer.SupportedMediaTypes(), serializer.MediaType)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("no encoder for %q exists in the requested target %#v", serializer.MediaType, contentSerializer), w, req)
|
||||
return
|
||||
}
|
||||
embeddedEncoder = contentSerializer.EncoderForVersion(embedded, contentKind.GroupVersion())
|
||||
embeddedEncoder = contentSerializer.EncoderForVersion(info.Serializer, contentKind.GroupVersion())
|
||||
} else {
|
||||
embeddedEncoder = scope.Serializer.EncoderForVersion(serializer.Serializer, contentKind.GroupVersion())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue