mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Simplify RESTHander getter and remove specialized serializers
This commit is contained in:
parent
96a26d93f5
commit
f9f680a937
@ -367,7 +367,12 @@ func SupportedResourcesHandler(s runtime.NegotiatedSerializer, groupVersion unve
|
|||||||
// directly to the response body. If content type is returned it is used, otherwise the content type will
|
// directly to the response body. If content type is returned it is used, otherwise the content type will
|
||||||
// be "application/octet-stream". All other objects are sent to standard JSON serialization.
|
// be "application/octet-stream". All other objects are sent to standard JSON serialization.
|
||||||
func write(statusCode int, gv unversioned.GroupVersion, s runtime.NegotiatedSerializer, object runtime.Object, w http.ResponseWriter, req *http.Request) {
|
func write(statusCode int, gv unversioned.GroupVersion, s runtime.NegotiatedSerializer, object runtime.Object, w http.ResponseWriter, req *http.Request) {
|
||||||
if stream, ok := object.(rest.ResourceStreamer); ok {
|
stream, ok := object.(rest.ResourceStreamer)
|
||||||
|
if !ok {
|
||||||
|
writeNegotiated(s, gv, w, req, statusCode, object)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
out, flush, contentType, err := stream.InputStream(gv.String(), req.Header.Get("Accept"))
|
out, flush, contentType, err := stream.InputStream(gv.String(), req.Header.Get("Accept"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorNegotiated(err, s, gv, w, req)
|
errorNegotiated(err, s, gv, w, req)
|
||||||
@ -398,9 +403,6 @@ func write(statusCode int, gv unversioned.GroupVersion, s runtime.NegotiatedSeri
|
|||||||
writer = flushwriter.Wrap(w)
|
writer = flushwriter.Wrap(w)
|
||||||
}
|
}
|
||||||
io.Copy(writer, out)
|
io.Copy(writer, out)
|
||||||
return
|
|
||||||
}
|
|
||||||
writeNegotiated(s, gv, w, req, statusCode, object)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeNegotiated renders an object in the content type negotiated by the client
|
// writeNegotiated renders an object in the content type negotiated by the client
|
||||||
|
@ -43,18 +43,12 @@ type serializerType struct {
|
|||||||
// be expected to pass into or a gvk to Decode, since no type information will be available on
|
// be expected to pass into or a gvk to Decode, since no type information will be available on
|
||||||
// the object itself.
|
// the object itself.
|
||||||
RawSerializer runtime.Serializer
|
RawSerializer runtime.Serializer
|
||||||
// Specialize gives the type the opportunity to return a different serializer implementation if
|
|
||||||
// the content type contains alternate operations. Here it is used to implement "pretty" as an
|
|
||||||
// option to application/json, but could also be used to allow serializers to perform type
|
|
||||||
// defaulting or alter output.
|
|
||||||
Specialize func(map[string]string) (runtime.Serializer, bool)
|
|
||||||
|
|
||||||
AcceptStreamContentTypes []string
|
AcceptStreamContentTypes []string
|
||||||
StreamContentType string
|
StreamContentType string
|
||||||
|
|
||||||
Framer runtime.Framer
|
Framer runtime.Framer
|
||||||
StreamSerializer runtime.Serializer
|
StreamSerializer runtime.Serializer
|
||||||
StreamSpecialize func(map[string]string) (runtime.Serializer, bool)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []serializerType {
|
func newSerializersForScheme(scheme *runtime.Scheme, mf json.MetaFactory) []serializerType {
|
||||||
@ -254,13 +248,6 @@ func (f CodecFactory) SerializerForMediaType(mediaType string, params map[string
|
|||||||
for _, s := range f.serializers {
|
for _, s := range f.serializers {
|
||||||
for _, accepted := range s.AcceptContentTypes {
|
for _, accepted := range s.AcceptContentTypes {
|
||||||
if accepted == mediaType {
|
if accepted == mediaType {
|
||||||
// specialization abstracts variants to the content type
|
|
||||||
if s.Specialize != nil && len(params) > 0 {
|
|
||||||
serializer, ok := s.Specialize(params)
|
|
||||||
// TODO: return formatted mediaType+params
|
|
||||||
return runtime.SerializerInfo{Serializer: serializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// legacy support for ?pretty=1 continues, but this is more formally defined
|
// legacy support for ?pretty=1 continues, but this is more formally defined
|
||||||
if v, ok := params["pretty"]; ok && v == "1" && s.PrettySerializer != nil {
|
if v, ok := params["pretty"]; ok && v == "1" && s.PrettySerializer != nil {
|
||||||
return runtime.SerializerInfo{Serializer: s.PrettySerializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, true
|
return runtime.SerializerInfo{Serializer: s.PrettySerializer, MediaType: s.ContentType, EncodesAsText: s.EncodesAsText}, true
|
||||||
@ -286,20 +273,6 @@ func (f CodecFactory) StreamingSerializerForMediaType(mediaType string, params m
|
|||||||
panic("no serializer defined for internal content type")
|
panic("no serializer defined for internal content type")
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.StreamSpecialize != nil && len(params) > 0 {
|
|
||||||
serializer, ok := s.StreamSpecialize(params)
|
|
||||||
// TODO: return formatted mediaType+params
|
|
||||||
return runtime.StreamSerializerInfo{
|
|
||||||
SerializerInfo: runtime.SerializerInfo{
|
|
||||||
Serializer: serializer,
|
|
||||||
MediaType: s.StreamContentType,
|
|
||||||
EncodesAsText: s.EncodesAsText,
|
|
||||||
},
|
|
||||||
Framer: s.Framer,
|
|
||||||
Embedded: nested,
|
|
||||||
}, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
return runtime.StreamSerializerInfo{
|
return runtime.StreamSerializerInfo{
|
||||||
SerializerInfo: runtime.SerializerInfo{
|
SerializerInfo: runtime.SerializerInfo{
|
||||||
Serializer: s.StreamSerializer,
|
Serializer: s.StreamSerializer,
|
||||||
|
Loading…
Reference in New Issue
Block a user