mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Simplify RESTHander getter and remove specialized serializers
This commit is contained in:
parent
96a26d93f5
commit
f9f680a937
@ -367,40 +367,42 @@ 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
|
||||
// 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) {
|
||||
if stream, ok := object.(rest.ResourceStreamer); ok {
|
||||
out, flush, contentType, err := stream.InputStream(gv.String(), req.Header.Get("Accept"))
|
||||
if err != nil {
|
||||
errorNegotiated(err, s, gv, w, req)
|
||||
return
|
||||
}
|
||||
if out == nil {
|
||||
// No output provided - return StatusNoContent
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if wsstream.IsWebSocketRequest(req) {
|
||||
r := wsstream.NewReader(out, true, wsstream.NewDefaultReaderProtocols())
|
||||
if err := r.Copy(w, req); err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("error encountered while streaming results via websocket: %v", err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(contentType) == 0 {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(statusCode)
|
||||
writer := w.(io.Writer)
|
||||
if flush {
|
||||
writer = flushwriter.Wrap(w)
|
||||
}
|
||||
io.Copy(writer, out)
|
||||
stream, ok := object.(rest.ResourceStreamer)
|
||||
if !ok {
|
||||
writeNegotiated(s, gv, w, req, statusCode, object)
|
||||
return
|
||||
}
|
||||
writeNegotiated(s, gv, w, req, statusCode, object)
|
||||
|
||||
out, flush, contentType, err := stream.InputStream(gv.String(), req.Header.Get("Accept"))
|
||||
if err != nil {
|
||||
errorNegotiated(err, s, gv, w, req)
|
||||
return
|
||||
}
|
||||
if out == nil {
|
||||
// No output provided - return StatusNoContent
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
if wsstream.IsWebSocketRequest(req) {
|
||||
r := wsstream.NewReader(out, true, wsstream.NewDefaultReaderProtocols())
|
||||
if err := r.Copy(w, req); err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("error encountered while streaming results via websocket: %v", err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(contentType) == 0 {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(statusCode)
|
||||
writer := w.(io.Writer)
|
||||
if flush {
|
||||
writer = flushwriter.Wrap(w)
|
||||
}
|
||||
io.Copy(writer, out)
|
||||
}
|
||||
|
||||
// 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
|
||||
// the object itself.
|
||||
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
|
||||
StreamContentType string
|
||||
|
||||
Framer runtime.Framer
|
||||
StreamSerializer runtime.Serializer
|
||||
StreamSpecialize func(map[string]string) (runtime.Serializer, bool)
|
||||
}
|
||||
|
||||
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 _, accepted := range s.AcceptContentTypes {
|
||||
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
|
||||
if v, ok := params["pretty"]; ok && v == "1" && s.PrettySerializer != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
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{
|
||||
SerializerInfo: runtime.SerializerInfo{
|
||||
Serializer: s.StreamSerializer,
|
||||
|
Loading…
Reference in New Issue
Block a user