1/*
2Copyright 2016 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
17// Package app implements a server that runs a set of active
18// components.  This includes replication controllers, service endpoints and
19// nodes.
20//
21package app
22
23import (
24	"net/http"
25
26	"k8s.io/apimachinery/pkg/runtime/schema"
27	"k8s.io/client-go/dynamic"
28	"k8s.io/client-go/scale"
29	"k8s.io/kubernetes/pkg/controller/podautoscaler"
30	"k8s.io/kubernetes/pkg/controller/podautoscaler/metrics"
31
32	resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
33	"k8s.io/metrics/pkg/client/custom_metrics"
34	"k8s.io/metrics/pkg/client/external_metrics"
35)
36
37func startHPAController(ctx ControllerContext) (http.Handler, bool, error) {
38	if !ctx.AvailableResources[schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscalers"}] {
39		return nil, false, nil
40	}
41
42	return startHPAControllerWithRESTClient(ctx)
43}
44
45func startHPAControllerWithRESTClient(ctx ControllerContext) (http.Handler, bool, error) {
46	clientConfig := ctx.ClientBuilder.ConfigOrDie("horizontal-pod-autoscaler")
47	hpaClient := ctx.ClientBuilder.ClientOrDie("horizontal-pod-autoscaler")
48
49	apiVersionsGetter := custom_metrics.NewAvailableAPIsGetter(hpaClient.Discovery())
50	// invalidate the discovery information roughly once per resync interval our API
51	// information is *at most* two resync intervals old.
52	go custom_metrics.PeriodicallyInvalidate(
53		apiVersionsGetter,
54		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerSyncPeriod.Duration,
55		ctx.Stop)
56
57	metricsClient := metrics.NewRESTMetricsClient(
58		resourceclient.NewForConfigOrDie(clientConfig),
59		custom_metrics.NewForConfig(clientConfig, ctx.RESTMapper, apiVersionsGetter),
60		external_metrics.NewForConfigOrDie(clientConfig),
61	)
62	return startHPAControllerWithMetricsClient(ctx, metricsClient)
63}
64
65func startHPAControllerWithMetricsClient(ctx ControllerContext, metricsClient metrics.MetricsClient) (http.Handler, bool, error) {
66	hpaClient := ctx.ClientBuilder.ClientOrDie("horizontal-pod-autoscaler")
67	hpaClientConfig := ctx.ClientBuilder.ConfigOrDie("horizontal-pod-autoscaler")
68
69	// we don't use cached discovery because DiscoveryScaleKindResolver does its own caching,
70	// so we want to re-fetch every time when we actually ask for it
71	scaleKindResolver := scale.NewDiscoveryScaleKindResolver(hpaClient.Discovery())
72	scaleClient, err := scale.NewForConfig(hpaClientConfig, ctx.RESTMapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
73	if err != nil {
74		return nil, false, err
75	}
76
77	go podautoscaler.NewHorizontalController(
78		hpaClient.CoreV1(),
79		scaleClient,
80		hpaClient.AutoscalingV1(),
81		ctx.RESTMapper,
82		metricsClient,
83		ctx.InformerFactory.Autoscaling().V1().HorizontalPodAutoscalers(),
84		ctx.InformerFactory.Core().V1().Pods(),
85		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerSyncPeriod.Duration,
86		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerDownscaleStabilizationWindow.Duration,
87		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerTolerance,
88		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerCPUInitializationPeriod.Duration,
89		ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerInitialReadinessDelay.Duration,
90	).Run(ctx.Stop)
91	return nil, true, nil
92}
93