Merge pull request #35272 from yarntime/refactor_reconcileAutoscaler

Automatic merge from submit-queue

rescale immediately if the basic constraints are not satisfied

refactor reconcileAutoscaler.
If the basic constraints are not satisfied, we should rescale the target ref immediately.
This commit is contained in:
Kubernetes Submit Queue 2016-12-01 15:06:58 -08:00 committed by GitHub
commit 67a12c9e1f
2 changed files with 40 additions and 1 deletions

View File

@ -292,9 +292,12 @@ func (a *HorizontalController) reconcileAutoscaler(hpa *autoscaling.HorizontalPo
rescaleReason := "" rescaleReason := ""
timestamp := time.Now() timestamp := time.Now()
rescale := true
if scale.Spec.Replicas == 0 { if scale.Spec.Replicas == 0 {
// Autoscaling is disabled for this resource // Autoscaling is disabled for this resource
desiredReplicas = 0 desiredReplicas = 0
rescale = false
} else if currentReplicas > hpa.Spec.MaxReplicas { } else if currentReplicas > hpa.Spec.MaxReplicas {
rescaleReason = "Current number of replicas above Spec.MaxReplicas" rescaleReason = "Current number of replicas above Spec.MaxReplicas"
desiredReplicas = hpa.Spec.MaxReplicas desiredReplicas = hpa.Spec.MaxReplicas
@ -360,9 +363,10 @@ func (a *HorizontalController) reconcileAutoscaler(hpa *autoscaling.HorizontalPo
if desiredReplicas > calculateScaleUpLimit(currentReplicas) { if desiredReplicas > calculateScaleUpLimit(currentReplicas) {
desiredReplicas = calculateScaleUpLimit(currentReplicas) desiredReplicas = calculateScaleUpLimit(currentReplicas)
} }
rescale = shouldScale(hpa, currentReplicas, desiredReplicas, timestamp)
} }
rescale := shouldScale(hpa, currentReplicas, desiredReplicas, timestamp)
if rescale { if rescale {
scale.Spec.Replicas = desiredReplicas scale.Spec.Replicas = desiredReplicas
_, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(hpa.Spec.ScaleTargetRef.Kind, scale) _, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(hpa.Spec.ScaleTargetRef.Kind, scale)

View File

@ -95,6 +95,9 @@ type testCase struct {
// Target resource information. // Target resource information.
resource *fakeResource resource *fakeResource
// Last scale time
lastScaleTime *unversioned.Time
} }
// Needs to be called under a lock. // Needs to be called under a lock.
@ -1042,4 +1045,36 @@ func TestComputedToleranceAlgImplementation(t *testing.T) {
tc.runTest(t) tc.runTest(t)
} }
func TestScaleUpRCImmediately(t *testing.T) {
time := unversioned.Time{Time: time.Now()}
tc := testCase{
minReplicas: 2,
maxReplicas: 6,
initialReplicas: 1,
desiredReplicas: 2,
verifyCPUCurrent: true,
reportedLevels: []uint64{0, 0, 0, 0},
reportedCPURequests: []resource.Quantity{resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0"), resource.MustParse("1.0")},
useMetricsApi: true,
lastScaleTime: &time,
}
tc.runTest(t)
}
func TestScaleDownRCImmediately(t *testing.T) {
time := unversioned.Time{Time: time.Now()}
tc := testCase{
minReplicas: 2,
maxReplicas: 5,
initialReplicas: 6,
desiredReplicas: 5,
CPUTarget: 50,
reportedLevels: []uint64{8000, 9500, 1000},
reportedCPURequests: []resource.Quantity{resource.MustParse("0.9"), resource.MustParse("1.0"), resource.MustParse("1.1")},
useMetricsApi: true,
lastScaleTime: &time,
}
tc.runTest(t)
}
// TODO: add more tests // TODO: add more tests