2014-06-06 23:40:48 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-06-16 05:34:16 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
2014-11-25 23:11:43 +00:00
|
|
|
"bytes"
|
2014-07-29 21:35:20 +00:00
|
|
|
"encoding/json"
|
2014-12-12 01:25:07 +00:00
|
|
|
"fmt"
|
2015-03-22 03:25:38 +00:00
|
|
|
"io"
|
2014-06-06 23:40:48 +00:00
|
|
|
"io/ioutil"
|
2015-05-28 04:38:21 +00:00
|
|
|
"net"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http"
|
2014-11-11 07:11:45 +00:00
|
|
|
"path"
|
2015-08-28 18:01:31 +00:00
|
|
|
rt "runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
"strings"
|
2014-06-19 04:04:11 +00:00
|
|
|
"time"
|
2014-06-17 01:03:44 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/admission"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
apierrors "k8s.io/kubernetes/pkg/api/errors"
|
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
|
|
|
"k8s.io/kubernetes/pkg/api/rest"
|
2015-10-09 01:32:15 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apiserver/metrics"
|
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-10-14 05:18:37 +00:00
|
|
|
utilerrors "k8s.io/kubernetes/pkg/util/errors"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/flushwriter"
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
|
Expose exec and logs via WebSockets
Not all clients and systems can support SPDY protocols. This commit adds
support for two new websocket protocols, one to handle streaming of pod
logs from a pod, and the other to allow exec to be tunneled over
websocket.
Browser support for chunked encoding is still poor, and web consoles
that wish to show pod logs may need to make compromises to display the
output. The /pods/<name>/log endpoint now supports websocket upgrade to
the 'binary.k8s.io' subprotocol, which sends chunks of logs as binary to
the client. Messages are written as logs are streamed from the container
daemon, so flushing should be unaffected.
Browser support for raw communication over SDPY is not possible, and
some languages lack libraries for it and HTTP/2. The Kubelet supports
upgrade to WebSocket instead of SPDY, and will multiplex STDOUT/IN/ERR
over websockets by prepending each binary message with a single byte
representing the channel (0 for IN, 1 for OUT, and 2 for ERR). Because
framing on WebSockets suffers from head-of-line blocking, clients and
other server code should ensure that no particular stream blocks. An
alternative subprotocol 'base64.channel.k8s.io' base64 encodes the body
and uses '0'-'9' to represent the channel for ease of use in browsers.
2015-09-11 20:09:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/wsstream"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/version"
|
2014-11-06 01:22:18 +00:00
|
|
|
|
2014-11-11 07:11:45 +00:00
|
|
|
"github.com/emicklei/go-restful"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2015-02-10 01:04:37 +00:00
|
|
|
func init() {
|
2015-06-23 07:14:43 +00:00
|
|
|
metrics.Register()
|
2015-02-10 01:04:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 21:36:15 +00:00
|
|
|
// mux is an object that can register http handlers.
|
2014-10-23 20:56:18 +00:00
|
|
|
type Mux interface {
|
2014-08-09 21:12:55 +00:00
|
|
|
Handle(pattern string, handler http.Handler)
|
|
|
|
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:24:16 +00:00
|
|
|
// APIGroupVersion is a helper for exposing rest.Storage objects as http.Handlers via go-restful
|
2014-06-06 23:40:48 +00:00
|
|
|
// It handles URLs of the form:
|
2014-08-09 21:12:55 +00:00
|
|
|
// /${storage_key}[/${object_name}]
|
2015-03-21 16:24:16 +00:00
|
|
|
// Where 'storage_key' points to a rest.Storage object stored in storage.
|
2015-06-16 02:39:31 +00:00
|
|
|
// This object should contain all parameterization necessary for running a particular API version
|
2014-11-11 07:11:45 +00:00
|
|
|
type APIGroupVersion struct {
|
2015-03-21 16:24:16 +00:00
|
|
|
Storage map[string]rest.Storage
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
Root string
|
|
|
|
|
|
|
|
// GroupVersion is the external group version
|
2015-11-12 20:20:20 +00:00
|
|
|
GroupVersion unversioned.GroupVersion
|
2015-03-04 20:57:05 +00:00
|
|
|
|
2015-10-20 17:34:26 +00:00
|
|
|
// RequestInfoResolver is used to parse URLs for the legacy proxy handler. Don't use this for anything else
|
2015-09-25 18:57:10 +00:00
|
|
|
// TODO: refactor proxy handler to use sub resources
|
2015-10-20 17:34:26 +00:00
|
|
|
RequestInfoResolver *RequestInfoResolver
|
2015-09-25 18:57:10 +00:00
|
|
|
|
2015-12-08 19:40:23 +00:00
|
|
|
// OptionsExternalVersion controls the Kubernetes APIVersion used for common objects in the apiserver
|
2015-12-16 13:02:09 +00:00
|
|
|
// schema like api.Status, api.DeleteOptions, and api.ListOptions. Other implementors may
|
2015-07-23 04:52:05 +00:00
|
|
|
// define a version "v1beta1" but want to use the Kubernetes "v1" internal objects. If
|
2015-12-08 19:40:23 +00:00
|
|
|
// empty, defaults to GroupVersion.
|
|
|
|
OptionsExternalVersion *unversioned.GroupVersion
|
2015-03-22 21:43:00 +00:00
|
|
|
|
2015-03-04 20:57:05 +00:00
|
|
|
Mapper meta.RESTMapper
|
|
|
|
|
2016-04-05 03:07:43 +00:00
|
|
|
// Serializer is used to determine how to convert responses from API methods into bytes to send over
|
|
|
|
// the wire.
|
2016-04-20 17:35:09 +00:00
|
|
|
Serializer runtime.NegotiatedSerializer
|
|
|
|
ParameterCodec runtime.ParameterCodec
|
2015-12-21 05:15:35 +00:00
|
|
|
|
2015-03-22 21:43:00 +00:00
|
|
|
Typer runtime.ObjectTyper
|
|
|
|
Creater runtime.ObjectCreater
|
|
|
|
Convertor runtime.ObjectConvertor
|
|
|
|
Linker runtime.SelfLinker
|
2015-03-04 20:57:05 +00:00
|
|
|
|
|
|
|
Admit admission.Interface
|
|
|
|
Context api.RequestContextMapper
|
2015-06-16 02:39:31 +00:00
|
|
|
|
|
|
|
MinRequestTimeout time.Duration
|
2016-02-24 19:10:37 +00:00
|
|
|
|
|
|
|
// SubresourceGroupVersionKind contains the GroupVersionKind overrides for each subresource that is
|
|
|
|
// accessible from this API group version. The GroupVersionKind is that of the external version of
|
|
|
|
// the subresource. The key of this map should be the path of the subresource. The keys here should
|
|
|
|
// match the keys in the Storage map above for subresources.
|
|
|
|
SubresourceGroupVersionKind map[string]unversioned.GroupVersionKind
|
2014-08-09 21:12:55 +00:00
|
|
|
}
|
2014-08-06 17:06:42 +00:00
|
|
|
|
2015-06-16 02:39:31 +00:00
|
|
|
type ProxyDialerFunc func(network, addr string) (net.Conn, error)
|
|
|
|
|
2015-05-12 02:41:13 +00:00
|
|
|
// TODO: Pipe these in through the apiserver cmd line
|
|
|
|
const (
|
|
|
|
// Minimum duration before timing out read/write requests
|
|
|
|
MinTimeoutSecs = 300
|
|
|
|
// Maximum duration before timing out read/write requests
|
|
|
|
MaxTimeoutSecs = 600
|
|
|
|
)
|
|
|
|
|
2015-01-28 21:13:10 +00:00
|
|
|
// InstallREST registers the REST handlers (storage, watch, proxy and redirect) into a restful Container.
|
2014-11-11 07:11:45 +00:00
|
|
|
// It is expected that the provided path root prefix will serve all operations. Root MUST NOT end
|
2015-09-14 20:34:11 +00:00
|
|
|
// in a slash.
|
2015-06-16 02:39:31 +00:00
|
|
|
func (g *APIGroupVersion) InstallREST(container *restful.Container) error {
|
2015-09-14 20:34:11 +00:00
|
|
|
installer := g.newInstaller()
|
|
|
|
ws := installer.NewWebService()
|
2015-09-15 03:55:18 +00:00
|
|
|
apiResources, registrationErrors := installer.Install(ws)
|
2015-12-21 05:15:35 +00:00
|
|
|
AddSupportedResourcesWebService(g.Serializer, ws, g.GroupVersion, apiResources)
|
2015-09-14 20:34:11 +00:00
|
|
|
container.Add(ws)
|
2015-10-14 05:18:37 +00:00
|
|
|
return utilerrors.NewAggregate(registrationErrors)
|
2015-09-14 20:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateREST registers the REST handlers for this APIGroupVersion to an existing web service
|
|
|
|
// in the restful Container. It will use the prefix (root/version) to find the existing
|
|
|
|
// web service. If a web service does not exist within the container to support the prefix
|
|
|
|
// this method will return an error.
|
|
|
|
func (g *APIGroupVersion) UpdateREST(container *restful.Container) error {
|
|
|
|
installer := g.newInstaller()
|
|
|
|
var ws *restful.WebService = nil
|
|
|
|
|
|
|
|
for i, s := range container.RegisteredWebServices() {
|
|
|
|
if s.RootPath() == installer.prefix {
|
|
|
|
ws = container.RegisteredWebServices()[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ws == nil {
|
|
|
|
return apierrors.NewInternalError(fmt.Errorf("unable to find an existing webservice for prefix %s", installer.prefix))
|
|
|
|
}
|
2015-09-15 03:55:18 +00:00
|
|
|
apiResources, registrationErrors := installer.Install(ws)
|
2015-12-21 05:15:35 +00:00
|
|
|
AddSupportedResourcesWebService(g.Serializer, ws, g.GroupVersion, apiResources)
|
2015-10-14 05:18:37 +00:00
|
|
|
return utilerrors.NewAggregate(registrationErrors)
|
2015-09-14 20:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newInstaller is a helper to create the installer. Used by InstallREST and UpdateREST.
|
|
|
|
func (g *APIGroupVersion) newInstaller() *APIInstaller {
|
2015-11-12 20:20:20 +00:00
|
|
|
prefix := path.Join(g.Root, g.GroupVersion.Group, g.GroupVersion.Version)
|
2015-02-09 14:47:13 +00:00
|
|
|
installer := &APIInstaller{
|
2015-05-27 01:39:42 +00:00
|
|
|
group: g,
|
2015-10-20 17:34:26 +00:00
|
|
|
info: g.RequestInfoResolver,
|
2015-05-27 01:39:42 +00:00
|
|
|
prefix: prefix,
|
2015-06-16 02:39:31 +00:00
|
|
|
minRequestTimeout: g.MinRequestTimeout,
|
2015-02-09 14:47:13 +00:00
|
|
|
}
|
2015-09-14 20:34:11 +00:00
|
|
|
return installer
|
2014-11-11 07:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: document all handlers
|
2016-04-27 01:51:37 +00:00
|
|
|
// InstallVersionHandler registers the APIServer's `/version` handler
|
|
|
|
func InstallVersionHandler(mux Mux, container *restful.Container) {
|
2015-01-07 23:43:38 +00:00
|
|
|
|
|
|
|
// Set up a service to return the git code version.
|
2016-04-19 15:28:13 +00:00
|
|
|
versionWS := new(restful.WebService)
|
|
|
|
versionWS.Path("/version")
|
|
|
|
versionWS.Doc("git code version from which this is built")
|
|
|
|
versionWS.Route(
|
|
|
|
versionWS.GET("/").To(handleVersion).
|
2015-01-07 23:43:38 +00:00
|
|
|
Doc("get the code version").
|
|
|
|
Operation("getCodeVersion").
|
|
|
|
Produces(restful.MIME_JSON).
|
|
|
|
Consumes(restful.MIME_JSON))
|
2016-04-19 15:28:13 +00:00
|
|
|
|
2016-04-27 01:51:37 +00:00
|
|
|
container.Add(versionWS)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 23:26:34 +00:00
|
|
|
// InstallLogsSupport registers the APIServer log support function into a mux.
|
2014-10-23 20:56:18 +00:00
|
|
|
func InstallLogsSupport(mux Mux) {
|
2014-11-11 07:11:45 +00:00
|
|
|
// TODO: use restful: ws.Route(ws.GET("/logs/{logpath:*}").To(fileHandler))
|
|
|
|
// See github.com/emicklei/go-restful/blob/master/examples/restful-serve-static.go
|
2014-10-09 23:26:34 +00:00
|
|
|
mux.Handle("/logs/", http.StripPrefix("/logs/", http.FileServer(http.Dir("/var/log/"))))
|
|
|
|
}
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
// TODO: needs to perform response type negotiation, this is probably the wrong way to recover panics
|
|
|
|
func InstallRecoverHandler(s runtime.NegotiatedSerializer, container *restful.Container) {
|
|
|
|
container.RecoverHandler(func(panicReason interface{}, httpWriter http.ResponseWriter) {
|
|
|
|
logStackOnRecover(s, panicReason, httpWriter)
|
|
|
|
})
|
2015-08-28 18:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Unify with RecoverPanics?
|
2015-12-21 05:15:35 +00:00
|
|
|
func logStackOnRecover(s runtime.NegotiatedSerializer, panicReason interface{}, w http.ResponseWriter) {
|
2015-08-28 18:01:31 +00:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
buffer.WriteString(fmt.Sprintf("recover from panic situation: - %v\r\n", panicReason))
|
|
|
|
for i := 2; ; i += 1 {
|
|
|
|
_, file, line, ok := rt.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buffer.WriteString(fmt.Sprintf(" %s:%d\r\n", file, line))
|
|
|
|
}
|
|
|
|
glog.Errorln(buffer.String())
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
headers := http.Header{}
|
|
|
|
if ct := w.Header().Get("Content-Type"); len(ct) > 0 {
|
|
|
|
headers.Set("Accept", ct)
|
|
|
|
}
|
|
|
|
errorNegotiated(apierrors.NewGenericServerResponse(http.StatusInternalServerError, "", api.Resource(""), "", "", 0, false), s, unversioned.GroupVersion{}, w, &http.Request{Header: headers})
|
2015-08-28 18:01:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
func InstallServiceErrorHandler(s runtime.NegotiatedSerializer, container *restful.Container, requestResolver *RequestInfoResolver, apiVersions []string) {
|
2015-04-23 07:06:59 +00:00
|
|
|
container.ServiceErrorHandler(func(serviceErr restful.ServiceError, request *restful.Request, response *restful.Response) {
|
2015-12-21 05:15:35 +00:00
|
|
|
serviceErrorHandler(s, requestResolver, apiVersions, serviceErr, request, response)
|
2015-04-23 07:06:59 +00:00
|
|
|
})
|
2015-04-15 23:33:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
func serviceErrorHandler(s runtime.NegotiatedSerializer, requestResolver *RequestInfoResolver, apiVersions []string, serviceErr restful.ServiceError, request *restful.Request, response *restful.Response) {
|
|
|
|
errorNegotiated(apierrors.NewGenericServerResponse(serviceErr.Code, "", api.Resource(""), "", "", 0, false), s, unversioned.GroupVersion{}, response.ResponseWriter, request.Request)
|
2015-04-15 23:33:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 03:55:18 +00:00
|
|
|
// Adds a service to return the supported api versions at the legacy /api.
|
2016-02-04 05:55:04 +00:00
|
|
|
func AddApiWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, getAPIVersionsFunc func(req *restful.Request) *unversioned.APIVersions) {
|
2015-01-07 23:43:38 +00:00
|
|
|
// TODO: InstallREST should register each version automatically
|
|
|
|
|
2016-02-12 01:03:43 +00:00
|
|
|
// Because in release 1.1, /api returns response with empty APIVersion, we
|
|
|
|
// use StripVersionNegotiatedSerializer to keep the response backwards
|
|
|
|
// compatible.
|
|
|
|
ss := StripVersionNegotiatedSerializer{s}
|
|
|
|
versionHandler := APIVersionHandler(ss, getAPIVersionsFunc)
|
2015-02-09 14:47:13 +00:00
|
|
|
ws := new(restful.WebService)
|
|
|
|
ws.Path(apiPrefix)
|
|
|
|
ws.Doc("get available API versions")
|
|
|
|
ws.Route(ws.GET("/").To(versionHandler).
|
|
|
|
Doc("get available API versions").
|
|
|
|
Operation("getAPIVersions").
|
2015-12-21 05:15:35 +00:00
|
|
|
Produces(s.SupportedMediaTypes()...).
|
2016-01-13 00:26:50 +00:00
|
|
|
Consumes(s.SupportedMediaTypes()...).
|
|
|
|
Writes(unversioned.APIVersions{}))
|
2015-02-09 14:47:13 +00:00
|
|
|
container.Add(ws)
|
2015-01-07 23:43:38 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:03:43 +00:00
|
|
|
// stripVersionEncoder strips APIVersion field from the encoding output. It's
|
|
|
|
// used to keep the responses at the discovery endpoints backward compatible
|
|
|
|
// with release-1.1, when the responses have empty APIVersion.
|
|
|
|
type stripVersionEncoder struct {
|
|
|
|
encoder runtime.Encoder
|
|
|
|
serializer runtime.Serializer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c stripVersionEncoder) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unversioned.GroupVersion) error {
|
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
err := c.encoder.EncodeToStream(obj, buf, overrides...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
roundTrippedObj, gvk, err := c.serializer.Decode(buf.Bytes(), nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gvk.Group = ""
|
|
|
|
gvk.Version = ""
|
2016-05-01 00:34:40 +00:00
|
|
|
roundTrippedObj.GetObjectKind().SetGroupVersionKind(*gvk)
|
2016-02-12 01:03:43 +00:00
|
|
|
return c.serializer.EncodeToStream(roundTrippedObj, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StripVersionNegotiatedSerializer will return stripVersionEncoder when
|
|
|
|
// EncoderForVersion is called. See comments for stripVersionEncoder.
|
|
|
|
type StripVersionNegotiatedSerializer struct {
|
|
|
|
runtime.NegotiatedSerializer
|
|
|
|
}
|
|
|
|
|
2016-04-23 19:00:28 +00:00
|
|
|
func (n StripVersionNegotiatedSerializer) EncoderForVersion(encoder runtime.Encoder, gv unversioned.GroupVersion) runtime.Encoder {
|
|
|
|
serializer, ok := encoder.(runtime.Serializer)
|
|
|
|
if !ok {
|
|
|
|
// The stripVersionEncoder needs both an encoder and decoder, but is called from a context that doesn't have access to the
|
|
|
|
// decoder. We do a best effort cast here (since this code path is only for backwards compatibility) to get access to the caller's
|
|
|
|
// decoder.
|
|
|
|
panic(fmt.Sprintf("Unable to extract serializer from %#v", encoder))
|
|
|
|
}
|
|
|
|
versioned := n.NegotiatedSerializer.EncoderForVersion(encoder, gv)
|
|
|
|
return stripVersionEncoder{versioned, serializer}
|
2016-02-12 01:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func keepUnversioned(group string) bool {
|
|
|
|
return group == "" || group == "extensions"
|
|
|
|
}
|
|
|
|
|
2015-09-15 03:55:18 +00:00
|
|
|
// Adds a service to return the supported api versions at /apis.
|
2016-02-04 05:55:04 +00:00
|
|
|
func AddApisWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, f func(req *restful.Request) []unversioned.APIGroup) {
|
2016-02-12 01:03:43 +00:00
|
|
|
// Because in release 1.1, /apis returns response with empty APIVersion, we
|
|
|
|
// use StripVersionNegotiatedSerializer to keep the response backwards
|
|
|
|
// compatible.
|
|
|
|
ss := StripVersionNegotiatedSerializer{s}
|
|
|
|
rootAPIHandler := RootAPIHandler(ss, f)
|
2015-09-15 03:55:18 +00:00
|
|
|
ws := new(restful.WebService)
|
|
|
|
ws.Path(apiPrefix)
|
|
|
|
ws.Doc("get available API versions")
|
|
|
|
ws.Route(ws.GET("/").To(rootAPIHandler).
|
|
|
|
Doc("get available API versions").
|
|
|
|
Operation("getAPIVersions").
|
2015-12-21 05:15:35 +00:00
|
|
|
Produces(s.SupportedMediaTypes()...).
|
2016-01-13 00:26:50 +00:00
|
|
|
Consumes(s.SupportedMediaTypes()...).
|
|
|
|
Writes(unversioned.APIGroupList{}))
|
2015-09-15 03:55:18 +00:00
|
|
|
container.Add(ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a service to return the supported versions, preferred version, and name
|
2015-10-09 22:04:41 +00:00
|
|
|
// of a group. E.g., a such web service will be registered at /apis/extensions.
|
2015-12-21 05:15:35 +00:00
|
|
|
func AddGroupWebService(s runtime.NegotiatedSerializer, container *restful.Container, path string, group unversioned.APIGroup) {
|
2016-02-12 01:03:43 +00:00
|
|
|
ss := s
|
|
|
|
if keepUnversioned(group.Name) {
|
|
|
|
// Because in release 1.1, /apis/extensions returns response with empty
|
|
|
|
// APIVersion, we use StripVersionNegotiatedSerializer to keep the
|
|
|
|
// response backwards compatible.
|
|
|
|
ss = StripVersionNegotiatedSerializer{s}
|
|
|
|
}
|
|
|
|
groupHandler := GroupHandler(ss, group)
|
2015-09-15 03:55:18 +00:00
|
|
|
ws := new(restful.WebService)
|
|
|
|
ws.Path(path)
|
|
|
|
ws.Doc("get information of a group")
|
|
|
|
ws.Route(ws.GET("/").To(groupHandler).
|
|
|
|
Doc("get information of a group").
|
|
|
|
Operation("getAPIGroup").
|
2015-12-21 05:15:35 +00:00
|
|
|
Produces(s.SupportedMediaTypes()...).
|
2016-01-13 00:26:50 +00:00
|
|
|
Consumes(s.SupportedMediaTypes()...).
|
|
|
|
Writes(unversioned.APIGroup{}))
|
2015-09-15 03:55:18 +00:00
|
|
|
container.Add(ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a service to return the supported resources, E.g., a such web service
|
2015-10-09 22:04:41 +00:00
|
|
|
// will be registered at /apis/extensions/v1.
|
2015-12-21 05:15:35 +00:00
|
|
|
func AddSupportedResourcesWebService(s runtime.NegotiatedSerializer, ws *restful.WebService, groupVersion unversioned.GroupVersion, apiResources []unversioned.APIResource) {
|
2016-02-12 01:03:43 +00:00
|
|
|
ss := s
|
|
|
|
if keepUnversioned(groupVersion.Group) {
|
|
|
|
// Because in release 1.1, /apis/extensions/v1beta1 returns response
|
|
|
|
// with empty APIVersion, we use StripVersionNegotiatedSerializer to
|
|
|
|
// keep the response backwards compatible.
|
|
|
|
ss = StripVersionNegotiatedSerializer{s}
|
|
|
|
}
|
|
|
|
resourceHandler := SupportedResourcesHandler(ss, groupVersion, apiResources)
|
2015-09-15 03:55:18 +00:00
|
|
|
ws.Route(ws.GET("/").To(resourceHandler).
|
|
|
|
Doc("get available resources").
|
|
|
|
Operation("getAPIResources").
|
2015-12-21 05:15:35 +00:00
|
|
|
Produces(s.SupportedMediaTypes()...).
|
2016-01-13 00:26:50 +00:00
|
|
|
Consumes(s.SupportedMediaTypes()...).
|
|
|
|
Writes(unversioned.APIResourceList{}))
|
2015-09-15 03:55:18 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// handleVersion writes the server's version information.
|
2014-11-11 07:11:45 +00:00
|
|
|
func handleVersion(req *restful.Request, resp *restful.Response) {
|
|
|
|
writeRawJSON(http.StatusOK, version.Get(), resp.ResponseWriter)
|
2014-07-29 21:36:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 00:20:40 +00:00
|
|
|
// APIVersionHandler returns a handler which will list the provided versions as available.
|
2016-02-04 05:55:04 +00:00
|
|
|
func APIVersionHandler(s runtime.NegotiatedSerializer, getAPIVersionsFunc func(req *restful.Request) *unversioned.APIVersions) restful.RouteFunction {
|
2014-11-11 07:11:45 +00:00
|
|
|
return func(req *restful.Request, resp *restful.Response) {
|
2016-02-04 05:55:04 +00:00
|
|
|
writeNegotiated(s, unversioned.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, getAPIVersionsFunc(req))
|
2014-11-11 07:11:45 +00:00
|
|
|
}
|
2014-10-29 00:20:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 03:55:18 +00:00
|
|
|
// RootAPIHandler returns a handler which will list the provided groups and versions as available.
|
2016-02-04 05:55:04 +00:00
|
|
|
func RootAPIHandler(s runtime.NegotiatedSerializer, f func(req *restful.Request) []unversioned.APIGroup) restful.RouteFunction {
|
2015-09-15 03:55:18 +00:00
|
|
|
return func(req *restful.Request, resp *restful.Response) {
|
2016-02-04 05:55:04 +00:00
|
|
|
writeNegotiated(s, unversioned.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, &unversioned.APIGroupList{Groups: f(req)})
|
2015-09-15 03:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GroupHandler returns a handler which will return the api.GroupAndVersion of
|
|
|
|
// the group.
|
2015-12-21 05:15:35 +00:00
|
|
|
func GroupHandler(s runtime.NegotiatedSerializer, group unversioned.APIGroup) restful.RouteFunction {
|
2015-09-15 03:55:18 +00:00
|
|
|
return func(req *restful.Request, resp *restful.Response) {
|
2015-12-21 05:15:35 +00:00
|
|
|
writeNegotiated(s, unversioned.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, &group)
|
2015-09-15 03:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SupportedResourcesHandler returns a handler which will list the provided resources as available.
|
2015-12-21 05:15:35 +00:00
|
|
|
func SupportedResourcesHandler(s runtime.NegotiatedSerializer, groupVersion unversioned.GroupVersion, apiResources []unversioned.APIResource) restful.RouteFunction {
|
2015-09-15 03:55:18 +00:00
|
|
|
return func(req *restful.Request, resp *restful.Response) {
|
2015-12-21 05:15:35 +00:00
|
|
|
writeNegotiated(s, unversioned.GroupVersion{}, resp.ResponseWriter, req.Request, http.StatusOK, &unversioned.APIResourceList{GroupVersion: groupVersion.String(), APIResources: apiResources})
|
2015-09-15 03:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:21:40 +00:00
|
|
|
// write renders a returned runtime.Object to the response as a stream or an encoded object. If the object
|
|
|
|
// returned by the response implements rest.ResourceStreamer that interface will be used to render the
|
|
|
|
// response. The Accept header and current API version will be passed in, and the output will be copied
|
|
|
|
// directly to the response body. If content type is returned it is used, otherwise the content type will
|
|
|
|
// be "application/octet-stream". All other objects are sent to standard JSON serialization.
|
2015-12-21 05:15:35 +00:00
|
|
|
func write(statusCode int, gv unversioned.GroupVersion, s runtime.NegotiatedSerializer, object runtime.Object, w http.ResponseWriter, req *http.Request) {
|
2015-03-22 03:25:38 +00:00
|
|
|
if stream, ok := object.(rest.ResourceStreamer); ok {
|
2015-12-21 05:15:35 +00:00
|
|
|
out, flush, contentType, err := stream.InputStream(gv.String(), req.Header.Get("Accept"))
|
2015-03-22 03:25:38 +00:00
|
|
|
if err != nil {
|
2015-12-21 05:15:35 +00:00
|
|
|
errorNegotiated(err, s, gv, w, req)
|
2015-03-22 03:25:38 +00:00
|
|
|
return
|
|
|
|
}
|
2015-04-06 16:58:00 +00:00
|
|
|
if out == nil {
|
|
|
|
// No output provided - return StatusNoContent
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
2015-03-22 03:25:38 +00:00
|
|
|
defer out.Close()
|
Expose exec and logs via WebSockets
Not all clients and systems can support SPDY protocols. This commit adds
support for two new websocket protocols, one to handle streaming of pod
logs from a pod, and the other to allow exec to be tunneled over
websocket.
Browser support for chunked encoding is still poor, and web consoles
that wish to show pod logs may need to make compromises to display the
output. The /pods/<name>/log endpoint now supports websocket upgrade to
the 'binary.k8s.io' subprotocol, which sends chunks of logs as binary to
the client. Messages are written as logs are streamed from the container
daemon, so flushing should be unaffected.
Browser support for raw communication over SDPY is not possible, and
some languages lack libraries for it and HTTP/2. The Kubelet supports
upgrade to WebSocket instead of SPDY, and will multiplex STDOUT/IN/ERR
over websockets by prepending each binary message with a single byte
representing the channel (0 for IN, 1 for OUT, and 2 for ERR). Because
framing on WebSockets suffers from head-of-line blocking, clients and
other server code should ensure that no particular stream blocks. An
alternative subprotocol 'base64.channel.k8s.io' base64 encodes the body
and uses '0'-'9' to represent the channel for ease of use in browsers.
2015-09-11 20:09:51 +00:00
|
|
|
|
|
|
|
if wsstream.IsWebSocketRequest(req) {
|
|
|
|
r := wsstream.NewReader(out, true)
|
|
|
|
if err := r.Copy(w, req); err != nil {
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("error encountered while streaming results via websocket: %v", err))
|
Expose exec and logs via WebSockets
Not all clients and systems can support SPDY protocols. This commit adds
support for two new websocket protocols, one to handle streaming of pod
logs from a pod, and the other to allow exec to be tunneled over
websocket.
Browser support for chunked encoding is still poor, and web consoles
that wish to show pod logs may need to make compromises to display the
output. The /pods/<name>/log endpoint now supports websocket upgrade to
the 'binary.k8s.io' subprotocol, which sends chunks of logs as binary to
the client. Messages are written as logs are streamed from the container
daemon, so flushing should be unaffected.
Browser support for raw communication over SDPY is not possible, and
some languages lack libraries for it and HTTP/2. The Kubelet supports
upgrade to WebSocket instead of SPDY, and will multiplex STDOUT/IN/ERR
over websockets by prepending each binary message with a single byte
representing the channel (0 for IN, 1 for OUT, and 2 for ERR). Because
framing on WebSockets suffers from head-of-line blocking, clients and
other server code should ensure that no particular stream blocks. An
alternative subprotocol 'base64.channel.k8s.io' base64 encodes the body
and uses '0'-'9' to represent the channel for ease of use in browsers.
2015-09-11 20:09:51 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-22 03:25:38 +00:00
|
|
|
if len(contentType) == 0 {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
w.WriteHeader(statusCode)
|
2015-04-06 16:58:00 +00:00
|
|
|
writer := w.(io.Writer)
|
|
|
|
if flush {
|
|
|
|
writer = flushwriter.Wrap(w)
|
|
|
|
}
|
|
|
|
io.Copy(writer, out)
|
2015-03-22 03:25:38 +00:00
|
|
|
return
|
|
|
|
}
|
2015-12-21 05:15:35 +00:00
|
|
|
writeNegotiated(s, gv, w, req, statusCode, object)
|
2015-06-08 23:33:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
// writeNegotiated renders an object in the content type negotiated by the client
|
|
|
|
func writeNegotiated(s runtime.NegotiatedSerializer, gv unversioned.GroupVersion, w http.ResponseWriter, req *http.Request, statusCode int, object runtime.Object) {
|
2016-04-23 19:00:28 +00:00
|
|
|
serializer, err := negotiateOutputSerializer(req, s)
|
2015-12-21 05:15:35 +00:00
|
|
|
if err != nil {
|
|
|
|
status := errToAPIStatus(err)
|
|
|
|
writeRawJSON(int(status.Code), status, w)
|
|
|
|
return
|
2015-06-08 23:33:58 +00:00
|
|
|
}
|
2015-03-22 03:25:38 +00:00
|
|
|
|
2016-04-23 19:00:28 +00:00
|
|
|
w.Header().Set("Content-Type", serializer.MediaType)
|
2015-09-18 00:43:05 +00:00
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
encoder := s.EncoderForVersion(serializer, gv)
|
|
|
|
if err := encoder.EncodeToStream(object, w); err != nil {
|
|
|
|
errorJSONFatal(err, encoder, w)
|
2014-11-25 23:11:43 +00:00
|
|
|
}
|
2014-07-29 22:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 05:15:35 +00:00
|
|
|
// errorNegotiated renders an error to the response. Returns the HTTP status code of the error.
|
|
|
|
func errorNegotiated(err error, s runtime.NegotiatedSerializer, gv unversioned.GroupVersion, w http.ResponseWriter, req *http.Request) int {
|
2014-07-31 18:26:34 +00:00
|
|
|
status := errToAPIStatus(err)
|
2015-11-18 18:15:16 +00:00
|
|
|
code := int(status.Code)
|
2015-12-21 05:15:35 +00:00
|
|
|
writeNegotiated(s, gv, w, req, code, status)
|
2015-11-18 18:15:16 +00:00
|
|
|
return code
|
2014-07-31 18:26:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 01:04:37 +00:00
|
|
|
// errorJSONFatal renders an error to the response, and if codec fails will render plaintext.
|
|
|
|
// Returns the HTTP status code of the error.
|
2015-12-21 05:15:35 +00:00
|
|
|
func errorJSONFatal(err error, codec runtime.Encoder, w http.ResponseWriter) int {
|
2016-01-15 07:32:10 +00:00
|
|
|
utilruntime.HandleError(fmt.Errorf("apiserver was unable to write a JSON response: %v", err))
|
2014-12-12 01:25:07 +00:00
|
|
|
status := errToAPIStatus(err)
|
2015-11-18 18:15:16 +00:00
|
|
|
code := int(status.Code)
|
2015-12-10 02:15:02 +00:00
|
|
|
output, err := runtime.Encode(codec, status)
|
2014-12-12 01:25:07 +00:00
|
|
|
if err != nil {
|
2015-11-18 18:15:16 +00:00
|
|
|
w.WriteHeader(code)
|
2014-12-12 01:25:07 +00:00
|
|
|
fmt.Fprintf(w, "%s: %s", status.Reason, status.Message)
|
2015-11-18 18:15:16 +00:00
|
|
|
return code
|
2014-12-12 01:25:07 +00:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-11-18 18:15:16 +00:00
|
|
|
w.WriteHeader(code)
|
2014-12-12 01:25:07 +00:00
|
|
|
w.Write(output)
|
2015-11-18 18:15:16 +00:00
|
|
|
return code
|
2014-12-12 01:25:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:14:00 +00:00
|
|
|
// writeRawJSON writes a non-API object in JSON.
|
|
|
|
func writeRawJSON(statusCode int, object interface{}, w http.ResponseWriter) {
|
2014-11-25 23:11:43 +00:00
|
|
|
output, err := json.MarshalIndent(object, "", " ")
|
2014-07-29 22:14:00 +00:00
|
|
|
if err != nil {
|
2014-07-31 18:26:34 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2014-07-29 22:14:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
w.Write(output)
|
|
|
|
}
|
|
|
|
|
2014-07-29 22:13:02 +00:00
|
|
|
func parseTimeout(str string) time.Duration {
|
|
|
|
if str != "" {
|
|
|
|
timeout, err := time.ParseDuration(str)
|
|
|
|
if err == nil {
|
|
|
|
return timeout
|
|
|
|
}
|
2014-11-20 10:00:36 +00:00
|
|
|
glog.Errorf("Failed to parse %q: %v", str, err)
|
2014-07-29 22:13:02 +00:00
|
|
|
}
|
2015-09-03 17:50:23 +00:00
|
|
|
return 30 * time.Second
|
2014-07-29 22:13:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 22:10:29 +00:00
|
|
|
func readBody(req *http.Request) ([]byte, error) {
|
|
|
|
defer req.Body.Close()
|
|
|
|
return ioutil.ReadAll(req.Body)
|
|
|
|
}
|
2014-08-06 17:06:42 +00:00
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// splitPath returns the segments for a URL path.
|
2014-08-06 17:06:42 +00:00
|
|
|
func splitPath(path string) []string {
|
|
|
|
path = strings.Trim(path, "/")
|
|
|
|
if path == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return strings.Split(path, "/")
|
|
|
|
}
|