Skip to content

Commit ab706a7

Browse files
fix HPA init bug (#399)
* fix HPA init bug * Update pkg/hpa/service_test.go Co-authored-by: Kensei Nakada <[email protected]> --------- Co-authored-by: Kensei Nakada <[email protected]>
1 parent c9a9c31 commit ab706a7

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

pkg/hpa/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func New(
8181
func (c *Service) InitializeHPA(ctx context.Context, tortoise *autoscalingv1beta3.Tortoise, replicaNum int32, now time.Time) (*autoscalingv1beta3.Tortoise, error) {
8282
logger := log.FromContext(ctx)
8383
// if all policy is off or Vertical, we don't need HPA.
84-
if !HasHorizontal(tortoise) {
84+
if !HasHorizontal(tortoise) && tortoise.Spec.TargetRefs.HorizontalPodAutoscalerName == nil {
8585
logger.Info("no horizontal policy, no need to create HPA")
8686
return tortoise, nil
8787
}

pkg/hpa/service_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -2969,6 +2969,96 @@ func TestService_InitializeHPA(t *testing.T) {
29692969
},
29702970
},
29712971
},
2972+
{
2973+
name: "set tortoise hpa to existing even if all autoscaling policy is vertical",
2974+
args: args{
2975+
tortoise: &v1beta3.Tortoise{
2976+
ObjectMeta: metav1.ObjectMeta{
2977+
Name: "tortoise",
2978+
Namespace: "default",
2979+
},
2980+
Spec: v1beta3.TortoiseSpec{
2981+
TargetRefs: v1beta3.TargetRefs{
2982+
ScaleTargetRef: v1beta3.CrossVersionObjectReference{
2983+
Kind: "Deployment",
2984+
Name: "deployment",
2985+
APIVersion: "apps/v1",
2986+
},
2987+
},
2988+
},
2989+
Status: v1beta3.TortoiseStatus{
2990+
AutoscalingPolicy: []v1beta3.ContainerAutoscalingPolicy{
2991+
{
2992+
ContainerName: "app",
2993+
Policy: map[v1.ResourceName]v1beta3.AutoscalingType{
2994+
v1.ResourceMemory: v1beta3.AutoscalingTypeVertical,
2995+
},
2996+
},
2997+
{
2998+
ContainerName: "istio-proxy",
2999+
Policy: map[v1.ResourceName]v1beta3.AutoscalingType{
3000+
v1.ResourceMemory: v1beta3.AutoscalingTypeVertical,
3001+
},
3002+
},
3003+
},
3004+
},
3005+
},
3006+
replicaNum: 4,
3007+
},
3008+
initialHPA: &v2.HorizontalPodAutoscaler{
3009+
ObjectMeta: metav1.ObjectMeta{
3010+
Name: "existing-hpa",
3011+
Namespace: "default",
3012+
Annotations: map[string]string{},
3013+
},
3014+
Spec: v2.HorizontalPodAutoscalerSpec{
3015+
MinReplicas: ptrInt32(1),
3016+
MaxReplicas: 2,
3017+
ScaleTargetRef: v2.CrossVersionObjectReference{
3018+
Kind: "Deployment",
3019+
Name: "deployment",
3020+
APIVersion: "apps/v1",
3021+
},
3022+
Behavior: &v2.HorizontalPodAutoscalerBehavior{
3023+
ScaleDown: &v2.HPAScalingRules{
3024+
Policies: []v2.HPAScalingPolicy{
3025+
{
3026+
Type: v2.PercentScalingPolicy,
3027+
Value: 2,
3028+
PeriodSeconds: 90,
3029+
},
3030+
},
3031+
},
3032+
},
3033+
},
3034+
},
3035+
afterHPA: &v2.HorizontalPodAutoscaler{
3036+
ObjectMeta: metav1.ObjectMeta{
3037+
Name: "existing-hpa",
3038+
Namespace: "default",
3039+
},
3040+
Spec: v2.HorizontalPodAutoscalerSpec{
3041+
MinReplicas: ptrInt32(1),
3042+
MaxReplicas: 2,
3043+
ScaleTargetRef: v2.CrossVersionObjectReference{
3044+
Kind: "Deployment",
3045+
Name: "deployment",
3046+
APIVersion: "apps/v1",
3047+
},
3048+
Behavior: &v2.HorizontalPodAutoscalerBehavior{
3049+
ScaleDown: &v2.HPAScalingRules{
3050+
Policies: []v2.HPAScalingPolicy{
3051+
{
3052+
Type: v2.PercentScalingPolicy,
3053+
Value: 2,
3054+
PeriodSeconds: 90,
3055+
},
3056+
},
3057+
},
3058+
},
3059+
},
3060+
},
3061+
},
29723062
}
29733063
for _, tt := range tests {
29743064
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)