return a Status formatted JSON response

This commit is contained in:
Haowei Cai 2020-11-04 22:29:52 -08:00
parent b5b93004b5
commit 8a1d8f7fd5
3 changed files with 6 additions and 10 deletions

View File

@ -21,6 +21,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
"k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/endpoints/request"
@ -35,7 +37,7 @@ import (
// 2. read requests, // 2. read requests,
// 3. write requests to the storageversion API, // 3. write requests to the storageversion API,
// 4. resources whose StorageVersion is not pending update, including non-persisted resources. // 4. resources whose StorageVersion is not pending update, including non-persisted resources.
func WithStorageVersionPrecondition(handler http.Handler, svm storageversion.Manager) http.Handler { func WithStorageVersionPrecondition(handler http.Handler, svm storageversion.Manager, s runtime.NegotiatedSerializer) http.Handler {
if svm == nil { if svm == nil {
// TODO(roycaihw): switch to warning after the feature graduate to beta/GA // TODO(roycaihw): switch to warning after the feature graduate to beta/GA
klog.V(2).Infof("Storage Version barrier is disabled") klog.V(2).Infof("Storage Version barrier is disabled")
@ -75,6 +77,7 @@ func WithStorageVersionPrecondition(handler http.Handler, svm storageversion.Man
return return
} }
responsewriters.ServiceUnavailabeError(w, req, errors.New(fmt.Sprintf("wait for storage version registration to complete for resource: %v, last seen error: %v", gr, svm.LastUpdateError(gr)))) gv := schema.GroupVersion{requestInfo.APIGroup, requestInfo.APIVersion}
responsewriters.ErrorNegotiated(apierrors.NewServiceUnavailable(fmt.Sprintf("wait for storage version registration to complete for resource: %v, last seen error: %v", gr, svm.LastUpdateError(gr))), s, gv, w, req)
}) })
} }

View File

@ -76,10 +76,3 @@ func InternalError(w http.ResponseWriter, req *http.Request, err error) {
http.StatusInternalServerError) http.StatusInternalServerError)
utilruntime.HandleError(err) utilruntime.HandleError(err)
} }
// ServiceUnavailabeError renders a simple internal error
func ServiceUnavailabeError(w http.ResponseWriter, req *http.Request, err error) {
http.Error(w, sanitizer.Replace(fmt.Sprintf("Service Unavailable: %q: %v", req.RequestURI, err)),
http.StatusServiceUnavailable)
utilruntime.HandleError(err)
}

View File

@ -712,7 +712,7 @@ func (c completedConfig) New(name string, delegationTarget DelegationTarget) (*G
func BuildHandlerChainWithStorageVersionPrecondition(apiHandler http.Handler, c *Config) http.Handler { func BuildHandlerChainWithStorageVersionPrecondition(apiHandler http.Handler, c *Config) http.Handler {
// WithStorageVersionPrecondition needs the WithRequestInfo to run first // WithStorageVersionPrecondition needs the WithRequestInfo to run first
handler := genericapifilters.WithStorageVersionPrecondition(apiHandler, c.StorageVersionManager) handler := genericapifilters.WithStorageVersionPrecondition(apiHandler, c.StorageVersionManager, c.Serializer)
return DefaultBuildHandlerChain(handler, c) return DefaultBuildHandlerChain(handler, c)
} }