k3s/pkg/apiserver/redirect.go

106 lines
2.9 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"
"strconv"
"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"
2014-08-25 21:36:15 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/prometheus/client_golang/prometheus"
)
var (
redirectCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "apiserver_redirect_count",
Help: "Counter of redirect requests broken out by apiserver resource and HTTP response code.",
},
[]string{"resource", "code"},
)
2014-08-25 21:36:15 +00:00
)
func init() {
prometheus.MustRegister(redirectCounter)
}
2014-08-25 21:36:15 +00:00
type RedirectHandler struct {
2015-01-29 19:14:36 +00:00
storage map[string]RESTStorage
codec runtime.Codec
apiRequestInfoResolver *APIRequestInfoResolver
2014-08-25 21:36:15 +00:00
}
func (r *RedirectHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
apiResource := ""
var httpCode int
reqStart := time.Now()
defer func() {
redirectCounter.WithLabelValues(apiResource, strconv.Itoa(httpCode)).Inc()
apiserverLatencies.WithLabelValues("redirect", "get", strconv.Itoa(httpCode)).Observe(float64((time.Since(reqStart)) / time.Microsecond))
}()
2015-01-29 19:14:36 +00:00
requestInfo, err := r.apiRequestInfoResolver.GetAPIRequestInfo(req)
if err != nil {
notFound(w, req)
httpCode = http.StatusNotFound
return
}
2015-01-29 19:14:36 +00:00
resource, parts := requestInfo.Resource, requestInfo.Parts
ctx := api.WithNamespace(api.NewContext(), 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)
apiResource = "invalidResource"
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.(Redirector)
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
}
2014-09-25 18:34:01 +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)
httpCode = status.Code
2014-08-25 21:36:15 +00:00
return
}
w.Header().Set("Location", location)
w.WriteHeader(http.StatusTemporaryRedirect)
httpCode = http.StatusTemporaryRedirect
2014-08-25 21:36:15 +00:00
}