Skip to content

Commit abce1d8

Browse files
committed
add additional log & UT for HPA
add additional log & UT for HPA
1 parent 468676c commit abce1d8

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

pkg/controller/podautoscaler/horizontal.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,11 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare
845845

846846
logger.V(4).Info("Proposing desired replicas",
847847
"desiredReplicas", metricDesiredReplicas,
848+
"currentReplicas", currentReplicas,
848849
"metric", metricName,
849850
"timestamp", metricTimestamp,
850-
"scaleTarget", reference)
851+
"scaleTarget", reference,
852+
"metrics", metricStatuses)
851853

852854
rescaleMetric := ""
853855
if metricDesiredReplicas > desiredReplicas {
@@ -885,6 +887,7 @@ func (a *HorizontalController) reconcileAutoscaler(ctx context.Context, hpaShare
885887
a.storeScaleEvent(hpa.Spec.Behavior, key, currentReplicas, desiredReplicas)
886888
logger.Info("Successfully rescaled",
887889
"HPA", klog.KObj(hpa),
890+
"scaleTarget", reference,
888891
"currentReplicas", currentReplicas,
889892
"desiredReplicas", desiredReplicas,
890893
"reason", rescaleReason)
@@ -957,6 +960,9 @@ func (a *HorizontalController) normalizeDesiredReplicas(hpa *autoscalingv2.Horiz
957960
} else {
958961
setCondition(hpa, autoscalingv2.ScalingLimited, v1.ConditionTrue, reason, "%s", message)
959962
}
963+
reference := fmt.Sprintf("%s/%s/%s", hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name)
964+
klog.V(4).Infof("Normalized desired replicas: scaleTarget=%s, currentReplicas=%d, desiredReplicas=%d, minReplicas=%d, stabilizedRecommendation=%d, prenormalizedDesiredReplicas=%d, condition=%s, reason=%s",
965+
reference, currentReplicas, desiredReplicas, minReplicas, stabilizedRecommendation, prenormalizedDesiredReplicas, reason, reason)
960966

961967
return desiredReplicas
962968
}
@@ -995,13 +1001,21 @@ func (a *HorizontalController) normalizeDesiredReplicasWithBehaviors(hpa *autosc
9951001
} else {
9961002
setCondition(hpa, autoscalingv2.AbleToScale, v1.ConditionTrue, "ReadyForNewScale", "recommended size matches current size")
9971003
}
1004+
1005+
reference := fmt.Sprintf("%s/%s/%s", hpa.Spec.ScaleTargetRef.Kind, hpa.Namespace, hpa.Spec.ScaleTargetRef.Name)
1006+
klog.V(4).Infof("Normalized desired replicas with behaviors - after stabilized recommendation: scaleTarget=%s, currentReplicas=%d, minReplicas=%d, stabilizedRecommendation=%d, prenormalizedDesiredReplicas=%d, reason=%s, message=%s",
1007+
reference, currentReplicas, minReplicas, stabilizedRecommendation, prenormalizedDesiredReplicas, reason, message)
1008+
9981009
desiredReplicas, reason, message := a.convertDesiredReplicasWithBehaviorRate(normalizationArg)
9991010
if desiredReplicas == stabilizedRecommendation {
10001011
setCondition(hpa, autoscalingv2.ScalingLimited, v1.ConditionFalse, reason, "%s", message)
10011012
} else {
10021013
setCondition(hpa, autoscalingv2.ScalingLimited, v1.ConditionTrue, reason, "%s", message)
10031014
}
10041015

1016+
klog.V(4).Infof("Normalized desired replicas with behaviors - after rated recommendation: scaleTarget=%s, currentReplicas=%d, minReplicas=%d, stabilizedRecommendation=%d, desiredReplicas=%d, reason=%s, message=%s",
1017+
reference, currentReplicas, minReplicas, stabilizedRecommendation, desiredReplicas, reason, message)
1018+
10051019
return desiredReplicas
10061020
}
10071021

