mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
Merge pull request #6881 from nikhiljindal/errHandle
Registering a serviceErrorHandler with go-restful to always return JSON responses
This commit is contained in:
commit
a927c239fe
@ -250,7 +250,7 @@ func NewTimeoutError(message string, retryAfterSeconds int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewGenericServerResponse returns a new error for server responses that are not in a recognizable form.
|
// NewGenericServerResponse returns a new error for server responses that are not in a recognizable form.
|
||||||
func NewGenericServerResponse(code int, verb, kind, name, serverMessage string, retryAfterSeconds int) error {
|
func NewGenericServerResponse(code int, verb, kind, name, serverMessage string, retryAfterSeconds int, isUnexpectedResponse bool) error {
|
||||||
reason := api.StatusReasonUnknown
|
reason := api.StatusReasonUnknown
|
||||||
message := fmt.Sprintf("the server responded with the status code %d but did not return more information", code)
|
message := fmt.Sprintf("the server responded with the status code %d but did not return more information", code)
|
||||||
switch code {
|
switch code {
|
||||||
@ -297,6 +297,17 @@ func NewGenericServerResponse(code int, verb, kind, name, serverMessage string,
|
|||||||
case len(kind) > 0:
|
case len(kind) > 0:
|
||||||
message = fmt.Sprintf("%s (%s %s)", message, strings.ToLower(verb), kind)
|
message = fmt.Sprintf("%s (%s %s)", message, strings.ToLower(verb), kind)
|
||||||
}
|
}
|
||||||
|
var causes []api.StatusCause
|
||||||
|
if isUnexpectedResponse {
|
||||||
|
causes = []api.StatusCause{
|
||||||
|
{
|
||||||
|
Type: api.CauseTypeUnexpectedServerResponse,
|
||||||
|
Message: serverMessage,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
causes = nil
|
||||||
|
}
|
||||||
return &StatusError{api.Status{
|
return &StatusError{api.Status{
|
||||||
Status: api.StatusFailure,
|
Status: api.StatusFailure,
|
||||||
Code: code,
|
Code: code,
|
||||||
@ -305,13 +316,7 @@ func NewGenericServerResponse(code int, verb, kind, name, serverMessage string,
|
|||||||
Kind: kind,
|
Kind: kind,
|
||||||
ID: name,
|
ID: name,
|
||||||
|
|
||||||
Causes: []api.StatusCause{
|
Causes: causes,
|
||||||
{
|
|
||||||
Type: api.CauseTypeUnexpectedServerResponse,
|
|
||||||
Message: serverMessage,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
RetryAfterSeconds: retryAfterSeconds,
|
RetryAfterSeconds: retryAfterSeconds,
|
||||||
},
|
},
|
||||||
Message: message,
|
Message: message,
|
||||||
|
@ -30,6 +30,8 @@ import (
|
|||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
apierrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/healthz"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/healthz"
|
||||||
@ -168,6 +170,15 @@ func InstallLogsSupport(mux Mux) {
|
|||||||
mux.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log/"))))
|
mux.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log/"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InstallServiceErrorHandler(container *restful.Container) {
|
||||||
|
container.ServiceErrorHandler(serviceErrorHandler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func serviceErrorHandler(serviceErr restful.ServiceError, response *restful.Response) {
|
||||||
|
// TODO: Update go-restful to return the request as well, so that we can use the appropriate codec rather than using the latest one.
|
||||||
|
errorJSON(apierrors.NewGenericServerResponse(serviceErr.Code, "", "", "", "", 0, false), latest.Codec, response.ResponseWriter)
|
||||||
|
}
|
||||||
|
|
||||||
// Adds a service to return the supported api versions.
|
// Adds a service to return the supported api versions.
|
||||||
func AddApiWebService(container *restful.Container, apiPrefix string, versions []string) {
|
func AddApiWebService(container *restful.Container, apiPrefix string, versions []string) {
|
||||||
// TODO: InstallREST should register each version automatically
|
// TODO: InstallREST should register each version automatically
|
||||||
@ -246,6 +257,7 @@ func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w htt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
glog.Infof("Writting status code: %d", statusCode)
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
w.Write(formatted.Bytes())
|
w.Write(formatted.Bytes())
|
||||||
}
|
}
|
||||||
|
@ -792,7 +792,7 @@ func (r *Request) transformUnstructuredResponseError(resp *http.Response, req *h
|
|||||||
message = strings.TrimSpace(string(body))
|
message = strings.TrimSpace(string(body))
|
||||||
}
|
}
|
||||||
retryAfter, _ := retryAfterSeconds(resp)
|
retryAfter, _ := retryAfterSeconds(resp)
|
||||||
return errors.NewGenericServerResponse(resp.StatusCode, req.Method, r.resource, r.resourceName, message, retryAfter)
|
return errors.NewGenericServerResponse(resp.StatusCode, req.Method, r.resource, r.resourceName, message, retryAfter, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// isTextResponse returns true if the response appears to be a textual media type.
|
// isTextResponse returns true if the response appears to be a textual media type.
|
||||||
|
@ -431,6 +431,7 @@ func (m *Master) init(c *Config) {
|
|||||||
|
|
||||||
apiserver.InstallSupport(m.muxHelper, m.rootWebService)
|
apiserver.InstallSupport(m.muxHelper, m.rootWebService)
|
||||||
apiserver.AddApiWebService(m.handlerContainer, c.APIPrefix, apiVersions)
|
apiserver.AddApiWebService(m.handlerContainer, c.APIPrefix, apiVersions)
|
||||||
|
apiserver.InstallServiceErrorHandler(m.handlerContainer)
|
||||||
|
|
||||||
// Register root handler.
|
// Register root handler.
|
||||||
// We do not register this using restful Webservice since we do not want to surface this in api docs.
|
// We do not register this using restful Webservice since we do not want to surface this in api docs.
|
||||||
|
Loading…
Reference in New Issue
Block a user