Skip to content

Commit

Permalink
fix HPA init bug (#399)
Browse files Browse the repository at this point in the history
* fix HPA init bug

* Update pkg/hpa/service_test.go

Co-authored-by: Kensei Nakada <[email protected]>

---------

Co-authored-by: Kensei Nakada <[email protected]>
  • Loading branch information
randytqwjp and sanposhiho authored Apr 26, 2024
1 parent c9a9c31 commit ab706a7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/hpa/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func New(
func (c *Service) InitializeHPA(ctx context.Context, tortoise *autoscalingv1beta3.Tortoise, replicaNum int32, now time.Time) (*autoscalingv1beta3.Tortoise, error) {
logger := log.FromContext(ctx)
// if all policy is off or Vertical, we don't need HPA.
if !HasHorizontal(tortoise) {
if !HasHorizontal(tortoise) && tortoise.Spec.TargetRefs.HorizontalPodAutoscalerName == nil {
logger.Info("no horizontal policy, no need to create HPA")
return tortoise, nil
}
Expand Down
90 changes: 90 additions & 0 deletions pkg/hpa/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2969,6 +2969,96 @@ func TestService_InitializeHPA(t *testing.T) {
},
},
},
{
name: "set tortoise hpa to existing even if all autoscaling policy is vertical",
args: args{
tortoise: &v1beta3.Tortoise{
ObjectMeta: metav1.ObjectMeta{
Name: "tortoise",
Namespace: "default",
},
Spec: v1beta3.TortoiseSpec{
TargetRefs: v1beta3.TargetRefs{
ScaleTargetRef: v1beta3.CrossVersionObjectReference{
Kind: "Deployment",
Name: "deployment",
APIVersion: "apps/v1",
},
},
},
Status: v1beta3.TortoiseStatus{
AutoscalingPolicy: []v1beta3.ContainerAutoscalingPolicy{
{
ContainerName: "app",
Policy: map[v1.ResourceName]v1beta3.AutoscalingType{
v1.ResourceMemory: v1beta3.AutoscalingTypeVertical,
},
},
{
ContainerName: "istio-proxy",
Policy: map[v1.ResourceName]v1beta3.AutoscalingType{
v1.ResourceMemory: v1beta3.AutoscalingTypeVertical,
},
},
},
},
},
replicaNum: 4,
},
initialHPA: &v2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "existing-hpa",
Namespace: "default",
Annotations: map[string]string{},
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
MaxReplicas: 2,
ScaleTargetRef: v2.CrossVersionObjectReference{
Kind: "Deployment",
Name: "deployment",
APIVersion: "apps/v1",
},
Behavior: &v2.HorizontalPodAutoscalerBehavior{
ScaleDown: &v2.HPAScalingRules{
Policies: []v2.HPAScalingPolicy{
{
Type: v2.PercentScalingPolicy,
Value: 2,
PeriodSeconds: 90,
},
},
},
},
},
},
afterHPA: &v2.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "existing-hpa",
Namespace: "default",
},
Spec: v2.HorizontalPodAutoscalerSpec{
MinReplicas: ptrInt32(1),
MaxReplicas: 2,
ScaleTargetRef: v2.CrossVersionObjectReference{
Kind: "Deployment",
Name: "deployment",
APIVersion: "apps/v1",
},
Behavior: &v2.HorizontalPodAutoscalerBehavior{
ScaleDown: &v2.HPAScalingRules{
Policies: []v2.HPAScalingPolicy{
{
Type: v2.PercentScalingPolicy,
Value: 2,
PeriodSeconds: 90,
},
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit ab706a7

Please sign in to comment.