Skip to content

Commit

Permalink
Remove HPA when stacket is scaled down (#538)
Browse files Browse the repository at this point in the history
Co-authored-by: Milad Barazandeh <[email protected]>
  • Loading branch information
miladbarazandeh and Milad Barazandeh authored Oct 23, 2023
1 parent 0d0a376 commit 424c532
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/core/stack_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
33 changes: 29 additions & 4 deletions pkg/core/stack_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
})
}
}
Expand Down

0 comments on commit 424c532

Please sign in to comment.