blob: fa22c10cff107f5e2c97d3a758db2ea3a484ce38 [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 apiserver
16
17import (
18 "strings"
19
20 openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
21 genericapiserver "k8s.io/apiserver/pkg/server"
22 "k8s.io/client-go/informers"
23
24 "github.com/kubernetes-incubator/metrics-server/pkg/apiserver/generic"
25 generatedopenapi "github.com/kubernetes-incubator/metrics-server/pkg/generated/openapi"
26 "github.com/kubernetes-incubator/metrics-server/pkg/version"
27)
28
29// Config contains configuration for launching an instance of metrics-server.
30type Config struct {
31 GenericConfig *genericapiserver.Config
32 ProviderConfig generic.ProviderConfig
33}
34
35type completedConfig struct {
36 genericapiserver.CompletedConfig
37 ProviderConfig *generic.ProviderConfig
38}
39
40// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
41func (c *Config) Complete(informers informers.SharedInformerFactory) completedConfig {
42 c.GenericConfig.Version = version.VersionInfo()
43
44 // enable OpenAPI schemas
45 c.GenericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(generatedopenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(generic.Scheme))
46 c.GenericConfig.OpenAPIConfig.Info.Title = "Kubernetes metrics-server"
47 c.GenericConfig.OpenAPIConfig.Info.Version = strings.Split(c.GenericConfig.Version.String(), "-")[0] // TODO(directxman12): remove this once autosetting this doesn't require security definitions
48 c.GenericConfig.SwaggerConfig = genericapiserver.DefaultSwaggerConfig()
49
50 return completedConfig{
51 CompletedConfig: c.GenericConfig.Complete(informers),
52 ProviderConfig: &c.ProviderConfig,
53 }
54}
55
56type MetricsServer struct {
57 *genericapiserver.GenericAPIServer
58}
59
60// New returns a new instance of MetricsServer from the given config.
61func (c completedConfig) New() (*MetricsServer, error) {
62 genericServer, err := c.CompletedConfig.New("metrics-server", genericapiserver.NewEmptyDelegate()) // completion is done in Complete, no need for a second time
63 if err != nil {
64 return nil, err
65 }
66
67 if err := generic.InstallStorage(c.ProviderConfig, c.SharedInformerFactory.Core().V1(), genericServer); err != nil {
68 return nil, err
69 }
70
71 return &MetricsServer{
72 GenericAPIServer: genericServer,
73 }, nil
74}