Skip to content

Commit

Permalink
支持 maxReplicas 为可选项 (#42)
Browse files Browse the repository at this point in the history
默认MaxReplicas为6

Change-Id: I17415eacaab1530661e176bc63a78520f6423c17
  • Loading branch information
caoyingjunz authored Nov 17, 2024
1 parent 86a8fd2 commit 7cee531
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 3 additions & 14 deletions pkg/controller/autoscaler/autoscaler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 13 additions & 12 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7cee531

Please sign in to comment.