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"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-09 21:12:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RESTHandler struct {
|
|
|
|
storage map[string]RESTStorage
|
2014-09-06 02:22:03 +00:00
|
|
|
codec runtime.Codec
|
2014-08-09 21:12:55 +00:00
|
|
|
ops *Operations
|
|
|
|
asyncOpWait time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP handles requests to all RESTStorage objects.
|
|
|
|
func (h *RESTHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
parts := splitPath(req.URL.Path)
|
|
|
|
if len(parts) < 1 {
|
|
|
|
notFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
storage := h.storage[parts[0]]
|
|
|
|
if storage == nil {
|
2014-09-09 21:05:18 +00:00
|
|
|
httplog.LogOf(req, w).Addf("'%v' has no storage object", parts[0])
|
2014-08-09 21:12:55 +00:00
|
|
|
notFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.handleRESTStorage(parts, req, w, storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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'
|
|
|
|
// Returns 404 if the method/pattern doesn't match one of these entries
|
|
|
|
// The s accepts several query parameters:
|
|
|
|
// sync=[false|true] Synchronous request (only applies to create, update, delete operations)
|
|
|
|
// timeout=<duration> Timeout for synchronous requests, only applies if sync=true
|
|
|
|
// labels=<label-selector> Used for filtering list operations
|
|
|
|
func (h *RESTHandler) handleRESTStorage(parts []string, req *http.Request, w http.ResponseWriter, storage RESTStorage) {
|
2014-09-25 18:34:01 +00:00
|
|
|
ctx := api.NewContext()
|
2014-08-09 21:12:55 +00:00
|
|
|
sync := req.URL.Query().Get("sync") == "true"
|
|
|
|
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 {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
2014-09-16 23:15:40 +00:00
|
|
|
field, err := labels.ParseSelector(req.URL.Query().Get("fields"))
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
2014-09-25 18:34:01 +00:00
|
|
|
list, err := storage.List(ctx, label, field)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSON(http.StatusOK, h.codec, list, w)
|
|
|
|
case 2:
|
2014-09-25 18:34:01 +00:00
|
|
|
item, err := storage.Get(ctx, parts[1])
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeJSON(http.StatusOK, h.codec, item, w)
|
|
|
|
default:
|
|
|
|
notFound(w, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
case "POST":
|
|
|
|
if len(parts) != 1 {
|
|
|
|
notFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body, err := readBody(req)
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := storage.New()
|
|
|
|
err = h.codec.DecodeInto(body, obj)
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
2014-09-25 18:34:01 +00:00
|
|
|
out, err := storage.Create(ctx, obj)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op := h.createOperation(out, sync, timeout)
|
2014-09-09 21:05:18 +00:00
|
|
|
h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
if len(parts) != 2 {
|
|
|
|
notFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
2014-09-25 18:34:01 +00:00
|
|
|
out, err := storage.Delete(ctx, parts[1])
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op := h.createOperation(out, sync, timeout)
|
2014-09-09 21:05:18 +00:00
|
|
|
h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
case "PUT":
|
|
|
|
if len(parts) != 2 {
|
|
|
|
notFound(w, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body, err := readBody(req)
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := storage.New()
|
|
|
|
err = h.codec.DecodeInto(body, obj)
|
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
2014-09-25 18:34:01 +00:00
|
|
|
out, err := storage.Update(ctx, obj)
|
2014-08-09 21:12:55 +00:00
|
|
|
if err != nil {
|
|
|
|
errorJSON(err, h.codec, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
op := h.createOperation(out, sync, timeout)
|
2014-09-09 21:05:18 +00:00
|
|
|
h.finishReq(op, req, w)
|
2014-08-09 21:12:55 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
notFound(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// createOperation creates an operation to process a channel response.
|
2014-09-06 02:22:03 +00:00
|
|
|
func (h *RESTHandler) createOperation(out <-chan runtime.Object, sync bool, timeout time.Duration) *Operation {
|
2014-08-09 21:12:55 +00:00
|
|
|
op := h.ops.NewOperation(out)
|
|
|
|
if sync {
|
|
|
|
op.WaitFor(timeout)
|
|
|
|
} else if h.asyncOpWait != 0 {
|
|
|
|
op.WaitFor(h.asyncOpWait)
|
|
|
|
}
|
|
|
|
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.
|
2014-09-09 21:05:18 +00:00
|
|
|
func (h *RESTHandler) finishReq(op *Operation, req *http.Request, w http.ResponseWriter) {
|
2014-08-09 21:12:55 +00:00
|
|
|
obj, complete := op.StatusOrResult()
|
|
|
|
if complete {
|
|
|
|
status := http.StatusOK
|
|
|
|
switch stat := obj.(type) {
|
|
|
|
case *api.Status:
|
|
|
|
if stat.Code != 0 {
|
|
|
|
status = stat.Code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeJSON(status, h.codec, obj, w)
|
|
|
|
} else {
|
|
|
|
writeJSON(http.StatusAccepted, h.codec, obj, w)
|
|
|
|
}
|
|
|
|
}
|