autoscaling.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package app implements a server that runs a set of active
  14. // components. This includes replication controllers, service endpoints and
  15. // nodes.
  16. //
  17. package app
  18. import (
  19. "net/http"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/client-go/dynamic"
  22. "k8s.io/client-go/scale"
  23. "k8s.io/kubernetes/pkg/controller/podautoscaler"
  24. "k8s.io/kubernetes/pkg/controller/podautoscaler/metrics"
  25. resourceclient "k8s.io/metrics/pkg/client/clientset/versioned/typed/metrics/v1beta1"
  26. "k8s.io/metrics/pkg/client/custom_metrics"
  27. "k8s.io/metrics/pkg/client/external_metrics"
  28. )
  29. func startHPAController(ctx ControllerContext) (http.Handler, bool, error) {
  30. if !ctx.AvailableResources[schema.GroupVersionResource{Group: "autoscaling", Version: "v1", Resource: "horizontalpodautoscalers"}] {
  31. return nil, false, nil
  32. }
  33. if ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerUseRESTClients {
  34. // use the new-style clients if support for custom metrics is enabled
  35. return startHPAControllerWithRESTClient(ctx)
  36. }
  37. return startHPAControllerWithLegacyClient(ctx)
  38. }
  39. func startHPAControllerWithRESTClient(ctx ControllerContext) (http.Handler, bool, error) {
  40. clientConfig := ctx.ClientBuilder.ConfigOrDie("horizontal-pod-autoscaler")
  41. hpaClient := ctx.ClientBuilder.ClientOrDie("horizontal-pod-autoscaler")
  42. apiVersionsGetter := custom_metrics.NewAvailableAPIsGetter(hpaClient.Discovery())
  43. // invalidate the discovery information roughly once per resync interval our API
  44. // information is *at most* two resync intervals old.
  45. go custom_metrics.PeriodicallyInvalidate(
  46. apiVersionsGetter,
  47. ctx.ComponentConfig.HPAController.HorizontalPodAutoscalerSyncPeriod.Duration,
  48. ctx.Stop)
  49. metricsClient := metrics.NewRESTMetricsClient(
  50. resourceclient.NewForConfigOrDie(clientConfig),
  51. custom_metrics.NewForConfig(clientConfig, ctx.RESTMapper, apiVersionsGetter),
  52. external_metrics.NewForConfigOrDie(clientConfig),
  53. )
  54. return startHPAControllerWithMetricsClient(ctx, metricsClient)
  55. }
  56. func startHPAControllerWithLegacyClient(ctx ControllerContext) (http.Handler, bool, error) {
  57. hpaClient := ctx.ClientBuilder.ClientOrDie("horizontal-pod-autoscaler")
  58. metricsClient := metrics.NewHeapsterMetricsClient(
  59. hpaClient,
  60. metrics.DefaultHeapsterNamespace,
  61. metrics.DefaultHeapsterScheme,
  62. metrics.DefaultHeapsterService,
  63. metrics.DefaultHeapsterPort,
  64. )
  65. return startHPAControllerWithMetricsClient(ctx, metricsClient)
  66. }
  67. func startHPAControllerWithMetricsClient(ctx ControllerContext, metricsClient metrics.MetricsClient) (http.Handler, bool, error) {
  68. hpaClient := ctx.ClientBuilder.ClientOrDie("horizontal-pod-autoscaler")
  69. hpaClientConfig := ctx.ClientBuilder.ConfigOrDie("horizontal-pod-autoscaler")
  70. // we don't use cached discovery because DiscoveryScaleKindResolver does its own caching,
  71. // so we want to re-fetch every time when we actually ask for it
  72. scaleKindResolver := scale.NewDiscoveryScaleKindResolver(hpaClient.Discovery())
  73. scaleClient, err := scale.NewForConfig(hpaClientConfig, ctx.RESTMapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
  74. if err != nil {
  75. return nil, false, err
  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. }