2014-08-09 21:12:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 (
|
|
|
|
"net/http"
|
2014-09-26 00:20:28 +00:00
|
|
|
"path"
|
2014-08-09 21:12:55 +00:00
|
|
|
"time"
|
|
|
|
|
2015-01-06 16:44:43 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/admission"
|
2014-08-09 21:12:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-12 05:33:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-08-09 21:12:55 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-09-26 00:20:28 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2014-08-09 21:12:55 +00:00
|
|
|
)
|
|
|
|
|
2014-10-24 17:16:02 +00:00
|
|
|
// RESTHandler implements HTTP verbs on a set of RESTful resources identified by name.
|
2014-08-09 21:12:55 +00:00
|
|
|
type RESTHandler struct {
|
2015-01-29 19:14:36 +00:00
|
|
|
storage map[string]RESTStorage
|
|
|
|
codec runtime.Codec
|
|
|
|
canonicalPrefix string
|
|
|
|
selfLinker runtime.SelfLinker
|
|
|
|
ops *Operations
|
|
|
|
admissionControl admission.Interface
|
|
|
|
apiRequestInfoResolver *APIRequestInfoResolver
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP handles requests to all RESTStorage objects.
|
|
|
|
func (h *RESTHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2015-02-10 22:23:02 +00:00
|
|
|
var verb string
|
|
|
|
var apiResource string
|
2015-02-10 01:04:37 +00:00
|
|
|
var httpCode int
|
|
|
|
reqStart := time.Now()
|
2015-02-10 22:23:02 +00:00
|
|
|
defer func() { monitor("rest", verb, apiResource, httpCode, reqStart) }()
|
2015-02-10 01:04:37 +00:00
|
|
|
|
2015-01-29 19:14:36 +00:00
|
|
|
requestInfo, err := h.apiRequestInfoResolver.GetAPIRequestInfo(req)
|
2014-12-09 19:23:21 +00:00
|
|
|
if err != nil {
|
2015-01-19 21:50:00 +00:00
|
|
|
glog.Errorf("Unable to handle request %s %s %v", requestInfo.Namespace, requestInfo.Kind, err)
|
2014-08-09 21:12:55 +00:00
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-10 01:04:37 +00:00
|
|
|
verb = requestInfo.Verb
|
|
|
|
|
2015-01-29 19:14:36 +00:00
|
|
|
storage, ok := h.storage[requestInfo.Resource]
|
2015-01-12 05:33:25 +00:00
|
|
|
if !ok {
|
2014-08-09 21:12:55 +00:00
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-10 01:04:37 +00:00
|
|
|
apiResource = requestInfo.Resource
|
2014-08-09 21:12:55 +00:00
|
|
|
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = h.handleRESTStorage(requestInfo.Parts, req, w, storage, requestInfo.Namespace, requestInfo.Resource)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 00:20:28 +00:00
|
|
|
// Sets the SelfLink field of the object.
|
|
|
|
func (h *RESTHandler) setSelfLink(obj runtime.Object, req *http.Request) error {
|
|
|
|
newURL := *req.URL
|
|
|
|
newURL.Path = path.Join(h.canonicalPrefix, req.URL.Path)
|
|
|
|
newURL.RawQuery = ""
|
|
|
|
newURL.Fragment = ""
|
2014-11-24 18:35:24 +00:00
|
|
|
namespace, err := h.selfLinker.Namespace(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-09 19:23:21 +00:00
|
|
|
|
|
|
|
// we need to add namespace as a query param, if its not in the resource path
|
2014-11-24 18:35:24 +00:00
|
|
|
if len(namespace) > 0 {
|
2014-12-09 19:23:21 +00:00
|
|
|
parts := splitPath(req.URL.Path)
|
|
|
|
if parts[0] != "ns" {
|
|
|
|
query := newURL.Query()
|
|
|
|
query.Set("namespace", namespace)
|
|
|
|
newURL.RawQuery = query.Encode()
|
|
|
|
}
|
2014-11-24 18:35:24 +00:00
|
|
|
}
|
2014-12-09 19:23:21 +00:00
|
|
|
|
2014-11-24 18:35:24 +00:00
|
|
|
err = h.selfLinker.SetSelfLink(obj, newURL.String())
|
2014-10-30 22:04:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !runtime.IsListType(obj) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set self-link of objects in the list.
|
|
|
|
items, err := runtime.ExtractList(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range items {
|
|
|
|
if err := h.setSelfLinkAddName(items[i], req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return runtime.SetList(obj, items)
|
2014-09-26 00:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// Like setSelfLink, but appends the object's name.
|
|
|
|
func (h *RESTHandler) setSelfLinkAddName(obj runtime.Object, req *http.Request) error {
|
|
|
|
name, err := h.selfLinker.Name(obj)
|
2014-09-26 00:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-24 18:35:24 +00:00
|
|
|
namespace, err := h.selfLinker.Namespace(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
newURL := *req.URL
|
2014-10-23 02:54:34 +00:00
|
|
|
newURL.Path = path.Join(h.canonicalPrefix, req.URL.Path, name)
|
2014-09-26 00:20:28 +00:00
|
|
|
newURL.RawQuery = ""
|
|
|
|
newURL.Fragment = ""
|
2014-12-09 19:23:21 +00:00
|
|
|
// we need to add namespace as a query param, if its not in the resource path
|
2014-11-24 18:35:24 +00:00
|
|
|
if len(namespace) > 0 {
|
2014-12-09 19:23:21 +00:00
|
|
|
parts := splitPath(req.URL.Path)
|
|
|
|
if parts[0] != "ns" {
|
|
|
|
query := newURL.Query()
|
|
|
|
query.Set("namespace", namespace)
|
|
|
|
newURL.RawQuery = query.Encode()
|
|
|
|
}
|
2014-11-24 18:35:24 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
return h.selfLinker.SetSelfLink(obj, newURL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// curry adapts either of the self link setting functions into a function appropriate for operation's hook.
|
2014-10-24 17:16:02 +00:00
|
|
|
func curry(f func(runtime.Object, *http.Request) error, req *http.Request) func(RESTResult) {
|
|
|
|
return func(obj RESTResult) {
|
|
|
|
if err := f(obj.Object, req); err != nil {
|
2014-09-26 00:20:28 +00:00
|
|
|
glog.Errorf("unable to set self link for %#v: %v", obj, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 21:12:55 +00:00
|
|
|
// handleRESTStorage is the main dispatcher for a storage object. It switches on the HTTP method, and then
|
|
|
|
// on path length, according to the following table:
|
|
|
|
// Method Path Action
|
|
|
|
// GET /foo list
|
|
|
|
// GET /foo/bar get 'bar'
|
|
|
|
// POST /foo create
|
|
|
|
// PUT /foo/bar update 'bar'
|
|
|
|
// DELETE /foo/bar delete 'bar'
|
2015-02-10 01:04:37 +00:00
|
|
|
// Responds with a 404 if the method/pattern doesn't match one of these entries.
|
2014-08-09 21:12:55 +00:00
|
|
|
// The s accepts several query parameters:
|
2015-01-22 05:20:57 +00:00
|
|
|
// timeout=<duration> Timeout for synchronous requests
|
2014-08-09 21:12:55 +00:00
|
|
|
// labels=<label-selector> Used for filtering list operations
|
2015-02-10 01:04:37 +00:00
|
|
|
// Returns the HTTP status code written to the response.
|
|
|
|
func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage, namespace, kind string) int {
|
2014-12-09 19:23:21 +00:00
|
|
|
ctx := api.WithNamespace(api.NewContext(), namespace)
|
2015-01-22 05:20:57 +00:00
|
|
|
// TODO: Document the timeout query parameter.
|
2014-08-09 21:12:55 +00:00
|
|
|
timeout := parseTimeout(req.URL.Query().Get("timeout"))
|
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
|
|
|
switch len(parts) {
|
|
|
|
case 1:
|
2014-09-16 23:15:40 +00:00
|
|
|
label, err := labels.ParseSelector(req.URL.Query().Get("labels"))
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2014-09-16 23:15:40 +00:00
|
|
|
field, err := labels.ParseSelector(req.URL.Query().Get("fields"))
|
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-09-16 23:15:40 +00:00
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
lister, ok := storage.(RESTLister)
|
|
|
|
if !ok {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(errors.NewMethodNotSupported(kind, "list"), h.codec, w)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
list, err := lister.List(ctx, label, field)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if err := h.setSelfLink(list, req); err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-09-26 00:20:28 +00:00
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
writeJSON(http.StatusOK, h.codec, list, w)
|
|
|
|
case 2:
|
2015-01-12 05:33:25 +00:00
|
|
|
getter, ok := storage.(RESTGetter)
|
|
|
|
if !ok {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(errors.NewMethodNotSupported(kind, "get"), h.codec, w)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
item, err := getter.Get(ctx, parts[1])
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2014-09-26 00:20:28 +00:00
|
|
|
if err := h.setSelfLink(item, req); err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-09-26 00:20:28 +00:00
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
writeJSON(http.StatusOK, h.codec, item, w)
|
|
|
|
default:
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "POST":
|
|
|
|
if len(parts) != 1 {
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
creater, ok := storage.(RESTCreater)
|
|
|
|
if !ok {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(errors.NewMethodNotSupported(kind, "create"), h.codec, w)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-09 21:12:55 +00:00
|
|
|
body, err := readBody(req)
|
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
obj := storage.New()
|
|
|
|
err = h.codec.DecodeInto(body, obj)
|
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
|
|
|
// invoke admission control
|
2015-01-07 19:33:21 +00:00
|
|
|
err = h.admissionControl.Admit(admission.NewAttributesRecord(obj, namespace, parts[0], "CREATE"))
|
2015-01-06 16:44:43 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2015-01-06 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 05:33:25 +00:00
|
|
|
out, err := creater.Create(ctx, obj)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-22 05:20:57 +00:00
|
|
|
op := h.createOperation(out, timeout, curry(h.setSelfLinkAddName, req))
|
2015-02-10 01:04:37 +00:00
|
|
|
return h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
if len(parts) != 2 {
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
deleter, ok := storage.(RESTDeleter)
|
|
|
|
if !ok {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(errors.NewMethodNotSupported(kind, "delete"), h.codec, w)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
|
|
|
// invoke admission control
|
2015-01-07 19:33:21 +00:00
|
|
|
err := h.admissionControl.Admit(admission.NewAttributesRecord(nil, namespace, parts[0], "DELETE"))
|
2015-01-06 16:44:43 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2015-01-06 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 05:33:25 +00:00
|
|
|
out, err := deleter.Delete(ctx, parts[1])
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-22 05:20:57 +00:00
|
|
|
op := h.createOperation(out, timeout, nil)
|
2015-02-10 01:04:37 +00:00
|
|
|
return h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
if len(parts) != 2 {
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-12 05:33:25 +00:00
|
|
|
updater, ok := storage.(RESTUpdater)
|
|
|
|
if !ok {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(errors.NewMethodNotSupported(kind, "create"), h.codec, w)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2014-08-09 21:12:55 +00:00
|
|
|
body, err := readBody(req)
|
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
obj := storage.New()
|
|
|
|
err = h.codec.DecodeInto(body, obj)
|
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-06 16:44:43 +00:00
|
|
|
|
|
|
|
// invoke admission control
|
2015-01-07 19:33:21 +00:00
|
|
|
err = h.admissionControl.Admit(admission.NewAttributesRecord(obj, namespace, parts[0], "UPDATE"))
|
2015-01-06 16:44:43 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2015-01-06 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 05:33:25 +00:00
|
|
|
out, err := updater.Update(ctx, obj)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
2015-02-10 01:04:37 +00:00
|
|
|
return errorJSON(err, h.codec, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-01-22 05:20:57 +00:00
|
|
|
op := h.createOperation(out, timeout, curry(h.setSelfLink, req))
|
2015-02-10 01:04:37 +00:00
|
|
|
return h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusNotFound
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-10 01:04:37 +00:00
|
|
|
return http.StatusOK
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// createOperation creates an operation to process a channel response.
|
2015-01-22 05:20:57 +00:00
|
|
|
func (h *RESTHandler) createOperation(out <-chan RESTResult, timeout time.Duration, onReceive func(RESTResult)) *Operation {
|
2014-09-26 00:20:28 +00:00
|
|
|
op := h.ops.NewOperation(out, onReceive)
|
2015-01-22 05:20:57 +00:00
|
|
|
op.WaitFor(timeout)
|
2014-08-09 21:12:55 +00:00
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
|
|
|
// finishReq finishes up a request, waiting until the operation finishes or, after a timeout, creating an
|
|
|
|
// Operation to receive the result and returning its ID down the writer.
|
2015-02-10 01:04:37 +00:00
|
|
|
// Returns the HTTP status code written to the response.
|
|
|
|
func (h *RESTHandler) finishReq(op *Operation, req *http.Request, w http.ResponseWriter) int {
|
2014-10-24 17:16:02 +00:00
|
|
|
result, complete := op.StatusOrResult()
|
|
|
|
obj := result.Object
|
2015-02-10 01:04:37 +00:00
|
|
|
var status int
|
2014-08-09 21:12:55 +00:00
|
|
|
if complete {
|
2015-02-10 01:04:37 +00:00
|
|
|
status = http.StatusOK
|
2014-10-24 17:16:02 +00:00
|
|
|
if result.Created {
|
|
|
|
status = http.StatusCreated
|
|
|
|
}
|
2014-08-09 21:12:55 +00:00
|
|
|
switch stat := obj.(type) {
|
|
|
|
case *api.Status:
|
|
|
|
if stat.Code != 0 {
|
|
|
|
status = stat.Code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-02-10 01:04:37 +00:00
|
|
|
status = http.StatusAccepted
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2015-02-10 01:04:37 +00:00
|
|
|
writeJSON(status, h.codec, obj, w)
|
|
|
|
return status
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|