From 7cee531170ea66a5438e375970ec28b9b8fd7c8e Mon Sep 17 00:00:00 2001 From: caoyingjunz Date: Sun, 17 Nov 2024 14:54:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20maxReplicas=20=E4=B8=BA?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E9=A1=B9=20(#42)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认MaxReplicas为6 Change-Id: I17415eacaab1530661e176bc63a78520f6423c17 --- README.md | 3 ++- .../autoscaler/autoscaler_controller.go | 17 +++---------- pkg/controller/controller_utils.go | 25 ++++++++++--------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index f0e57b6..6c141f9 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,10 @@ metadata: ... # 可选,默认 minReplicas 为 1 hpa.caoyingjunz.io/minReplicas: "2" # MINPODS - # 必填 + # 可选,默认 maxReplicas 为 6 hpa.caoyingjunz.io/maxReplicas: "6" # MAXPODS ... + # 支持多种 TARGETS 类型,若开启,至少选择一种,可同时支持多个 TARGETS # CPU, in cores. (500m = .5 cores) # Memory, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) diff --git a/pkg/controller/autoscaler/autoscaler_controller.go b/pkg/controller/autoscaler/autoscaler_controller.go index 8804610..6150cb2 100644 --- a/pkg/controller/autoscaler/autoscaler_controller.go +++ b/pkg/controller/autoscaler/autoscaler_controller.go @@ -152,25 +152,14 @@ func (ac *AutoscalerController) IsDeploymentControlHPA(d *appsv1.Deployment) boo return false } - var fdTarget, fdReplicas bool for annotation := range annotations { - if !fdReplicas { - if annotation == controller.MaxReplicas { - fdReplicas = true - } - } - if !fdTarget { - _, found := ac.items[annotation] - if found { - fdTarget = true - } - } - if fdReplicas && fdTarget { + _, found := ac.items[annotation] + if found { return true } } - return fdReplicas && fdTarget + return false } // syncAutoscaler will sync the autoscaler with the given key. diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index a58dd52..5172882 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -264,27 +264,28 @@ func ManageByPixiuController(hpa *autoscalingv2.HorizontalPodAutoscaler) bool { } func extractReplicas(annotations map[string]string, replicasType string) (int32, error) { - var Replicas int64 - var err error + var ( + Replicas string + exists bool + ) switch replicasType { case MinReplicas: - minReplicas, exists := annotations[MinReplicas] + Replicas, exists = annotations[MinReplicas] if !exists { - // Default minReplicas is 1 - return 1, nil - } - if Replicas, err = strconv.ParseInt(minReplicas, 10, 32); err != nil { - return 0, err + return 1, nil // Default minReplicas is 1 } case MaxReplicas: - maxReplicas, exists := annotations[MaxReplicas] + Replicas, exists = annotations[MaxReplicas] if !exists { - return 0, fmt.Errorf("%s is required", MaxReplicas) + return 6, nil // Default maxReplicas is 6 } - Replicas, err = strconv.ParseInt(maxReplicas, 10, 32) } - return int32(Replicas), err + targetReplicas, err := strconv.ParseInt(Replicas, 10, 32) + if err != nil { + return 0, err + } + return int32(targetReplicas), err } func extractAverageUtilization(averageUtilization string) (int32, error) {