Skip to content

Commit a1736ce

Browse files
committed
scheduled-scaling: Handle mili values (fractions) in target value
Signed-off-by: Mikkel Oscar Lyderik Larsen <[email protected]>
1 parent 33db2b7 commit a1736ce

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

pkg/controller/scheduledscaling/scheduled_scaling.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var (
5757
type now func() time.Time
5858

5959
type scalingScheduleStore interface {
60-
List() []interface{}
60+
List() []any
6161
}
6262

6363
type Controller struct {
@@ -325,7 +325,7 @@ func highestActiveSchedule(hpa *autoscalingv2.HorizontalPodAutoscaler, activeSch
325325
continue
326326
}
327327

328-
target := int64(metric.Object.Target.AverageValue.MilliValue() / 1000)
328+
target := float64(metric.Object.Target.AverageValue.MilliValue()) / 1000.0
329329
if target == 0 {
330330
continue
331331
}
@@ -338,10 +338,10 @@ func highestActiveSchedule(hpa *autoscalingv2.HorizontalPodAutoscaler, activeSch
338338
value = activeSchedules[scheduleName]
339339
}
340340

341-
expected := int64(math.Ceil(float64(value) / float64(target)))
341+
expected := int64(math.Ceil(float64(value) / target))
342342
if expected > highestExpected {
343343
highestExpected = expected
344-
usageRatio = float64(value) / (float64(target) * float64(currentReplicas))
344+
usageRatio = float64(value) / (target * float64(currentReplicas))
345345
highestObject = metric.Object.DescribedObject
346346
}
347347
}

pkg/controller/scheduledscaling/scheduled_scaling_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,35 +336,42 @@ func TestAdjustScaling(t *testing.T) {
336336
msg string
337337
currentReplicas int32
338338
desiredReplicas int32
339-
targetValue int64
339+
targetValue string
340340
scheduleTarget int64
341341
}{
342342
{
343343
msg: "current less than 10%% below desired (target 10000)",
344344
currentReplicas: 28, // 7.1% increase to desired
345345
desiredReplicas: 31,
346-
targetValue: 333, // 10000/333 ~= 31
346+
targetValue: "333", // 10000/333 ~= 31
347347
scheduleTarget: 10000,
348348
},
349349
{
350350
msg: "current less than 10%% below desired",
351351
currentReplicas: 95, // 5.3% increase to desired
352352
desiredReplicas: 100,
353-
targetValue: 10, // 1000/10 = 100
353+
targetValue: "10", // 1000/10 = 100
354354
scheduleTarget: 1000,
355355
},
356356
{
357357
msg: "current more than 10%% below desired, no adjustment",
358358
currentReplicas: 90, // 11% increase to desired
359359
desiredReplicas: 90,
360-
targetValue: 10, // 1000/10 = 100
360+
targetValue: "10", // 1000/10 = 100
361361
scheduleTarget: 1000,
362362
},
363363
{
364364
msg: "invalid HPA should not do any adjustment",
365365
currentReplicas: 95,
366366
desiredReplicas: 95,
367-
targetValue: 0, // this is treated as invalid in the test, thus the HPA is ingored and no adjustment happens.
367+
targetValue: "", // this is treated as invalid in the test, thus the HPA is ingored and no adjustment happens.
368+
scheduleTarget: 1000,
369+
},
370+
{
371+
msg: "handle milivalue as targetValue",
372+
currentReplicas: 90, // 5.8% increase to desired
373+
desiredReplicas: 96,
374+
targetValue: "10500m", // 1000/10.5 ~= 96
368375
scheduleTarget: 1000,
369376
},
370377
} {
@@ -444,8 +451,11 @@ func TestAdjustScaling(t *testing.T) {
444451
},
445452
}
446453

447-
if tc.targetValue != 0 {
448-
hpa.Spec.Metrics[0].Object.Target.AverageValue = resource.NewQuantity(tc.targetValue, resource.DecimalSI)
454+
if tc.targetValue != "" {
455+
quantity, err := resource.ParseQuantity(tc.targetValue)
456+
require.NoError(t, err)
457+
458+
hpa.Spec.Metrics[0].Object.Target.AverageValue = &quantity
449459
}
450460

451461
hpa, err = kubeClient.AutoscalingV2().HorizontalPodAutoscalers("default").Create(context.Background(), hpa, metav1.CreateOptions{})

0 commit comments

Comments
 (0)