| 1 |
/* |
| 2 |
Copyright 2014 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package endpoints |
| 18 |
|
| 19 |
import ( |
| 20 |
"path" |
| 21 |
"time" |
| 22 |
|
| 23 |
restful "github.com/emicklei/go-restful/v3" |
| 24 |
|
| 25 |
apidiscoveryv2 "k8s.io/api/apidiscovery/v2" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
"k8s.io/apimachinery/pkg/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 29 |
utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 30 |
"k8s.io/apimachinery/pkg/util/managedfields" |
| 31 |
"k8s.io/apiserver/pkg/admission" |
| 32 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 33 |
"k8s.io/apiserver/pkg/endpoints/discovery" |
| 34 |
"k8s.io/apiserver/pkg/registry/rest" |
| 35 |
"k8s.io/apiserver/pkg/storageversion" |
| 36 |
) |
| 37 |
|
| 38 |
// ConvertabilityChecker indicates what versions a GroupKind is available in. |
| 39 |
type ConvertabilityChecker interface { |
| 40 |
// VersionsForGroupKind indicates what versions are available to convert a group kind. This determines |
| 41 |
// what our decoding abilities are. |
| 42 |
VersionsForGroupKind(gk schema.GroupKind) []schema.GroupVersion |
| 43 |
} |
| 44 |
|
| 45 |
// APIGroupVersion is a helper for exposing rest.Storage objects as http.Handlers via go-restful |
| 46 |
// It handles URLs of the form: |
| 47 |
// /${storage_key}[/${object_name}] |
| 48 |
// Where 'storage_key' points to a rest.Storage object stored in storage. |
| 49 |
// This object should contain all parameterization necessary for running a particular API version |
| 50 |
type APIGroupVersion struct { |
| 51 |
Storage map[string]rest.Storage |
| 52 |
|
| 53 |
Root string |
| 54 |
|
| 55 |
// GroupVersion is the external group version |
| 56 |
GroupVersion schema.GroupVersion |
| 57 |
|
| 58 |
// AllServedVersionsByResource is indexed by resource and maps to a list of versions that resource exists in. |
| 59 |
// This was created so that StorageVersion for APIs can include a list of all version that are served for each |
| 60 |
// GroupResource tuple. |
| 61 |
AllServedVersionsByResource map[string][]string |
| 62 |
|
| 63 |
// OptionsExternalVersion controls the Kubernetes APIVersion used for common objects in the apiserver |
| 64 |
// schema like api.Status, api.DeleteOptions, and metav1.ListOptions. Other implementors may |
| 65 |
// define a version "v1beta1" but want to use the Kubernetes "v1" internal objects. If |
| 66 |
// empty, defaults to GroupVersion. |
| 67 |
OptionsExternalVersion *schema.GroupVersion |
| 68 |
// MetaGroupVersion defaults to "meta.k8s.io/v1" and is the scheme group version used to decode |
| 69 |
// common API implementations like ListOptions. Future changes will allow this to vary by group |
| 70 |
// version (for when the inevitable meta/v2 group emerges). |
| 71 |
MetaGroupVersion *schema.GroupVersion |
| 72 |
|
| 73 |
// Serializer is used to determine how to convert responses from API methods into bytes to send over |
| 74 |
// the wire. |
| 75 |
Serializer runtime.NegotiatedSerializer |
| 76 |
ParameterCodec runtime.ParameterCodec |
| 77 |
|
| 78 |
Typer runtime.ObjectTyper |
| 79 |
Creater runtime.ObjectCreater |
| 80 |
Convertor runtime.ObjectConvertor |
| 81 |
ConvertabilityChecker ConvertabilityChecker |
| 82 |
Defaulter runtime.ObjectDefaulter |
| 83 |
Namer runtime.Namer |
| 84 |
UnsafeConvertor runtime.ObjectConvertor |
| 85 |
TypeConverter managedfields.TypeConverter |
| 86 |
|
| 87 |
EquivalentResourceRegistry runtime.EquivalentResourceRegistry |
| 88 |
|
| 89 |
// Authorizer determines whether a user is allowed to make a certain request. The Handler does a preliminary |
| 90 |
// authorization check using the request URI but it may be necessary to make additional checks, such as in |
| 91 |
// the create-on-update case |
| 92 |
Authorizer authorizer.Authorizer |
| 93 |
|
| 94 |
Admit admission.Interface |
| 95 |
|
| 96 |
MinRequestTimeout time.Duration |
| 97 |
|
| 98 |
// The limit on the request body size that would be accepted and decoded in a write request. |
| 99 |
// 0 means no limit. |
| 100 |
MaxRequestBodyBytes int64 |
| 101 |
} |
| 102 |
|
| 103 |
// InstallREST registers the REST handlers (storage, watch, proxy and redirect) into a restful Container. |
| 104 |
// It is expected that the provided path root prefix will serve all operations. Root MUST NOT end |
| 105 |
// in a slash. |
| 106 |
func (g *APIGroupVersion) InstallREST(container *restful.Container) ([]apidiscoveryv2.APIResourceDiscovery, []*storageversion.ResourceInfo, error) { |
| 107 |
prefix := path.Join(g.Root, g.GroupVersion.Group, g.GroupVersion.Version) |
| 108 |
installer := &APIInstaller{ |
| 109 |
group: g, |
| 110 |
prefix: prefix, |
| 111 |
minRequestTimeout: g.MinRequestTimeout, |
| 112 |
} |
| 113 |
|
| 114 |
apiResources, resourceInfos, ws, registrationErrors := installer.Install() |
| 115 |
versionDiscoveryHandler := discovery.NewAPIVersionHandler(g.Serializer, g.GroupVersion, staticLister{apiResources}) |
| 116 |
versionDiscoveryHandler.AddToWebService(ws) |
| 117 |
container.Add(ws) |
| 118 |
aggregatedDiscoveryResources, err := ConvertGroupVersionIntoToDiscovery(apiResources) |
| 119 |
if err != nil { |
| 120 |
registrationErrors = append(registrationErrors, err) |
| 121 |
} |
| 122 |
return aggregatedDiscoveryResources, removeNonPersistedResources(resourceInfos), utilerrors.NewAggregate(registrationErrors) |
| 123 |
} |
| 124 |
|
| 125 |
func removeNonPersistedResources(infos []*storageversion.ResourceInfo) []*storageversion.ResourceInfo { |
| 126 |
var filtered []*storageversion.ResourceInfo |
| 127 |
for _, info := range infos { |
| 128 |
// if EncodingVersion is empty, then the apiserver does not |
| 129 |
// need to register this resource via the storage version API, |
| 130 |
// thus we can remove it. |
| 131 |
if info != nil && len(info.EncodingVersion) > 0 { |
| 132 |
filtered = append(filtered, info) |
| 133 |
} |
| 134 |
} |
| 135 |
return filtered |
| 136 |
} |
| 137 |
|
| 138 |
// staticLister implements the APIResourceLister interface |
| 139 |
type staticLister struct { |
| 140 |
list []metav1.APIResource |
| 141 |
} |
| 142 |
|
| 143 |
func (s staticLister) ListAPIResources() []metav1.APIResource { |
| 144 |
return s.list |
| 145 |
} |
| 146 |
|
| 147 |
var _ discovery.APIResourceLister = &staticLister{} |