|
| 1 | +/* |
| 2 | +Copyright 2018 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 apiserver |
| 18 | + |
| 19 | +import ( |
| 20 | + "k8s.io/apimachinery/pkg/apimachinery" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/apimachinery/pkg/runtime" |
| 23 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 24 | + genericapi "k8s.io/apiserver/pkg/endpoints" |
| 25 | + "k8s.io/apiserver/pkg/endpoints/discovery" |
| 26 | + genericapiserver "k8s.io/apiserver/pkg/server" |
| 27 | + |
| 28 | + specificapi "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/apiserver/installer" |
| 29 | + "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider" |
| 30 | + metricstorage "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/registry/external_metrics" |
| 31 | + "k8s.io/metrics/pkg/apis/external_metrics" |
| 32 | +) |
| 33 | + |
| 34 | +// InstallExternalMetricsAPI registers the api server in Kube Aggregator |
| 35 | +func (s *CustomMetricsAdapterServer) InstallExternalMetricsAPI() error { |
| 36 | + |
| 37 | + groupMeta := registry.GroupOrDie(external_metrics.GroupName) |
| 38 | + |
| 39 | + preferredVersionForDiscovery := metav1.GroupVersionForDiscovery{ |
| 40 | + GroupVersion: groupMeta.GroupVersion.String(), |
| 41 | + Version: groupMeta.GroupVersion.Version, |
| 42 | + } |
| 43 | + groupVersion := metav1.GroupVersionForDiscovery{ |
| 44 | + GroupVersion: groupMeta.GroupVersion.String(), |
| 45 | + Version: groupMeta.GroupVersion.Version, |
| 46 | + } |
| 47 | + apiGroup := metav1.APIGroup{ |
| 48 | + Name: groupMeta.GroupVersion.Group, |
| 49 | + Versions: []metav1.GroupVersionForDiscovery{groupVersion}, |
| 50 | + PreferredVersion: preferredVersionForDiscovery, |
| 51 | + } |
| 52 | + |
| 53 | + emAPI := s.emAPI(groupMeta, &groupMeta.GroupVersion) |
| 54 | + |
| 55 | + if err := emAPI.InstallREST(s.GenericAPIServer.Handler.GoRestfulContainer); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + s.GenericAPIServer.DiscoveryGroupManager.AddGroup(apiGroup) |
| 60 | + s.GenericAPIServer.Handler.GoRestfulContainer.Add(discovery.NewAPIGroupHandler(s.GenericAPIServer.Serializer, apiGroup, s.GenericAPIServer.RequestContextMapper()).WebService()) |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (s *CustomMetricsAdapterServer) emAPI(groupMeta *apimachinery.GroupMeta, groupVersion *schema.GroupVersion) *specificapi.MetricsAPIGroupVersion { |
| 66 | + resourceStorage := metricstorage.NewREST(s.externalMetricsProvider) |
| 67 | + |
| 68 | + return &specificapi.MetricsAPIGroupVersion{ |
| 69 | + DynamicStorage: resourceStorage, |
| 70 | + APIGroupVersion: &genericapi.APIGroupVersion{ |
| 71 | + Root: genericapiserver.APIGroupPrefix, |
| 72 | + GroupVersion: *groupVersion, |
| 73 | + |
| 74 | + ParameterCodec: metav1.ParameterCodec, |
| 75 | + Serializer: Codecs, |
| 76 | + Creater: Scheme, |
| 77 | + Convertor: Scheme, |
| 78 | + UnsafeConvertor: runtime.UnsafeObjectConvertor(Scheme), |
| 79 | + Copier: Scheme, |
| 80 | + Typer: Scheme, |
| 81 | + Linker: groupMeta.SelfLinker, |
| 82 | + Mapper: groupMeta.RESTMapper, |
| 83 | + |
| 84 | + Context: s.GenericAPIServer.RequestContextMapper(), |
| 85 | + MinRequestTimeout: s.GenericAPIServer.MinRequestTimeout(), |
| 86 | + OptionsExternalVersion: &schema.GroupVersion{Version: "v1"}, |
| 87 | + }, |
| 88 | + ResourceLister: provider.NewExternalMetricResourceLister(s.externalMetricsProvider), |
| 89 | + Handlers: &specificapi.EMHandlers{}, |
| 90 | + } |
| 91 | +} |
0 commit comments