mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
negotiated refactor
Co-authored-by: Jeffrey Ying <jeffrey.ying86@live.com>
This commit is contained in:
parent
d62cc3dc6d
commit
76f056867a
@ -3821,7 +3821,7 @@ func (obj *UnregisteredAPIObject) DeepCopyObject() runtime.Object {
|
|||||||
|
|
||||||
func TestWriteJSONDecodeError(t *testing.T) {
|
func TestWriteJSONDecodeError(t *testing.T) {
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
responsewriters.WriteObjectNegotiated(codecs, negotiation.DefaultEndpointRestrictions, newGroupVersion, w, req, http.StatusOK, &UnregisteredAPIObject{"Undecodable"})
|
responsewriters.WriteObjectNegotiated(codecs, negotiation.DefaultEndpointRestrictions, newGroupVersion, w, req, http.StatusOK, &UnregisteredAPIObject{"Undecodable"}, false)
|
||||||
}))
|
}))
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
// Decode error response behavior is dictated by
|
// Decode error response behavior is dictated by
|
||||||
|
@ -69,5 +69,5 @@ func (s *APIGroupHandler) handle(req *restful.Request, resp *restful.Response) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (s *APIGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, &s.group)
|
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, &s.group, false)
|
||||||
}
|
}
|
||||||
|
@ -72,5 +72,5 @@ func (s *legacyRootAPIHandler) handle(req *restful.Request, resp *restful.Respon
|
|||||||
Versions: []string{"v1"},
|
Versions: []string{"v1"},
|
||||||
}
|
}
|
||||||
|
|
||||||
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, apiVersions)
|
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, apiVersions, false)
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ func (s *rootAPIsHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request)
|
|||||||
groups[i].ServerAddressByClientCIDRs = serverCIDR
|
groups[i].ServerAddressByClientCIDRs = serverCIDR
|
||||||
}
|
}
|
||||||
|
|
||||||
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp, req, http.StatusOK, &metav1.APIGroupList{Groups: groups})
|
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, resp, req, http.StatusOK, &metav1.APIGroupList{Groups: groups}, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rootAPIsHandler) restfulHandle(req *restful.Request, resp *restful.Response) {
|
func (s *rootAPIsHandler) restfulHandle(req *restful.Request, resp *restful.Response) {
|
||||||
|
@ -79,5 +79,5 @@ func (s *APIVersionHandler) handle(req *restful.Request, resp *restful.Response)
|
|||||||
|
|
||||||
func (s *APIVersionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (s *APIVersionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK,
|
responsewriters.WriteObjectNegotiated(s.serializer, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK,
|
||||||
&metav1.APIResourceList{GroupVersion: s.groupVersion.String(), APIResources: s.apiResourceLister.ListAPIResources()})
|
&metav1.APIResourceList{GroupVersion: s.groupVersion.String(), APIResources: s.apiResourceLister.ListAPIResources()}, false)
|
||||||
}
|
}
|
||||||
|
@ -146,7 +146,7 @@ func transformResponseObject(ctx context.Context, scope *RequestScope, req *http
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
kind, serializer, _ := targetEncodingForTransform(scope, mediaType, req)
|
kind, serializer, _ := targetEncodingForTransform(scope, mediaType, req)
|
||||||
responsewriters.WriteObjectNegotiated(serializer, scope, kind.GroupVersion(), w, req, statusCode, obj)
|
responsewriters.WriteObjectNegotiated(serializer, scope, kind.GroupVersion(), w, req, statusCode, obj, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// errNotAcceptable indicates Accept negotiation has failed
|
// errNotAcceptable indicates Accept negotiation has failed
|
||||||
|
@ -259,7 +259,7 @@ func (w *deferredResponseWriter) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteObjectNegotiated renders an object in the content type negotiated by the client.
|
// WriteObjectNegotiated renders an object in the content type negotiated by the client.
|
||||||
func WriteObjectNegotiated(s runtime.NegotiatedSerializer, restrictions negotiation.EndpointRestrictions, gv schema.GroupVersion, w http.ResponseWriter, req *http.Request, statusCode int, object runtime.Object) {
|
func WriteObjectNegotiated(s runtime.NegotiatedSerializer, restrictions negotiation.EndpointRestrictions, gv schema.GroupVersion, w http.ResponseWriter, req *http.Request, statusCode int, object runtime.Object, listGVKInContentType bool) {
|
||||||
stream, ok := object.(rest.ResourceStreamer)
|
stream, ok := object.(rest.ResourceStreamer)
|
||||||
if ok {
|
if ok {
|
||||||
requestInfo, _ := request.RequestInfoFrom(req.Context())
|
requestInfo, _ := request.RequestInfoFrom(req.Context())
|
||||||
@ -269,7 +269,7 @@ func WriteObjectNegotiated(s runtime.NegotiatedSerializer, restrictions negotiat
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, serializer, err := negotiation.NegotiateOutputMediaType(req, s, restrictions)
|
mediaType, serializer, err := negotiation.NegotiateOutputMediaType(req, s, restrictions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if original statusCode was not successful we need to return the original error
|
// if original statusCode was not successful we need to return the original error
|
||||||
// we cannot hide it behind negotiation problems
|
// we cannot hide it behind negotiation problems
|
||||||
@ -286,10 +286,30 @@ func WriteObjectNegotiated(s runtime.NegotiatedSerializer, restrictions negotiat
|
|||||||
|
|
||||||
encoder := s.EncoderForVersion(serializer.Serializer, gv)
|
encoder := s.EncoderForVersion(serializer.Serializer, gv)
|
||||||
request.TrackSerializeResponseObjectLatency(req.Context(), func() {
|
request.TrackSerializeResponseObjectLatency(req.Context(), func() {
|
||||||
SerializeObject(serializer.MediaType, encoder, w, req, statusCode, object)
|
if listGVKInContentType {
|
||||||
|
SerializeObject(generateMediaTypeWithGVK(serializer.MediaType, mediaType.Convert), encoder, w, req, statusCode, object)
|
||||||
|
} else {
|
||||||
|
SerializeObject(serializer.MediaType, encoder, w, req, statusCode, object)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateMediaTypeWithGVK(mediaType string, gvk *schema.GroupVersionKind) string {
|
||||||
|
if gvk == nil {
|
||||||
|
return mediaType
|
||||||
|
}
|
||||||
|
if gvk.Group != "" {
|
||||||
|
mediaType += ";g=" + gvk.Group
|
||||||
|
}
|
||||||
|
if gvk.Version != "" {
|
||||||
|
mediaType += ";v=" + gvk.Version
|
||||||
|
}
|
||||||
|
if gvk.Kind != "" {
|
||||||
|
mediaType += ";as=" + gvk.Kind
|
||||||
|
}
|
||||||
|
return mediaType
|
||||||
|
}
|
||||||
|
|
||||||
// ErrorNegotiated renders an error to the response. Returns the HTTP status code of the error.
|
// ErrorNegotiated renders an error to the response. Returns the HTTP status code of the error.
|
||||||
// The context is optional and may be nil.
|
// The context is optional and may be nil.
|
||||||
func ErrorNegotiated(err error, s runtime.NegotiatedSerializer, gv schema.GroupVersion, w http.ResponseWriter, req *http.Request) int {
|
func ErrorNegotiated(err error, s runtime.NegotiatedSerializer, gv schema.GroupVersion, w http.ResponseWriter, req *http.Request) int {
|
||||||
@ -306,7 +326,7 @@ func ErrorNegotiated(err error, s runtime.NegotiatedSerializer, gv schema.GroupV
|
|||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteObjectNegotiated(s, negotiation.DefaultEndpointRestrictions, gv, w, req, code, status)
|
WriteObjectNegotiated(s, negotiation.DefaultEndpointRestrictions, gv, w, req, code, status, false)
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,7 +237,7 @@ type responder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *responder) Object(statusCode int, obj runtime.Object) {
|
func (r *responder) Object(statusCode int, obj runtime.Object) {
|
||||||
responsewriters.WriteObjectNegotiated(r.scope.Serializer, r.scope, r.scope.Kind.GroupVersion(), r.w, r.req, statusCode, obj)
|
responsewriters.WriteObjectNegotiated(r.scope.Serializer, r.scope, r.scope.Kind.GroupVersion(), r.w, r.req, statusCode, obj, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responder) Error(err error) {
|
func (r *responder) Error(err error) {
|
||||||
|
@ -91,7 +91,7 @@ func (r *apisHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
responsewriters.WriteObjectNegotiated(r.codecs, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, discoveryGroupList)
|
responsewriters.WriteObjectNegotiated(r.codecs, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, discoveryGroupList, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertToDiscoveryAPIGroup takes apiservices in a single group and returns a discovery compatible object.
|
// convertToDiscoveryAPIGroup takes apiservices in a single group and returns a discovery compatible object.
|
||||||
@ -162,5 +162,5 @@ func (r *apiGroupHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
responsewriters.WriteObjectNegotiated(r.codecs, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, discoveryGroup)
|
responsewriters.WriteObjectNegotiated(r.codecs, negotiation.DefaultEndpointRestrictions, schema.GroupVersion{}, w, req, http.StatusOK, discoveryGroup, false)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user