blob: 1cd5304c1b420a05fd930d320557e7ab62428731 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// Copyright 2018 The Kubernetes Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package generic
16
17import (
18 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19 "k8s.io/apimachinery/pkg/runtime"
20 "k8s.io/apimachinery/pkg/runtime/schema"
21 "k8s.io/apimachinery/pkg/runtime/serializer"
22 "k8s.io/apiserver/pkg/registry/rest"
23 genericapiserver "k8s.io/apiserver/pkg/server"
24 coreinf "k8s.io/client-go/informers/core/v1"
25 "k8s.io/metrics/pkg/apis/metrics"
26 "k8s.io/metrics/pkg/apis/metrics/install"
27 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
28
29 "github.com/kubernetes-incubator/metrics-server/pkg/provider"
30 nodemetricsstorage "github.com/kubernetes-incubator/metrics-server/pkg/storage/nodemetrics"
31 podmetricsstorage "github.com/kubernetes-incubator/metrics-server/pkg/storage/podmetrics"
32)
33
34var (
35 // Scheme contains the types needed by the resource metrics API.
36 Scheme = runtime.NewScheme()
37 // Codecs is a codec factory for serving the resource metrics API.
38 Codecs = serializer.NewCodecFactory(Scheme)
39)
40
41func init() {
42 install.Install(Scheme)
43 metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
44}
45
46// ProviderConfig holds the providers for node and pod metrics
47// for serving the resource metrics API.
48type ProviderConfig struct {
49 Node provider.NodeMetricsProvider
50 Pod provider.PodMetricsProvider
51}
52
53// BuildStorage constructs APIGroupInfo the metrics.k8s.io API group using the given providers.
54func BuildStorage(providers *ProviderConfig, informers coreinf.Interface) genericapiserver.APIGroupInfo {
55 apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(metrics.GroupName, Scheme, metav1.ParameterCodec, Codecs)
56
57 nodemetricsStorage := nodemetricsstorage.NewStorage(metrics.Resource("nodemetrics"), providers.Node, informers.Nodes().Lister())
58 podmetricsStorage := podmetricsstorage.NewStorage(metrics.Resource("podmetrics"), providers.Pod, informers.Pods().Lister())
59 metricsServerResources := map[string]rest.Storage{
60 "nodes": nodemetricsStorage,
61 "pods": podmetricsStorage,
62 }
63 apiGroupInfo.VersionedResourcesStorageMap[v1beta1.SchemeGroupVersion.Version] = metricsServerResources
64
65 return apiGroupInfo
66}
67
68// InstallStorage builds the storage for the metrics.k8s.io API, and then installs it into the given API server.
69func InstallStorage(providers *ProviderConfig, informers coreinf.Interface, server *genericapiserver.GenericAPIServer) error {
70 info := BuildStorage(providers, informers)
71 return server.InstallAPIGroup(&info)
72}