Skip to content

Commit 271209c

Browse files
committed
Enhance: desireReplicas will be limited by maxReplicaCount when scaling up
Signed-off-by: ChrisLiu <chrisliu1995@163.com>
1 parent 250bd86 commit 271209c

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

pkg/externalscaler/externalscaler.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
const (
1717
NoneGameServerMinNumberKey = "minAvailable"
18+
MaxReplicaCountKey = "maxReplicaCount"
1819
)
1920

2021
type ExternalScaler struct {
@@ -90,16 +91,34 @@ func (e *ExternalScaler) GetMetrics(ctx context.Context, metricRequest *GetMetri
9091
}
9192
if err == nil && noneNum < int(minNum) {
9293
desireReplicas := *gss.Spec.Replicas + int32(minNum) - int32(noneNum)
93-
klog.Infof("GameServerSet %s/%s desire replicas is %d", ns, name, desireReplicas)
94-
return &GetMetricsResponse{
95-
MetricValues: []*MetricValue{{
96-
MetricName: "gssReplicas",
97-
MetricValue: int64(desireReplicas),
98-
}},
99-
}, nil
94+
maxReplicaCount, err := strconv.ParseInt(metricRequest.ScaledObjectRef.GetScalerMetadata()[MaxReplicaCountKey], 10, 32)
95+
// When maxReplicaCount is nil or set failed, desireReplicas will be determined just by minAvailable.
96+
if err != nil {
97+
klog.Infof("GameServerSet %s/%s desire replicas is %d", ns, name, desireReplicas)
98+
return &GetMetricsResponse{
99+
MetricValues: []*MetricValue{{
100+
MetricName: "gssReplicas",
101+
MetricValue: int64(desireReplicas),
102+
}},
103+
}, nil
104+
}
105+
106+
// When maxReplicaCount is set successfully, desireReplicas will be limited by maxReplicaCount.
107+
if *gss.Spec.Replicas != int32(maxReplicaCount) {
108+
if desireReplicas > int32(maxReplicaCount) {
109+
desireReplicas = int32(maxReplicaCount)
110+
}
111+
klog.Infof("GameServerSet %s/%s desire replicas is %d", ns, name, desireReplicas)
112+
return &GetMetricsResponse{
113+
MetricValues: []*MetricValue{{
114+
MetricName: "gssReplicas",
115+
MetricValue: int64(desireReplicas),
116+
}},
117+
}, nil
118+
}
100119
}
101120

102-
// scale up those GameServers with WaitToBeDeleted opsState
121+
// scale down those GameServers with WaitToBeDeleted opsState
103122
isWaitToDelete, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerOpsStateKey, selection.Equals, []string{string(gamekruiseiov1alpha1.WaitToDelete)})
104123
notDeleting, _ := labels.NewRequirement(gamekruiseiov1alpha1.GameServerStateKey, selection.NotEquals, []string{string(gamekruiseiov1alpha1.Deleting)})
105124
podList = &corev1.PodList{}

0 commit comments

Comments
 (0)