2015-08-17 12:18:26 +00:00
/ *
2016-06-03 00:25:58 +00:00
Copyright 2015 The Kubernetes Authors .
2015-08-17 12:18:26 +00:00
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 (
2016-01-29 11:20:19 +00:00
"encoding/json"
2015-08-17 12:18:26 +00:00
"fmt"
2015-09-07 10:25:04 +00:00
"math"
2015-08-17 12:18:26 +00:00
"time"
"github.com/golang/glog"
2016-01-29 11:20:19 +00:00
"k8s.io/kubernetes/pkg/api/resource"
2016-11-18 20:50:17 +00:00
"k8s.io/kubernetes/pkg/api/v1"
autoscaling "k8s.io/kubernetes/pkg/apis/autoscaling/v1"
2016-12-09 01:24:08 +00:00
extensionsv1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
2016-12-03 19:06:03 +00:00
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
2016-03-02 08:29:17 +00:00
"k8s.io/kubernetes/pkg/client/cache"
2016-12-14 01:18:17 +00:00
unversionedautoscaling "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/autoscaling/v1"
v1core "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/core/v1"
unversionedextensions "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/extensions/v1beta1"
2015-09-14 13:08:43 +00:00
"k8s.io/kubernetes/pkg/client/record"
2016-03-02 08:29:17 +00:00
"k8s.io/kubernetes/pkg/runtime"
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
"k8s.io/kubernetes/pkg/watch"
2015-08-20 12:55:28 +00:00
)
2015-12-13 08:54:43 +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
2016-01-29 11:20:19 +00:00
2016-02-29 11:02:54 +00:00
defaultTargetCPUUtilizationPercentage = 80
2016-02-05 10:31:51 +00:00
HpaCustomMetricsTargetAnnotationName = "alpha/target.custom-metrics.podautoscaler.kubernetes.io"
HpaCustomMetricsStatusAnnotationName = "alpha/status.custom-metrics.podautoscaler.kubernetes.io"
2016-10-17 15:14:15 +00:00
scaleUpLimitFactor = 2
scaleUpLimitMinimum = 4
2015-12-13 08:54:43 +00:00
)
2016-10-17 15:14:15 +00:00
func calculateScaleUpLimit ( currentReplicas int32 ) int32 {
return int32 ( math . Max ( scaleUpLimitFactor * float64 ( currentReplicas ) , scaleUpLimitMinimum ) )
}
2015-09-10 13:10:07 +00:00
type HorizontalController struct {
2016-02-16 17:54:53 +00:00
scaleNamespacer unversionedextensions . ScalesGetter
2016-05-05 10:27:24 +00:00
hpaNamespacer unversionedautoscaling . HorizontalPodAutoscalersGetter
2015-11-02 15:18:53 +00:00
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
replicaCalc * ReplicaCalculator
2015-12-13 08:54:43 +00:00
eventRecorder record . EventRecorder
2016-03-02 08:29:17 +00:00
// A store of HPA objects, populated by the controller.
store cache . Store
// Watches changes to all HPA objects.
2016-09-14 18:35:38 +00:00
controller * cache . Controller
2015-08-25 17:16:47 +00:00
}
2015-12-13 08:54:43 +00:00
var downscaleForbiddenWindow = 5 * time . Minute
var upscaleForbiddenWindow = 3 * time . Minute
2016-09-14 18:35:38 +00:00
func newInformer ( controller * HorizontalController , resyncPeriod time . Duration ) ( cache . Store , * cache . Controller ) {
return cache . NewInformer (
2016-03-02 08:29:17 +00:00
& cache . ListWatch {
2016-11-18 20:50:17 +00:00
ListFunc : func ( options v1 . ListOptions ) ( runtime . Object , error ) {
return controller . hpaNamespacer . HorizontalPodAutoscalers ( v1 . NamespaceAll ) . List ( options )
2016-03-02 08:29:17 +00:00
} ,
2016-11-18 20:50:17 +00:00
WatchFunc : func ( options v1 . ListOptions ) ( watch . Interface , error ) {
return controller . hpaNamespacer . HorizontalPodAutoscalers ( v1 . NamespaceAll ) . Watch ( options )
2016-03-02 08:29:17 +00:00
} ,
} ,
2016-05-05 10:27:24 +00:00
& autoscaling . HorizontalPodAutoscaler { } ,
2016-03-02 08:29:17 +00:00
resyncPeriod ,
2016-09-14 18:35:38 +00:00
cache . ResourceEventHandlerFuncs {
2016-03-02 08:29:17 +00:00
AddFunc : func ( obj interface { } ) {
2016-05-05 10:27:24 +00:00
hpa := obj . ( * autoscaling . HorizontalPodAutoscaler )
hasCPUPolicy := hpa . Spec . TargetCPUUtilizationPercentage != nil
2016-03-02 09:08:17 +00:00
_ , hasCustomMetricsPolicy := hpa . Annotations [ HpaCustomMetricsTargetAnnotationName ]
if ! hasCPUPolicy && ! hasCustomMetricsPolicy {
2016-11-18 20:50:17 +00:00
controller . eventRecorder . Event ( hpa , v1 . EventTypeNormal , "DefaultPolicy" , "No scaling policy specified - will use default one. See documentation for details" )
2016-03-02 09:08:17 +00:00
}
2016-03-02 08:29:17 +00:00
err := controller . reconcileAutoscaler ( hpa )
if err != nil {
glog . Warningf ( "Failed to reconcile %s: %v" , hpa . Name , err )
}
} ,
UpdateFunc : func ( old , cur interface { } ) {
2016-05-05 10:27:24 +00:00
hpa := cur . ( * autoscaling . HorizontalPodAutoscaler )
2016-03-02 08:29:17 +00:00
err := controller . reconcileAutoscaler ( hpa )
if err != nil {
glog . Warningf ( "Failed to reconcile %s: %v" , hpa . Name , err )
}
} ,
// We are not interested in deletions.
} ,
)
2016-03-03 10:48:07 +00:00
}
2016-11-18 20:50:17 +00:00
func NewHorizontalController ( evtNamespacer v1core . EventsGetter , scaleNamespacer unversionedextensions . ScalesGetter , hpaNamespacer unversionedautoscaling . HorizontalPodAutoscalersGetter , replicaCalc * ReplicaCalculator , resyncPeriod time . Duration ) * HorizontalController {
2016-03-03 10:48:07 +00:00
broadcaster := record . NewBroadcaster ( )
2016-11-18 20:50:17 +00:00
broadcaster . StartRecordingToSink ( & v1core . EventSinkImpl { Interface : evtNamespacer . Events ( "" ) } )
recorder := broadcaster . NewRecorder ( v1 . EventSource { Component : "horizontal-pod-autoscaler" } )
2016-03-03 10:48:07 +00:00
controller := & HorizontalController {
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
replicaCalc : replicaCalc ,
2016-03-03 10:48:07 +00:00
eventRecorder : recorder ,
scaleNamespacer : scaleNamespacer ,
hpaNamespacer : hpaNamespacer ,
}
store , frameworkController := newInformer ( controller , resyncPeriod )
controller . store = store
controller . controller = frameworkController
2016-03-02 08:29:17 +00:00
return controller
2015-08-17 12:18:26 +00:00
}
2016-03-02 08:29:17 +00:00
func ( a * HorizontalController ) Run ( stopCh <- chan struct { } ) {
defer utilruntime . HandleCrash ( )
glog . Infof ( "Starting HPA Controller" )
go a . controller . Run ( stopCh )
<- stopCh
glog . Infof ( "Shutting down HPA Controller" )
2015-08-17 12:18:26 +00:00
}
2016-11-04 14:38:06 +00:00
// getLastScaleTime returns the hpa's last scale time or the hpa's creation time if the last scale time is nil.
func getLastScaleTime ( hpa * autoscaling . HorizontalPodAutoscaler ) time . Time {
lastScaleTime := hpa . Status . LastScaleTime
if lastScaleTime == nil {
lastScaleTime = & hpa . CreationTimestamp
}
return lastScaleTime . Time
}
2016-12-09 01:24:08 +00:00
func ( a * HorizontalController ) computeReplicasForCPUUtilization ( hpa * autoscaling . HorizontalPodAutoscaler , scale * extensionsv1beta1 . Scale ) ( int32 , * int32 , time . Time , error ) {
2016-04-27 04:35:14 +00:00
targetUtilization := int32 ( defaultTargetCPUUtilizationPercentage )
2016-05-05 10:27:24 +00:00
if hpa . Spec . TargetCPUUtilizationPercentage != nil {
targetUtilization = * hpa . Spec . TargetCPUUtilizationPercentage
2015-10-13 15:24:23 +00:00
}
currentReplicas := scale . Status . Replicas
2016-03-09 00:27:13 +00:00
if scale . Status . Selector == nil {
errMsg := "selector is required"
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "SelectorRequired" , errMsg )
2016-03-09 00:27:13 +00:00
return 0 , nil , time . Time { } , fmt . Errorf ( errMsg )
}
2016-12-03 18:57:26 +00:00
selector , err := metav1 . LabelSelectorAsSelector ( & metav1 . LabelSelector { MatchLabels : scale . Status . Selector } )
2016-03-09 00:27:13 +00:00
if err != nil {
errMsg := fmt . Sprintf ( "couldn't convert selector string to a corresponding selector object: %v" , err )
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "InvalidSelector" , errMsg )
2016-03-09 00:27:13 +00:00
return 0 , nil , time . Time { } , fmt . Errorf ( errMsg )
}
2015-10-13 15:24:23 +00:00
2016-11-18 20:50:17 +00:00
desiredReplicas , utilization , timestamp , err := a . replicaCalc . GetResourceReplicas ( currentReplicas , targetUtilization , v1 . ResourceCPU , hpa . Namespace , selector )
2015-10-13 15:24:23 +00:00
if err != nil {
2016-11-04 14:38:06 +00:00
lastScaleTime := getLastScaleTime ( hpa )
if time . Now ( ) . After ( lastScaleTime . Add ( upscaleForbiddenWindow ) ) {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedGetMetrics" , err . Error ( ) )
2016-11-04 14:38:06 +00:00
} else {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeNormal , "MetricsNotAvailableYet" , err . Error ( ) )
2016-11-04 14:38:06 +00:00
}
2016-03-02 09:08:17 +00:00
return 0 , nil , time . Time { } , fmt . Errorf ( "failed to get CPU utilization: %v" , err )
2015-10-13 15:24:23 +00:00
}
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
if desiredReplicas != currentReplicas {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Eventf ( hpa , v1 . EventTypeNormal , "DesiredReplicasComputed" ,
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
"Computed the desired num of replicas: %d (avgCPUutil: %d, current replicas: %d)" ,
desiredReplicas , utilization , scale . Status . Replicas )
2015-10-13 15:24:23 +00:00
}
2016-07-11 06:42:51 +00:00
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
return desiredReplicas , & utilization , timestamp , nil
2015-10-13 15:24:23 +00:00
}
2016-11-04 14:38:06 +00:00
// computeReplicasForCustomMetrics computes the desired number of replicas based on the CustomMetrics passed in cmAnnotation
// as json-serialized extensions.CustomMetricsTargetList.
2016-03-02 09:08:17 +00:00
// Returns number of replicas, metric which required highest number of replicas,
// status string (also json-serialized extensions.CustomMetricsCurrentStatusList),
2016-01-29 11:20:19 +00:00
// last timestamp of the metrics involved in computations or error, if occurred.
2016-12-09 01:24:08 +00:00
func ( a * HorizontalController ) computeReplicasForCustomMetrics ( hpa * autoscaling . HorizontalPodAutoscaler , scale * extensionsv1beta1 . Scale ,
2016-04-27 04:35:14 +00:00
cmAnnotation string ) ( replicas int32 , metric string , status string , timestamp time . Time , err error ) {
2016-01-29 11:20:19 +00:00
if cmAnnotation == "" {
2016-03-02 09:08:17 +00:00
return
2016-01-29 11:20:19 +00:00
}
2016-07-11 06:42:51 +00:00
currentReplicas := scale . Status . Replicas
2016-12-09 01:24:08 +00:00
var targetList extensionsv1beta1 . CustomMetricTargetList
2016-01-29 11:20:19 +00:00
if err := json . Unmarshal ( [ ] byte ( cmAnnotation ) , & targetList ) ; err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedParseCustomMetricsAnnotation" , err . Error ( ) )
2016-03-02 09:08:17 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "failed to parse custom metrics annotation: %v" , err )
2016-01-29 11:20:19 +00:00
}
if len ( targetList . Items ) == 0 {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "NoCustomMetricsInAnnotation" , err . Error ( ) )
2016-03-02 09:08:17 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "no custom metrics in annotation" )
2016-01-29 11:20:19 +00:00
}
2016-12-09 01:24:08 +00:00
statusList := extensionsv1beta1 . CustomMetricCurrentStatusList {
Items : make ( [ ] extensionsv1beta1 . CustomMetricCurrentStatus , 0 ) ,
2016-01-29 11:20:19 +00:00
}
for _ , customMetricTarget := range targetList . Items {
2016-03-09 00:27:13 +00:00
if scale . Status . Selector == nil {
errMsg := "selector is required"
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "SelectorRequired" , errMsg )
2016-03-09 00:27:13 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "selector is required" )
}
2016-12-03 18:57:26 +00:00
selector , err := metav1 . LabelSelectorAsSelector ( & metav1 . LabelSelector { MatchLabels : scale . Status . Selector } )
2016-03-09 00:27:13 +00:00
if err != nil {
errMsg := fmt . Sprintf ( "couldn't convert selector string to a corresponding selector object: %v" , err )
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "InvalidSelector" , errMsg )
2016-03-09 00:27:13 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "couldn't convert selector string to a corresponding selector object: %v" , err )
}
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
floatTarget := float64 ( customMetricTarget . TargetValue . MilliValue ( ) ) / 1000.0
replicaCountProposal , utilizationProposal , timestampProposal , err := a . replicaCalc . GetMetricReplicas ( currentReplicas , floatTarget , fmt . Sprintf ( "custom/%s" , customMetricTarget . Name ) , hpa . Namespace , selector )
2016-01-29 11:20:19 +00:00
if err != nil {
2016-11-04 14:38:06 +00:00
lastScaleTime := getLastScaleTime ( hpa )
if time . Now ( ) . After ( lastScaleTime . Add ( upscaleForbiddenWindow ) ) {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedGetCustomMetrics" , err . Error ( ) )
2016-11-04 14:38:06 +00:00
} else {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeNormal , "CustomMetricsNotAvailableYet" , err . Error ( ) )
2016-11-04 14:38:06 +00:00
}
2016-03-02 09:08:17 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "failed to get custom metric value: %v" , err )
2016-01-29 11:20:19 +00:00
}
if replicaCountProposal > replicas {
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
timestamp = timestampProposal
2016-01-29 11:20:19 +00:00
replicas = replicaCountProposal
2016-03-02 09:08:17 +00:00
metric = fmt . Sprintf ( "Custom metric %s" , customMetricTarget . Name )
2016-01-29 11:20:19 +00:00
}
HPA: Consider unready pods and missing metrics
Currently, the HPA considers unready pods the same as ready pods when
looking at their CPU and custom metric usage. However, pods frequently
use extra CPU during initialization, so we want to consider them
separately.
This commit causes the HPA to consider unready pods as having 0 CPU
usage when scaling up, and ignores them when scaling down. If, when
scaling up, factoring the unready pods as having 0 CPU would cause a
downscale instead, we simply choose not to scale. Otherwise, we simply
scale up at the reduced amount caculated by factoring the pods in at
zero CPU usage.
The effect is that unready pods cause the autoscaler to be a bit more
conservative -- large increases in CPU usage can still cause scales,
even with unready pods in the mix, but will not cause the scale factors
to be as large, in anticipation of the new pods later becoming ready and
handling load.
Similarly, if there are pods for which no metrics have been retrieved,
these pods are treated as having 100% of the requested metric when
scaling down, and 0% when scaling up. As above, this cannot change the
direction of the scale.
This commit also changes the HPA to ignore superfluous metrics -- as
long as metrics for all ready pods are present, the HPA we make scaling
decisions. Currently, this only works for CPU. For custom metrics, we
cannot identify which metrics go to which pods if we get superfluous
metrics, so we abort the scale.
2016-09-27 18:47:52 +00:00
quantity , err := resource . ParseQuantity ( fmt . Sprintf ( "%.3f" , utilizationProposal ) )
2016-01-29 11:20:19 +00:00
if err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedSetCustomMetrics" , err . Error ( ) )
2016-03-02 09:08:17 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "failed to set custom metric value: %v" , err )
2016-01-29 11:20:19 +00:00
}
2016-12-09 01:24:08 +00:00
statusList . Items = append ( statusList . Items , extensionsv1beta1 . CustomMetricCurrentStatus {
2016-01-29 11:20:19 +00:00
Name : customMetricTarget . Name ,
2016-05-17 04:36:56 +00:00
CurrentValue : quantity ,
2016-01-29 11:20:19 +00:00
} )
}
byteStatusList , err := json . Marshal ( statusList )
if err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedSerializeCustomMetrics" , err . Error ( ) )
2016-03-02 09:08:17 +00:00
return 0 , "" , "" , time . Time { } , fmt . Errorf ( "failed to serialize custom metric status: %v" , err )
2016-01-29 11:20:19 +00:00
}
2016-11-08 12:11:08 +00:00
if replicas != currentReplicas {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Eventf ( hpa , v1 . EventTypeNormal , "DesiredReplicasComputedCustomMetric" ,
2016-11-08 12:11:08 +00:00
"Computed the desired num of replicas: %d, metric: %s, current replicas: %d" ,
2016-11-18 20:50:17 +00:00
func ( ) * int32 { i := int32 ( replicas ) ; return & i } ( ) , metric , scale . Status . Replicas )
2016-11-08 12:11:08 +00:00
}
2016-03-02 09:08:17 +00:00
return replicas , metric , string ( byteStatusList ) , timestamp , nil
2016-01-29 11:20:19 +00:00
}
2016-05-05 10:27:24 +00:00
func ( a * HorizontalController ) reconcileAutoscaler ( hpa * autoscaling . HorizontalPodAutoscaler ) error {
reference := fmt . Sprintf ( "%s/%s/%s" , hpa . Spec . ScaleTargetRef . Kind , hpa . Namespace , hpa . Spec . ScaleTargetRef . Name )
2015-09-14 13:08:43 +00:00
2016-05-05 10:27:24 +00:00
scale , err := a . scaleNamespacer . Scales ( hpa . Namespace ) . Get ( hpa . Spec . ScaleTargetRef . Kind , hpa . Spec . ScaleTargetRef . Name )
2015-08-17 12:18:26 +00:00
if err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedGetScale" , err . Error ( ) )
2015-09-14 13:08:43 +00:00
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-08-20 12:55:28 +00:00
2016-04-27 04:35:14 +00:00
cpuDesiredReplicas := int32 ( 0 )
2016-07-11 06:42:51 +00:00
cpuCurrentUtilization := new ( int32 )
2016-01-29 11:20:19 +00:00
cpuTimestamp := time . Time { }
2016-04-27 04:35:14 +00:00
cmDesiredReplicas := int32 ( 0 )
2016-03-02 09:08:17 +00:00
cmMetric := ""
2016-01-29 11:20:19 +00:00
cmStatus := ""
cmTimestamp := time . Time { }
2016-04-27 04:35:14 +00:00
desiredReplicas := int32 ( 0 )
2016-03-02 09:08:17 +00:00
rescaleReason := ""
2016-02-12 15:26:59 +00:00
timestamp := time . Now ( )
2016-01-29 11:20:19 +00:00
2016-11-21 09:32:00 +00:00
rescale := true
2016-07-19 15:54:38 +00:00
if scale . Spec . Replicas == 0 {
// Autoscaling is disabled for this resource
desiredReplicas = 0
2016-11-21 09:32:00 +00:00
rescale = false
2016-07-19 15:54:38 +00:00
} else if currentReplicas > hpa . Spec . MaxReplicas {
2016-03-02 09:08:17 +00:00
rescaleReason = "Current number of replicas above Spec.MaxReplicas"
2016-02-12 15:26:59 +00:00
desiredReplicas = hpa . Spec . MaxReplicas
} else if hpa . Spec . MinReplicas != nil && currentReplicas < * hpa . Spec . MinReplicas {
2016-03-02 09:08:17 +00:00
rescaleReason = "Current number of replicas below Spec.MinReplicas"
2016-02-12 15:26:59 +00:00
desiredReplicas = * hpa . Spec . MinReplicas
} else if currentReplicas == 0 {
2016-03-02 09:08:17 +00:00
rescaleReason = "Current number of replicas must be greater than 0"
2016-02-12 15:26:59 +00:00
desiredReplicas = 1
} else {
// All basic scenarios covered, the state should be sane, lets use metrics.
2016-02-29 11:02:54 +00:00
cmAnnotation , cmAnnotationFound := hpa . Annotations [ HpaCustomMetricsTargetAnnotationName ]
2016-02-12 15:26:59 +00:00
2016-05-05 10:27:24 +00:00
if hpa . Spec . TargetCPUUtilizationPercentage != nil || ! cmAnnotationFound {
2016-02-12 15:26:59 +00:00
cpuDesiredReplicas , cpuCurrentUtilization , cpuTimestamp , err = a . computeReplicasForCPUUtilization ( hpa , scale )
if err != nil {
2016-02-23 12:05:07 +00:00
a . updateCurrentReplicasInStatus ( hpa , currentReplicas )
2016-02-12 15:26:59 +00:00
return fmt . Errorf ( "failed to compute desired number of replicas based on CPU utilization for %s: %v" , reference , err )
}
2016-01-29 11:20:19 +00:00
}
2016-02-29 11:02:54 +00:00
if cmAnnotationFound {
2016-03-02 09:08:17 +00:00
cmDesiredReplicas , cmMetric , cmStatus , cmTimestamp , err = a . computeReplicasForCustomMetrics ( hpa , scale , cmAnnotation )
2016-02-12 15:26:59 +00:00
if err != nil {
2016-02-23 12:05:07 +00:00
a . updateCurrentReplicasInStatus ( hpa , currentReplicas )
2016-02-12 15:26:59 +00:00
return fmt . Errorf ( "failed to compute desired number of replicas based on Custom Metrics for %s: %v" , reference , err )
}
}
2016-01-29 11:20:19 +00:00
2016-03-02 09:08:17 +00:00
rescaleMetric := ""
2016-02-12 15:26:59 +00:00
if cpuDesiredReplicas > desiredReplicas {
desiredReplicas = cpuDesiredReplicas
timestamp = cpuTimestamp
2016-03-02 09:08:17 +00:00
rescaleMetric = "CPU utilization"
2016-02-12 15:26:59 +00:00
}
if cmDesiredReplicas > desiredReplicas {
desiredReplicas = cmDesiredReplicas
timestamp = cmTimestamp
2016-03-02 09:08:17 +00:00
rescaleMetric = cmMetric
}
if desiredReplicas > currentReplicas {
rescaleReason = fmt . Sprintf ( "%s above target" , rescaleMetric )
2016-07-11 06:42:51 +00:00
}
if desiredReplicas < currentReplicas {
2016-03-02 09:08:17 +00:00
rescaleReason = "All metrics below target"
2016-02-12 15:26:59 +00:00
}
2015-08-20 12:55:28 +00:00
2016-02-12 15:26:59 +00:00
if hpa . Spec . MinReplicas != nil && desiredReplicas < * hpa . Spec . MinReplicas {
desiredReplicas = * hpa . Spec . MinReplicas
}
2015-08-25 17:16:47 +00:00
2016-07-19 15:54:38 +00:00
// never scale down to 0, reserved for disabling autoscaling
2016-02-12 15:26:59 +00:00
if desiredReplicas == 0 {
desiredReplicas = 1
}
2015-09-07 10:25:04 +00:00
2016-02-12 15:26:59 +00:00
if desiredReplicas > hpa . Spec . MaxReplicas {
desiredReplicas = hpa . Spec . MaxReplicas
}
2016-10-17 15:14:15 +00:00
// Do not upscale too much to prevent incorrect rapid increase of the number of master replicas caused by
// bogus CPU usage report from heapster/kubelet (like in issue #32304).
if desiredReplicas > calculateScaleUpLimit ( currentReplicas ) {
desiredReplicas = calculateScaleUpLimit ( currentReplicas )
}
2016-11-21 09:32:00 +00:00
rescale = shouldScale ( hpa , currentReplicas , desiredReplicas , timestamp )
2015-09-14 13:08:43 +00:00
}
2016-02-23 10:29:40 +00:00
if rescale {
scale . Spec . Replicas = desiredReplicas
2016-05-05 10:27:24 +00:00
_ , err = a . scaleNamespacer . Scales ( hpa . Namespace ) . Update ( hpa . Spec . ScaleTargetRef . Kind , scale )
2016-02-23 10:29:40 +00:00
if err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Eventf ( hpa , v1 . EventTypeWarning , "FailedRescale" , "New size: %d; reason: %s; error: %v" , desiredReplicas , rescaleReason , err . Error ( ) )
2016-02-23 10:29:40 +00:00
return fmt . Errorf ( "failed to rescale %s: %v" , reference , err )
}
2016-11-18 20:50:17 +00:00
a . eventRecorder . Eventf ( hpa , v1 . EventTypeNormal , "SuccessfulRescale" , "New size: %d; reason: %s" , desiredReplicas , rescaleReason )
2016-03-02 09:08:17 +00:00
glog . Infof ( "Successfull rescale of %s, old size: %d, new size: %d, reason: %s" ,
hpa . Name , currentReplicas , desiredReplicas , rescaleReason )
2016-02-23 10:29:40 +00:00
} else {
desiredReplicas = currentReplicas
}
return a . updateStatus ( hpa , currentReplicas , desiredReplicas , cpuCurrentUtilization , cmStatus , rescale )
}
2016-05-05 10:27:24 +00:00
func shouldScale ( hpa * autoscaling . HorizontalPodAutoscaler , currentReplicas , desiredReplicas int32 , timestamp time . Time ) bool {
2016-07-11 06:42:51 +00:00
if desiredReplicas == currentReplicas {
return false
}
2015-09-07 10:25:04 +00:00
2016-08-16 11:02:08 +00:00
if hpa . Status . LastScaleTime == nil {
return true
}
2016-07-11 06:42:51 +00:00
// Going down only if the usageRatio dropped significantly below the target
// and there was no rescaling in the last downscaleForbiddenWindow.
2016-08-16 11:02:08 +00:00
if desiredReplicas < currentReplicas && hpa . Status . LastScaleTime . Add ( downscaleForbiddenWindow ) . Before ( timestamp ) {
2016-07-11 06:42:51 +00:00
return true
}
// Going up only if the usage ratio increased significantly above the target
// and there was no rescaling in the last upscaleForbiddenWindow.
2016-08-16 11:02:08 +00:00
if desiredReplicas > currentReplicas && hpa . Status . LastScaleTime . Add ( upscaleForbiddenWindow ) . Before ( timestamp ) {
2016-07-11 06:42:51 +00:00
return true
2015-09-14 13:08:43 +00:00
}
2016-02-23 10:29:40 +00:00
return false
}
2015-08-25 17:16:47 +00:00
2016-05-05 10:27:24 +00:00
func ( a * HorizontalController ) updateCurrentReplicasInStatus ( hpa * autoscaling . HorizontalPodAutoscaler , currentReplicas int32 ) {
2016-02-23 12:05:07 +00:00
err := a . updateStatus ( hpa , currentReplicas , hpa . Status . DesiredReplicas , hpa . Status . CurrentCPUUtilizationPercentage , hpa . Annotations [ HpaCustomMetricsStatusAnnotationName ] , false )
if err != nil {
glog . Errorf ( "%v" , err )
}
}
2016-05-05 10:27:24 +00:00
func ( a * HorizontalController ) updateStatus ( hpa * autoscaling . HorizontalPodAutoscaler , currentReplicas , desiredReplicas int32 , cpuCurrentUtilization * int32 , cmStatus string , rescale bool ) error {
hpa . Status = autoscaling . HorizontalPodAutoscalerStatus {
2015-10-13 15:24:23 +00:00
CurrentReplicas : currentReplicas ,
DesiredReplicas : desiredReplicas ,
2016-01-29 11:20:19 +00:00
CurrentCPUUtilizationPercentage : cpuCurrentUtilization ,
2015-10-26 15:36:05 +00:00
LastScaleTime : hpa . Status . LastScaleTime ,
2015-09-14 13:08:43 +00:00
}
2016-01-29 11:20:19 +00:00
if cmStatus != "" {
hpa . Annotations [ HpaCustomMetricsStatusAnnotationName ] = cmStatus
}
2015-09-14 13:08:43 +00:00
if rescale {
2016-12-03 18:57:26 +00:00
now := metav1 . NewTime ( time . Now ( ) )
2015-10-13 15:24:23 +00:00
hpa . Status . LastScaleTime = & now
2015-09-14 13:08:43 +00:00
}
2016-03-02 08:29:17 +00:00
_ , err := a . hpaNamespacer . HorizontalPodAutoscalers ( hpa . Namespace ) . UpdateStatus ( hpa )
2015-09-14 13:08:43 +00:00
if err != nil {
2016-11-18 20:50:17 +00:00
a . eventRecorder . Event ( hpa , v1 . EventTypeWarning , "FailedUpdateStatus" , err . Error ( ) )
2015-09-14 13:08:43 +00:00
return fmt . Errorf ( "failed to update status for %s: %v" , hpa . Name , err )
}
2016-03-02 08:29:17 +00:00
glog . V ( 2 ) . Infof ( "Successfully updated status for %s" , hpa . Name )
2015-08-17 12:18:26 +00:00
return nil
}