2014-08-25 21:36:15 +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"
|
2015-02-10 01:04:37 +00:00
|
|
|
"time"
|
2014-08-25 21:36:15 +00:00
|
|
|
|
2014-09-25 18:34:01 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-01-12 05:33:25 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2015-03-21 16:24:16 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
|
2014-08-25 21:36:15 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
|
2014-09-06 02:22:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-08-25 21:36:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedirectHandler struct {
|
2015-03-21 16:24:16 +00:00
|
|
|
storage map[string]rest.Storage
|
2015-01-29 19:14:36 +00:00
|
|
|
codec runtime.Codec
|
2015-02-11 22:09:25 +00:00
|
|
|
context api.RequestContextMapper
|
2015-01-29 19:14:36 +00:00
|
|
|
apiRequestInfoResolver *APIRequestInfoResolver
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedirectHandler) 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-03-06 22:36:03 +00:00
|
|
|
defer monitor("redirect", &verb, &apiResource, &httpCode, reqStart)
|
2015-02-10 01:04:37 +00:00
|
|
|
|
2015-01-29 19:14:36 +00:00
|
|
|
requestInfo, err := r.apiRequestInfoResolver.GetAPIRequestInfo(req)
|
2014-12-09 19:23:21 +00:00
|
|
|
if err != nil {
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusNotFound
|
2014-12-09 19:23:21 +00:00
|
|
|
return
|
2014-10-28 00:54:33 +00:00
|
|
|
}
|
2015-02-10 22:23:02 +00:00
|
|
|
verb = requestInfo.Verb
|
2015-01-29 19:14:36 +00:00
|
|
|
resource, parts := requestInfo.Resource, requestInfo.Parts
|
2015-02-11 22:09:25 +00:00
|
|
|
ctx, ok := r.context.Get(req)
|
|
|
|
if !ok {
|
|
|
|
ctx = api.NewContext()
|
|
|
|
}
|
|
|
|
ctx = api.WithNamespace(ctx, requestInfo.Namespace)
|
2014-12-09 19:23:21 +00:00
|
|
|
|
2015-01-29 19:14:36 +00:00
|
|
|
// redirection requires /resource/resourceName path parts
|
2014-08-25 21:36:15 +00:00
|
|
|
if len(parts) != 2 || req.Method != "GET" {
|
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusNotFound
|
2014-08-25 21:36:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
id := parts[1]
|
2015-01-29 19:14:36 +00:00
|
|
|
storage, ok := r.storage[resource]
|
2014-08-25 21:36:15 +00:00
|
|
|
if !ok {
|
2015-01-29 19:14:36 +00:00
|
|
|
httplog.LogOf(req, w).Addf("'%v' has no storage object", resource)
|
2014-08-25 21:36:15 +00:00
|
|
|
notFound(w, req)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusNotFound
|
2014-08-25 21:36:15 +00:00
|
|
|
return
|
|
|
|
}
|
2015-02-10 01:04:37 +00:00
|
|
|
apiResource = resource
|
2014-08-25 21:36:15 +00:00
|
|
|
|
2015-03-21 16:24:16 +00:00
|
|
|
redirector, ok := storage.(rest.Redirector)
|
2014-08-25 21:36:15 +00:00
|
|
|
if !ok {
|
2015-01-29 19:14:36 +00:00
|
|
|
httplog.LogOf(req, w).Addf("'%v' is not a redirector", resource)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = errorJSON(errors.NewMethodNotSupported(resource, "redirect"), r.codec, w)
|
2014-08-25 21:36:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-23 18:42:39 +00:00
|
|
|
location, _, err := redirector.ResourceLocation(ctx, id)
|
2014-08-25 21:36:15 +00:00
|
|
|
if err != nil {
|
|
|
|
status := errToAPIStatus(err)
|
|
|
|
writeJSON(status.Code, r.codec, status, w)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = status.Code
|
2014-08-25 21:36:15 +00:00
|
|
|
return
|
|
|
|
}
|
2015-03-23 18:42:39 +00:00
|
|
|
if location == nil {
|
|
|
|
httplog.LogOf(req, w).Addf("ResourceLocation for %v returned nil", id)
|
|
|
|
notFound(w, req)
|
|
|
|
httpCode = http.StatusNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to http
|
|
|
|
if location.Scheme == "" {
|
|
|
|
location.Scheme = "http"
|
|
|
|
}
|
2014-08-25 21:36:15 +00:00
|
|
|
|
2015-03-23 18:42:39 +00:00
|
|
|
w.Header().Set("Location", location.String())
|
2014-08-25 21:36:15 +00:00
|
|
|
w.WriteHeader(http.StatusTemporaryRedirect)
|
2015-02-10 01:04:37 +00:00
|
|
|
httpCode = http.StatusTemporaryRedirect
|
2014-08-25 21:36:15 +00:00
|
|
|
}
|