blob: 23d13adc3d3b71d807eb8c3a91f7393354651a89 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package endpoints
18
19import (
20 "path"
21 "time"
22
23 "github.com/emicklei/go-restful"
24
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/runtime/schema"
28 utilerrors "k8s.io/apimachinery/pkg/util/errors"
29 "k8s.io/apimachinery/pkg/util/sets"
30 "k8s.io/apiserver/pkg/admission"
31 "k8s.io/apiserver/pkg/endpoints/discovery"
32 "k8s.io/apiserver/pkg/registry/rest"
33 openapicommon "k8s.io/kube-openapi/pkg/common"
34)
35
36// APIGroupVersion is a helper for exposing rest.Storage objects as http.Handlers via go-restful
37// It handles URLs of the form:
38// /${storage_key}[/${object_name}]
39// Where 'storage_key' points to a rest.Storage object stored in storage.
40// This object should contain all parameterization necessary for running a particular API version
41type APIGroupVersion struct {
42 Storage map[string]rest.Storage
43
44 Root string
45
46 // GroupVersion is the external group version
47 GroupVersion schema.GroupVersion
48
49 // OptionsExternalVersion controls the Kubernetes APIVersion used for common objects in the apiserver
50 // schema like api.Status, api.DeleteOptions, and metav1.ListOptions. Other implementors may
51 // define a version "v1beta1" but want to use the Kubernetes "v1" internal objects. If
52 // empty, defaults to GroupVersion.
53 OptionsExternalVersion *schema.GroupVersion
54 // MetaGroupVersion defaults to "meta.k8s.io/v1" and is the scheme group version used to decode
55 // common API implementations like ListOptions. Future changes will allow this to vary by group
56 // version (for when the inevitable meta/v2 group emerges).
57 MetaGroupVersion *schema.GroupVersion
58
59 // RootScopedKinds are the root scoped kinds for the primary GroupVersion
60 RootScopedKinds sets.String
61
62 // Serializer is used to determine how to convert responses from API methods into bytes to send over
63 // the wire.
64 Serializer runtime.NegotiatedSerializer
65 ParameterCodec runtime.ParameterCodec
66
67 Typer runtime.ObjectTyper
68 Creater runtime.ObjectCreater
69 Convertor runtime.ObjectConvertor
70 Defaulter runtime.ObjectDefaulter
71 Linker runtime.SelfLinker
72 UnsafeConvertor runtime.ObjectConvertor
73
74 Admit admission.Interface
75
76 MinRequestTimeout time.Duration
77
78 // EnableAPIResponseCompression indicates whether API Responses should support compression
79 // if the client requests it via Accept-Encoding
80 EnableAPIResponseCompression bool
81
82 // OpenAPIConfig lets the individual handlers build a subset of the OpenAPI schema before they are installed.
83 OpenAPIConfig *openapicommon.Config
84}
85
86// InstallREST registers the REST handlers (storage, watch, proxy and redirect) into a restful Container.
87// It is expected that the provided path root prefix will serve all operations. Root MUST NOT end
88// in a slash.
89func (g *APIGroupVersion) InstallREST(container *restful.Container) error {
90 prefix := path.Join(g.Root, g.GroupVersion.Group, g.GroupVersion.Version)
91 installer := &APIInstaller{
92 group: g,
93 prefix: prefix,
94 minRequestTimeout: g.MinRequestTimeout,
95 enableAPIResponseCompression: g.EnableAPIResponseCompression,
96 }
97
98 apiResources, ws, registrationErrors := installer.Install()
99 versionDiscoveryHandler := discovery.NewAPIVersionHandler(g.Serializer, g.GroupVersion, staticLister{apiResources})
100 versionDiscoveryHandler.AddToWebService(ws)
101 container.Add(ws)
102 return utilerrors.NewAggregate(registrationErrors)
103}
104
105// staticLister implements the APIResourceLister interface
106type staticLister struct {
107 list []metav1.APIResource
108}
109
110func (s staticLister) ListAPIResources() []metav1.APIResource {
111 return s.list
112}
113
114var _ discovery.APIResourceLister = &staticLister{}