Fix missing AverageValue on HPA (#769)

Signed-off-by: Mikkel Oscar Lyderik Larsen <mikkel.larsen@zalando.de>
This commit is contained in:
Mikkel Oscar Lyderik Larsen 2024-12-13 15:32:09 +01:00 committed by GitHub
parent ca9031228b
commit c26604c36b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 2 deletions

View File

@ -320,6 +320,10 @@ func highestActiveSchedule(hpa *autoscalingv2.HorizontalPodAutoscaler, activeSch
scheduleName := metric.Object.DescribedObject.Name scheduleName := metric.Object.DescribedObject.Name
if metric.Object.Target.AverageValue == nil {
continue
}
target := int64(metric.Object.Target.AverageValue.MilliValue() / 1000) target := int64(metric.Object.Target.AverageValue.MilliValue() / 1000)
if target == 0 { if target == 0 {
continue continue

View File

@ -350,6 +350,12 @@ func TestAdjustScaling(t *testing.T) {
desiredReplicas: 90, desiredReplicas: 90,
targetValue: 10, // 1000/10 = 100 targetValue: 10, // 1000/10 = 100
}, },
{
msg: "invalid HPA should not do any adjustment",
currentReplicas: 95,
desiredReplicas: 95,
targetValue: 0, // this is treated as invalid in the test, thus the HPA is ingored and no adjustment happens.
},
} { } {
t.Run(tc.msg, func(t *testing.T) { t.Run(tc.msg, func(t *testing.T) {
kubeClient := fake.NewSimpleClientset() kubeClient := fake.NewSimpleClientset()
@ -419,8 +425,7 @@ func TestAdjustScaling(t *testing.T) {
Name: "schedule-1", Name: "schedule-1",
}, },
Target: v2.MetricTarget{ Target: v2.MetricTarget{
Type: v2.AverageValueMetricType, Type: v2.AverageValueMetricType,
AverageValue: resource.NewQuantity(tc.targetValue, resource.DecimalSI),
}, },
}, },
}, },
@ -428,6 +433,10 @@ func TestAdjustScaling(t *testing.T) {
}, },
} }
if tc.targetValue != 0 {
hpa.Spec.Metrics[0].Object.Target.AverageValue = resource.NewQuantity(tc.targetValue, resource.DecimalSI)
}
hpa, err = kubeClient.AutoscalingV2().HorizontalPodAutoscalers("default").Create(context.Background(), hpa, metav1.CreateOptions{}) hpa, err = kubeClient.AutoscalingV2().HorizontalPodAutoscalers("default").Create(context.Background(), hpa, metav1.CreateOptions{})
require.NoError(t, err) require.NoError(t, err)