k3s/pkg/apiserver/redirect.go

104 lines
2.8 KiB
Go
Raw Normal View History

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"
"time"
2014-08-25 21:36:15 +00:00
2014-09-25 18:34:01 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
2014-08-25 21:36:15 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
2014-08-25 21:36:15 +00:00
)
type RedirectHandler struct {
storage map[string]rest.Storage
2015-01-29 19:14:36 +00:00
codec runtime.Codec
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) {
var verb string
var apiResource string
var httpCode int
reqStart := time.Now()
defer monitor("redirect", &verb, &apiResource, &httpCode, reqStart)
2015-01-29 19:14:36 +00:00
requestInfo, err := r.apiRequestInfoResolver.GetAPIRequestInfo(req)
if err != nil {
notFound(w, req)
httpCode = http.StatusNotFound
return
}
verb = requestInfo.Verb
2015-01-29 19:14:36 +00:00
resource, parts := requestInfo.Resource, requestInfo.Parts
ctx, ok := r.context.Get(req)
if !ok {
ctx = api.NewContext()
}
ctx = api.WithNamespace(ctx, requestInfo.Namespace)
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)
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)
httpCode = http.StatusNotFound
2014-08-25 21:36:15 +00:00
return
}
apiResource = resource
2014-08-25 21:36:15 +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)
httpCode = errorJSON(errors.NewMethodNotSupported(resource, "redirect"), r.codec, w)
2014-08-25 21:36:15 +00:00
return
}
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)
httpCode = status.Code
2014-08-25 21:36:15 +00:00
return
}
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
w.Header().Set("Location", location.String())
2014-08-25 21:36:15 +00:00
w.WriteHeader(http.StatusTemporaryRedirect)
httpCode = http.StatusTemporaryRedirect
2014-08-25 21:36:15 +00:00
}