@@ -1128,7 +1142,13 @@ func (a *HorizontalController) stabilizeRecommendationWithBehaviors(args Normali
11281142
if recommendation > downRecommendation {
11291143
recommendation = downRecommendation
11301144
}
1131-
1145+
// Only keep the recommendations and ignore timestamp for logging.
1146+
var recommendations []int32
1147+
for _, rec := range a.recommendations[args.Key] {
1148+
recommendations = append(recommendations, rec.recommendation)
1149+
}
1150+
klog.V(4).Infof("Stabilizing recommendation: key=%s, currentReplicas=%d, desiredReplicas=%d, upRecommendation=%d, downRecommendation=%d, recommendations=%v",
1151+
args.Key, args.CurrentReplicas, args.DesiredReplicas, upRecommendation, downRecommendation, recommendations)
11321152
// Record the unstabilized recommendation.
11331153
if foundOldSample {
11341154
a.recommendations[args.Key][oldSampleIndex] = timestampedRecommendation{args.DesiredReplicas, time.Now()}
@@ -1158,6 +1178,8 @@ func (a *HorizontalController) convertDesiredReplicasWithBehaviorRate(args Norma
11581178
defer a.scaleUpEventsLock.RUnlock()
11591179
a.scaleDownEventsLock.RLock()
11601180
defer a.scaleDownEventsLock.RUnlock()
1181+
klog.V(4).Infof("Converting desired replicas with behavior rate - scale up: key=%s, currentReplicas=%d, desiredReplicas=%d, scaleUpEvents=%v, scaleDownEvents=%v",
1182+
args.Key, args.CurrentReplicas, args.DesiredReplicas, a.scaleUpEvents[args.Key], a.scaleDownEvents[args.Key])
11611183
scaleUpLimit := calculateScaleUpLimitWithScalingRules(args.CurrentReplicas, a.scaleUpEvents[args.Key], a.scaleDownEvents[args.Key], args.ScaleUpBehavior)
11621184

11631185
if scaleUpLimit < args.CurrentReplicas {
@@ -1181,6 +1203,8 @@ func (a *HorizontalController) convertDesiredReplicasWithBehaviorRate(args Norma
11811203
defer a.scaleUpEventsLock.RUnlock()
11821204
a.scaleDownEventsLock.RLock()
11831205
defer a.scaleDownEventsLock.RUnlock()
1206+
klog.V(4).Infof("Converting desired replicas with behavior rate - scale down: key=%s, currentReplicas=%d, desiredReplicas=%d, scaleUpEvents=%v, scaleDownEvents=%v",
1207+
args.Key, args.CurrentReplicas, args.DesiredReplicas, a.scaleUpEvents[args.Key], a.scaleDownEvents[args.Key])
11841208
scaleDownLimit := calculateScaleDownLimitWithBehaviors(args.CurrentReplicas, a.scaleUpEvents[args.Key], a.scaleDownEvents[args.Key], args.ScaleDownBehavior)
11851209

11861210
if scaleDownLimit > args.CurrentReplicas {

pkg/controller/podautoscaler/horizontal_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,6 +4315,42 @@ func TestScalingWithRules(t *testing.T) {
43154315
expectedReplicas: 16,
43164316
expectedCondition: "ScaleUpLimit",
43174317
},
4318+
{
4319+
name: "scaleUp with percent policy and no previous events, thus scaled up within range",
4320+
scaleUpEvents: generateEventsUniformDistribution([]int{0}, 120),
4321+
scaleDownEvents: generateEventsUniformDistribution([]int{0}, 120),
4322+
specMinReplicas: 3,
4323+
specMaxReplicas: 6,
4324+
scaleUpRules: generateScalingRules(0, 0, 20, 600, 300),
4325+
currentReplicas: 3,
4326+
prenormalizedDesiredReplicas: 4,
4327+
expectedReplicas: 4,
4328+
expectedCondition: "DesiredWithinRange",
4329+
},
4330+
{
4331+
name: "scaleUp with both pod and percent policy and previous scale up events, thus no scale up",
4332+
scaleUpEvents: generateEventsUniformDistribution([]int{2}, 120),
4333+
scaleDownEvents: generateEventsUniformDistribution([]int{0}, 120),
4334+
specMinReplicas: 3,
4335+
specMaxReplicas: 6,
4336+
scaleUpRules: generateScalingRules(1, 600, 20, 600, 300),
4337+
currentReplicas: 3,
4338+
prenormalizedDesiredReplicas: 4,
4339+
expectedReplicas: 3,
4340+
expectedCondition: "ScaleUpLimit",
4341+
},
4342+
{
4343+
name: "scaleUp with percent policy and previous events but still be able to scale up with limited rate",
4344+
scaleUpEvents: generateEventsUniformDistribution([]int{1}, 600),
4345+
scaleDownEvents: generateEventsUniformDistribution([]int{0}, 600),
4346+
specMinReplicas: 3,
4347+
specMaxReplicas: 20,
4348+
scaleUpRules: generateScalingRules(0, 0, 20, 600, 300),
4349+
currentReplicas: 10,
4350+
prenormalizedDesiredReplicas: 12,
4351+
expectedReplicas: 11,
4352+
expectedCondition: "ScaleUpLimit",
4353+
},
43184354
// ScaleDown with PeriodSeconds usage
43194355
{
43204356
name: "scaleDown with default policy and previous events",

0 commit comments

Comments
 (0)