2015-08-17 12:18:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-10 13:10:07 +00:00
|
|
|
package podautoscaler
|
2015-08-17 12:18:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-07 10:25:04 +00:00
|
|
|
"math"
|
2015-08-17 12:18:26 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-17 22:21:55 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2015-10-09 22:04:41 +00:00
|
|
|
"k8s.io/kubernetes/pkg/apis/extensions"
|
2015-09-14 13:08:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/record"
|
2015-08-20 12:55:28 +00:00
|
|
|
client "k8s.io/kubernetes/pkg/client/unversioned"
|
2015-09-10 13:10:07 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/podautoscaler/metrics"
|
2015-08-17 12:18:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-08-20 12:55:28 +00:00
|
|
|
)
|
|
|
|
|
2015-09-07 10:25:04 +00:00
|
|
|
const (
|
|
|
|
// Usage shoud exceed the tolerance before we start downscale or upscale the pods.
|
|
|
|
// TODO: make it a flag or HPA spec element.
|
|
|
|
tolerance = 0.1
|
|
|
|
)
|
|
|
|
|
2015-09-10 13:10:07 +00:00
|
|
|
type HorizontalController struct {
|
2015-08-28 10:24:00 +00:00
|
|
|
client client.Interface
|
|
|
|
metricsClient metrics.MetricsClient
|
2015-09-14 13:08:43 +00:00
|
|
|
eventRecorder record.EventRecorder
|
2015-08-25 17:16:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 09:09:40 +00:00
|
|
|
var downscaleForbiddenWindow = 5 * time.Minute
|
|
|
|
var upscaleForbiddenWindow = 3 * time.Minute
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-09-10 13:10:07 +00:00
|
|
|
func NewHorizontalController(client client.Interface, metricsClient metrics.MetricsClient) *HorizontalController {
|
2015-09-14 13:08:43 +00:00
|
|
|
broadcaster := record.NewBroadcaster()
|
|
|
|
broadcaster.StartRecordingToSink(client.Events(""))
|
|
|
|
recorder := broadcaster.NewRecorder(api.EventSource{Component: "horizontal-pod-autoscaler"})
|
|
|
|
|
2015-09-10 13:10:07 +00:00
|
|
|
return &HorizontalController{
|
2015-08-28 10:24:00 +00:00
|
|
|
client: client,
|
|
|
|
metricsClient: metricsClient,
|
2015-09-14 13:08:43 +00:00
|
|
|
eventRecorder: recorder,
|
2015-08-17 12:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 13:10:07 +00:00
|
|
|
func (a *HorizontalController) Run(syncPeriod time.Duration) {
|
2015-08-24 01:59:15 +00:00
|
|
|
go util.Until(func() {
|
2015-08-17 12:18:26 +00:00
|
|
|
if err := a.reconcileAutoscalers(); err != nil {
|
|
|
|
glog.Errorf("Couldn't reconcile horizontal pod autoscalers: %v", err)
|
|
|
|
}
|
2015-08-24 01:59:15 +00:00
|
|
|
}, syncPeriod, util.NeverStop)
|
2015-08-17 12:18:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
func (a *HorizontalController) reconcileAutoscaler(hpa extensions.HorizontalPodAutoscaler) error {
|
2015-09-14 13:08:43 +00:00
|
|
|
reference := fmt.Sprintf("%s/%s/%s", hpa.Spec.ScaleRef.Kind, hpa.Spec.ScaleRef.Namespace, hpa.Spec.ScaleRef.Name)
|
|
|
|
|
2015-10-12 18:18:50 +00:00
|
|
|
scale, err := a.client.Extensions().Scales(hpa.Spec.ScaleRef.Namespace).Get(hpa.Spec.ScaleRef.Kind, hpa.Spec.ScaleRef.Name)
|
2015-08-17 12:18:26 +00:00
|
|
|
if err != nil {
|
2015-09-14 13:08:43 +00:00
|
|
|
a.eventRecorder.Event(&hpa, "FailedGetScale", err.Error())
|
|
|
|
return fmt.Errorf("failed to query scale subresource for %s: %v", reference, err)
|
2015-08-17 12:18:26 +00:00
|
|
|
}
|
2015-09-14 13:08:43 +00:00
|
|
|
currentReplicas := scale.Status.Replicas
|
2015-09-14 08:14:32 +00:00
|
|
|
currentConsumption, err := a.metricsClient.
|
|
|
|
ResourceConsumption(hpa.Spec.ScaleRef.Namespace).
|
|
|
|
Get(hpa.Spec.Target.Resource, scale.Status.Selector)
|
2015-08-20 12:55:28 +00:00
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
// TODO: what to do on partial errors (like metrics obtained for 75% of pods).
|
|
|
|
if err != nil {
|
|
|
|
a.eventRecorder.Event(&hpa, "FailedGetMetrics", err.Error())
|
|
|
|
return fmt.Errorf("failed to get metrics for %s: %v", reference, err)
|
|
|
|
}
|
2015-08-20 12:55:28 +00:00
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
usageRatio := float64(currentConsumption.Quantity.MilliValue()) / float64(hpa.Spec.Target.Quantity.MilliValue())
|
|
|
|
desiredReplicas := int(math.Ceil(usageRatio * float64(currentReplicas)))
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-09-17 12:08:39 +00:00
|
|
|
if desiredReplicas < hpa.Spec.MinReplicas {
|
|
|
|
desiredReplicas = hpa.Spec.MinReplicas
|
2015-09-14 13:08:43 +00:00
|
|
|
}
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
// TODO: remove when pod ideling is done.
|
|
|
|
if desiredReplicas == 0 {
|
|
|
|
desiredReplicas = 1
|
|
|
|
}
|
2015-09-07 10:25:04 +00:00
|
|
|
|
2015-09-17 12:08:39 +00:00
|
|
|
if desiredReplicas > hpa.Spec.MaxReplicas {
|
|
|
|
desiredReplicas = hpa.Spec.MaxReplicas
|
2015-09-14 13:08:43 +00:00
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
rescale := false
|
|
|
|
|
|
|
|
if desiredReplicas != currentReplicas {
|
|
|
|
// Going down only if the usageRatio dropped significantly below the target
|
|
|
|
// and there was no rescaling in the last downscaleForbiddenWindow.
|
|
|
|
if desiredReplicas < currentReplicas && usageRatio < (1-tolerance) &&
|
2015-09-29 12:25:46 +00:00
|
|
|
(hpa.Status.LastScaleTimestamp == nil ||
|
2015-09-14 13:08:43 +00:00
|
|
|
hpa.Status.LastScaleTimestamp.Add(downscaleForbiddenWindow).Before(now)) {
|
|
|
|
rescale = true
|
2015-09-07 10:25:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
// Going up only if the usage ratio increased significantly above the target
|
|
|
|
// and there was no rescaling in the last upscaleForbiddenWindow.
|
|
|
|
if desiredReplicas > currentReplicas && usageRatio > (1+tolerance) &&
|
2015-09-29 12:25:46 +00:00
|
|
|
(hpa.Status.LastScaleTimestamp == nil ||
|
2015-09-14 13:08:43 +00:00
|
|
|
hpa.Status.LastScaleTimestamp.Add(upscaleForbiddenWindow).Before(now)) {
|
|
|
|
rescale = true
|
2015-09-07 10:25:04 +00:00
|
|
|
}
|
2015-09-14 13:08:43 +00:00
|
|
|
}
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
if rescale {
|
|
|
|
scale.Spec.Replicas = desiredReplicas
|
2015-10-12 18:18:50 +00:00
|
|
|
_, err = a.client.Extensions().Scales(hpa.Namespace).Update(hpa.Spec.ScaleRef.Kind, scale)
|
2015-09-14 13:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
a.eventRecorder.Eventf(&hpa, "FailedRescale", "New size: %d; error: %v", desiredReplicas, err.Error())
|
|
|
|
return fmt.Errorf("failed to rescale %s: %v", reference, err)
|
2015-08-25 17:16:47 +00:00
|
|
|
}
|
2015-09-14 13:08:43 +00:00
|
|
|
a.eventRecorder.Eventf(&hpa, "SuccessfulRescale", "New size: %d", desiredReplicas)
|
2015-10-05 14:26:14 +00:00
|
|
|
glog.Infof("Successfull rescale of %s, old size: %d, new size: %d, usage ratio: %f",
|
|
|
|
hpa.Name, currentReplicas, desiredReplicas, usageRatio)
|
2015-09-14 13:08:43 +00:00
|
|
|
} else {
|
|
|
|
desiredReplicas = currentReplicas
|
|
|
|
}
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-10-09 22:49:10 +00:00
|
|
|
hpa.Status = extensions.HorizontalPodAutoscalerStatus{
|
2015-09-14 13:08:43 +00:00
|
|
|
CurrentReplicas: currentReplicas,
|
|
|
|
DesiredReplicas: desiredReplicas,
|
|
|
|
CurrentConsumption: currentConsumption,
|
|
|
|
}
|
|
|
|
if rescale {
|
2015-09-17 22:21:55 +00:00
|
|
|
now := unversioned.NewTime(now)
|
2015-09-14 13:08:43 +00:00
|
|
|
hpa.Status.LastScaleTimestamp = &now
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:18:50 +00:00
|
|
|
_, err = a.client.Extensions().HorizontalPodAutoscalers(hpa.Namespace).UpdateStatus(&hpa)
|
2015-09-14 13:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
a.eventRecorder.Event(&hpa, "FailedUpdateStatus", err.Error())
|
|
|
|
return fmt.Errorf("failed to update status for %s: %v", hpa.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-25 17:16:47 +00:00
|
|
|
|
2015-09-14 13:08:43 +00:00
|
|
|
func (a *HorizontalController) reconcileAutoscalers() error {
|
|
|
|
ns := api.NamespaceAll
|
2015-10-12 18:18:50 +00:00
|
|
|
list, err := a.client.Extensions().HorizontalPodAutoscalers(ns).List(labels.Everything(), fields.Everything())
|
2015-09-14 13:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error listing nodes: %v", err)
|
|
|
|
}
|
|
|
|
for _, hpa := range list.Items {
|
|
|
|
err := a.reconcileAutoscaler(hpa)
|
2015-08-25 17:16:47 +00:00
|
|
|
if err != nil {
|
2015-09-14 13:08:43 +00:00
|
|
|
glog.Warningf("Failed to reconcile %s: %v", hpa.Name, err)
|
2015-08-25 17:16:47 +00:00
|
|
|
}
|
2015-08-20 12:55:28 +00:00
|
|
|
}
|
2015-08-17 12:18:26 +00:00
|
|
|
return nil
|
|
|
|
}
|