2014-07-29 21:35:33 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-07-29 21:35:33 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-12-21 05:15:35 +00:00
|
|
|
"strings"
|
2014-07-29 21:35:33 +00:00
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
2015-12-03 16:09:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/storage"
|
2016-01-15 07:32:10 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/runtime"
|
2014-07-31 18:26:34 +00:00
|
|
|
)
|
2014-08-03 17:07:40 +00:00
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
// statusError is an object that can be converted into an metav1.Status
|
2014-09-03 21:16:00 +00:00
|
|
|
type statusError interface {
|
2016-12-03 18:57:26 +00:00
|
|
|
Status() metav1.Status
|
2014-08-03 17:07:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
// errToAPIStatus converts an error to an metav1.Status object.
|
|
|
|
func errToAPIStatus(err error) *metav1.Status {
|
2014-07-31 18:26:34 +00:00
|
|
|
switch t := err.(type) {
|
2014-09-03 21:16:00 +00:00
|
|
|
case statusError:
|
|
|
|
status := t.Status()
|
2015-02-09 14:47:13 +00:00
|
|
|
if len(status.Status) == 0 {
|
2016-12-03 18:57:26 +00:00
|
|
|
status.Status = metav1.StatusFailure
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
2015-04-22 05:39:00 +00:00
|
|
|
if status.Code == 0 {
|
|
|
|
switch status.Status {
|
2016-12-03 18:57:26 +00:00
|
|
|
case metav1.StatusSuccess:
|
2015-02-09 14:47:13 +00:00
|
|
|
status.Code = http.StatusOK
|
2016-12-03 18:57:26 +00:00
|
|
|
case metav1.StatusFailure:
|
2015-02-09 14:47:13 +00:00
|
|
|
status.Code = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 18:26:34 +00:00
|
|
|
//TODO: check for invalid responses
|
|
|
|
return &status
|
|
|
|
default:
|
|
|
|
status := http.StatusInternalServerError
|
|
|
|
switch {
|
2014-08-20 02:58:24 +00:00
|
|
|
//TODO: replace me with NewConflictErr
|
2015-12-03 16:09:45 +00:00
|
|
|
case storage.IsTestFailed(err):
|
2014-07-31 18:26:34 +00:00
|
|
|
status = http.StatusConflict
|
|
|
|
}
|
2014-09-18 10:44:21 +00:00
|
|
|
// Log errors that were not converted to an error status
|
|
|
|
// by REST storage - these typically indicate programmer
|
|
|
|
// error by not using pkg/api/errors, or unexpected failure
|
|
|
|
// cases.
|
2016-12-03 18:57:26 +00:00
|
|
|
runtime.HandleError(fmt.Errorf("apiserver received an error that is not an metav1.Status: %v", err))
|
|
|
|
return &metav1.Status{
|
|
|
|
Status: metav1.StatusFailure,
|
2015-11-18 18:15:16 +00:00
|
|
|
Code: int32(status),
|
2016-12-03 18:57:26 +00:00
|
|
|
Reason: metav1.StatusReasonUnknown,
|
2014-07-31 18:26:34 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
2014-07-29 22:10:29 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// notFound renders a simple not found error.
|
2014-07-29 21:35:33 +00:00
|
|
|
func notFound(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "Not Found: %#v", req.RequestURI)
|
|
|
|
}
|
2014-08-06 04:53:00 +00:00
|
|
|
|
2016-09-26 15:18:19 +00:00
|
|
|
// internalError renders a simple internal error
|
|
|
|
func internalError(w http.ResponseWriter, req *http.Request, err error) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintf(w, "Internal Server Error: %#v", req.RequestURI)
|
|
|
|
runtime.HandleError(err)
|
|
|
|
}
|
|
|
|
|
2015-10-20 17:34:26 +00:00
|
|
|
// errAPIPrefixNotFound indicates that a RequestInfo resolution failed because the request isn't under
|
2015-09-22 19:43:29 +00:00
|
|
|
// any known API prefixes
|
|
|
|
type errAPIPrefixNotFound struct {
|
|
|
|
SpecifiedPrefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errAPIPrefixNotFound) Error() string {
|
|
|
|
return fmt.Sprintf("no valid API prefix found matching %v", e.SpecifiedPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsAPIPrefixNotFound(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := err.(*errAPIPrefixNotFound)
|
|
|
|
return ok
|
|
|
|
}
|
2015-12-21 05:15:35 +00:00
|
|
|
|
|
|
|
// errNotAcceptable indicates Accept negotiation has failed
|
|
|
|
// TODO: move to api/errors if other code needs to return this
|
|
|
|
type errNotAcceptable struct {
|
|
|
|
accepted []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e errNotAcceptable) Error() string {
|
|
|
|
return fmt.Sprintf("only the following media types are accepted: %v", strings.Join(e.accepted, ", "))
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
func (e errNotAcceptable) Status() metav1.Status {
|
|
|
|
return metav1.Status{
|
|
|
|
Status: metav1.StatusFailure,
|
2015-12-21 05:15:35 +00:00
|
|
|
Code: http.StatusNotAcceptable,
|
2016-12-03 18:57:26 +00:00
|
|
|
Reason: metav1.StatusReason("NotAcceptable"),
|
2015-12-21 05:15:35 +00:00
|
|
|
Message: e.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-16 03:31:52 +00:00
|
|
|
// errUnsupportedMediaType indicates Content-Type is not recognized
|
2015-12-21 05:15:35 +00:00
|
|
|
// TODO: move to api/errors if other code needs to return this
|
|
|
|
type errUnsupportedMediaType struct {
|
|
|
|
accepted []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e errUnsupportedMediaType) Error() string {
|
|
|
|
return fmt.Sprintf("the body of the request was in an unknown format - accepted media types include: %v", strings.Join(e.accepted, ", "))
|
|
|
|
}
|
|
|
|
|
2016-12-03 18:57:26 +00:00
|
|
|
func (e errUnsupportedMediaType) Status() metav1.Status {
|
|
|
|
return metav1.Status{
|
|
|
|
Status: metav1.StatusFailure,
|
2015-12-21 05:15:35 +00:00
|
|
|
Code: http.StatusUnsupportedMediaType,
|
2016-12-03 18:57:26 +00:00
|
|
|
Reason: metav1.StatusReason("UnsupportedMediaType"),
|
2015-12-21 05:15:35 +00:00
|
|
|
Message: e.Error(),
|
|
|
|
}
|
|
|
|
}
|