From 424c532be6fb72d6b6b2881bc3bf57ce0b0f2082 Mon Sep 17 00:00:00 2001 From: Milad Barazandeh Date: Mon, 23 Oct 2023 14:40:10 +0200 Subject: [PATCH] Remove HPA when stacket is scaled down (#538) Co-authored-by: Milad Barazandeh --- pkg/core/stack_resources.go | 4 ++++ pkg/core/stack_resources_test.go | 33 ++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/core/stack_resources.go b/pkg/core/stack_resources.go index b814b6e8..bc099f9b 100644 --- a/pkg/core/stack_resources.go +++ b/pkg/core/stack_resources.go @@ -223,6 +223,10 @@ func (sc *StackContainer) GenerateHPA() (*autoscaling.HorizontalPodAutoscaler, e return nil, nil } + if sc.ScaledDown() { + return nil, nil + } + result := &autoscaling.HorizontalPodAutoscaler{ ObjectMeta: sc.resourceMeta(), TypeMeta: metav1.TypeMeta{ diff --git a/pkg/core/stack_resources_test.go b/pkg/core/stack_resources_test.go index cafd5fbf..19cbf89c 100644 --- a/pkg/core/stack_resources_test.go +++ b/pkg/core/stack_resources_test.go @@ -895,6 +895,7 @@ func TestGenerateHPA(t *testing.T) { autoscaler *zv1.Autoscaler expectedMinReplicas *int32 expectedMaxReplicas int32 + noTrafficSince time.Time expectedMetrics []autoscaling.MetricSpec expectedBehavior *autoscaling.HorizontalPodAutoscalerBehavior }{ @@ -928,6 +929,24 @@ func TestGenerateHPA(t *testing.T) { }, expectedBehavior: exampleBehavior, }, + { + name: "HPA when stack scaled down", + autoscaler: &zv1.Autoscaler{ + MinReplicas: &min, + MaxReplicas: max, + + Metrics: []zv1.AutoscalerMetrics{ + { + Type: zv1.CPUAutoscalerMetric, + AverageUtilization: &utilization, + }, + }, + Behavior: exampleBehavior, + }, + noTrafficSince: time.Now().Add(-time.Hour), + expectedMetrics: nil, + expectedBehavior: nil, + }, } { t.Run(tc.name, func(t *testing.T) { podTemplate := zv1.PodTemplateSpec{ @@ -955,14 +974,20 @@ func TestGenerateHPA(t *testing.T) { }, }, }, + noTrafficSince: tc.noTrafficSince, + scaledownTTL: time.Minute, } hpa, err := autoscalerContainer.GenerateHPA() require.NoError(t, err) - require.Equal(t, tc.expectedMinReplicas, hpa.Spec.MinReplicas) - require.Equal(t, tc.expectedMaxReplicas, hpa.Spec.MaxReplicas) - require.Equal(t, tc.expectedMetrics, hpa.Spec.Metrics) - require.Equal(t, tc.expectedBehavior, hpa.Spec.Behavior) + if tc.expectedBehavior == nil { + require.Nil(t, hpa) + } else { + require.Equal(t, tc.expectedMinReplicas, hpa.Spec.MinReplicas) + require.Equal(t, tc.expectedMaxReplicas, hpa.Spec.MaxReplicas) + require.Equal(t, tc.expectedMetrics, hpa.Spec.Metrics) + require.Equal(t, tc.expectedBehavior, hpa.Spec.Behavior) + } }) } }