-
Notifications
You must be signed in to change notification settings - Fork 831
/
Copy pathfleetautoscalers.go
656 lines (565 loc) · 22.8 KB
/
fleetautoscalers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
* Copyright 2018 Google LLC All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fleetautoscalers
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
autoscalingv1 "agones.dev/agones/pkg/apis/autoscaling/v1"
listeragonesv1 "agones.dev/agones/pkg/client/listers/agones/v1"
"agones.dev/agones/pkg/fleets"
"agones.dev/agones/pkg/gameservers"
gssets "agones.dev/agones/pkg/gameserversets"
"agones.dev/agones/pkg/util/runtime"
)
const maxDuration = "2540400h" // 290 Years
var tlsConfig = &tls.Config{}
var client = http.Client{
Timeout: 15 * time.Second,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
// InactiveScheduleError denotes an error for schedules that are not currently active.
type InactiveScheduleError struct{}
func (InactiveScheduleError) Error() string {
return "inactive schedule, policy not applicable"
}
// computeDesiredFleetSize computes the new desired size of the given fleet
func computeDesiredFleetSize(pol autoscalingv1.FleetAutoscalerPolicy, f *agonesv1.Fleet,
gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount) (int32, bool, error) {
switch pol.Type {
case autoscalingv1.BufferPolicyType:
return applyBufferPolicy(pol.Buffer, f)
case autoscalingv1.WebhookPolicyType:
return applyWebhookPolicy(pol.Webhook, f)
case autoscalingv1.CounterPolicyType:
return applyCounterOrListPolicy(pol.Counter, nil, f, gameServerLister, nodeCounts)
case autoscalingv1.ListPolicyType:
return applyCounterOrListPolicy(nil, pol.List, f, gameServerLister, nodeCounts)
case autoscalingv1.SchedulePolicyType:
return applySchedulePolicy(pol.Schedule, f, gameServerLister, nodeCounts, time.Now())
case autoscalingv1.ChainPolicyType:
return applyChainPolicy(pol.Chain, f, gameServerLister, nodeCounts, time.Now())
}
return 0, false, errors.New("wrong policy type, should be one of: Buffer, Webhook, Counter, List")
}
// buildURLFromWebhookPolicy - build URL for Webhook and set CARoot for client Transport
func buildURLFromWebhookPolicy(w *autoscalingv1.WebhookPolicy) (u *url.URL, err error) {
if w.URL != nil && w.Service != nil {
return nil, errors.New("service and URL cannot be used simultaneously")
}
scheme := "http"
if w.CABundle != nil {
scheme = "https"
if err := setCABundle(w.CABundle); err != nil {
return nil, err
}
}
if w.URL != nil {
if *w.URL == "" {
return nil, errors.New("URL was not provided")
}
return url.ParseRequestURI(*w.URL)
}
if w.Service == nil {
return nil, errors.New("service was not provided, either URL or Service must be provided")
}
if w.Service.Name == "" {
return nil, errors.New("service name was not provided")
}
if w.Service.Path == nil {
empty := ""
w.Service.Path = &empty
}
if w.Service.Namespace == "" {
w.Service.Namespace = "default"
}
return createURL(scheme, w.Service.Name, w.Service.Namespace, *w.Service.Path, w.Service.Port), nil
}
// moved to a separate method to cover it with unit tests and check that URL corresponds to a proper pattern
func createURL(scheme, name, namespace, path string, port *int32) *url.URL {
var hostPort int32 = 8000
if port != nil {
hostPort = *port
}
return &url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s.%s.svc:%d", name, namespace, hostPort),
Path: path,
}
}
func setCABundle(caBundle []byte) error {
// We can have multiple fleetautoscalers with different CABundles defined,
// so we switch client.Transport before each POST request
rootCAs := x509.NewCertPool()
if ok := rootCAs.AppendCertsFromPEM(caBundle); !ok {
return errors.New("no certs were appended from caBundle")
}
tlsConfig.RootCAs = rootCAs
return nil
}
func applyWebhookPolicy(w *autoscalingv1.WebhookPolicy, f *agonesv1.Fleet) (replicas int32, limited bool, err error) {
if w == nil {
return 0, false, errors.New("webhookPolicy parameter must not be nil")
}
if f == nil {
return 0, false, errors.New("fleet parameter must not be nil")
}
u, err := buildURLFromWebhookPolicy(w)
if err != nil {
return 0, false, err
}
faReq := autoscalingv1.FleetAutoscaleReview{
Request: &autoscalingv1.FleetAutoscaleRequest{
UID: uuid.NewUUID(),
Name: f.Name,
Namespace: f.Namespace,
Status: f.Status,
},
Response: nil,
}
b, err := json.Marshal(faReq)
if err != nil {
return 0, false, err
}
res, err := client.Post(
u.String(),
"application/json",
strings.NewReader(string(b)),
)
if err != nil {
return 0, false, err
}
defer func() {
if cerr := res.Body.Close(); cerr != nil {
if err != nil {
err = errors.Wrap(err, cerr.Error())
} else {
err = cerr
}
}
}()
if res.StatusCode != http.StatusOK {
return 0, false, fmt.Errorf("bad status code %d from the server: %s", res.StatusCode, u.String())
}
result, err := io.ReadAll(res.Body)
if err != nil {
return 0, false, err
}
var faResp autoscalingv1.FleetAutoscaleReview
err = json.Unmarshal(result, &faResp)
if err != nil {
return 0, false, err
}
if faResp.Response.Scale {
return faResp.Response.Replicas, false, nil
}
return f.Status.Replicas, false, nil
}
func applyBufferPolicy(b *autoscalingv1.BufferPolicy, f *agonesv1.Fleet) (int32, bool, error) {
var replicas int32
if b.BufferSize.Type == intstr.Int {
replicas = f.Status.AllocatedReplicas + int32(b.BufferSize.IntValue())
} else {
// the percentage value is a little more complex, as we can't apply
// the desired percentage to any current value, but to the future one
// Example: we have 8 allocated replicas, 10 total replicas and bufferSize set to 30%
// 30% means that we must have 30% ready instances in the fleet
// Right now there are 20%, so we must increase the fleet until we reach 30%
// To compute the new size, we start from the other end: if ready must be 30%
// it means that allocated must be 70% and adjust the fleet size to make that true.
bufferPercent, err := intstr.GetValueFromIntOrPercent(&b.BufferSize, 100, true)
if err != nil {
return 0, false, err
}
// use Math.Ceil to round the result up
replicas = int32(math.Ceil(float64(f.Status.AllocatedReplicas*100) / float64(100-bufferPercent)))
}
scalingInLimited := false
scalingOutLimited := false
if replicas < b.MinReplicas {
replicas = b.MinReplicas
scalingInLimited = true
}
if replicas > b.MaxReplicas {
replicas = b.MaxReplicas
scalingOutLimited = true
}
return replicas, scalingInLimited || scalingOutLimited, nil
}
func applyCounterOrListPolicy(c *autoscalingv1.CounterPolicy, l *autoscalingv1.ListPolicy,
f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister,
nodeCounts map[string]gameservers.NodeCount) (int32, bool, error) {
if !runtime.FeatureEnabled(runtime.FeatureCountsAndLists) {
return 0, false, errors.Errorf("cannot apply CounterPolicy unless feature flag %s is enabled", runtime.FeatureCountsAndLists)
}
var isCounter bool // True if a CounterPolicy False if a ListPolicy
var key string // The specified Counter or List
var count int64 // The Count or number of Values in the template Game Server
var capacity int64 // The Capacity in the template Game Server
var aggCount int64 // The Aggregate Count of the specified Counter or List of all GameServers across the GameServerSet in the Fleet
var aggCapacity int64 // The Aggregate Capacity of the specified Counter or List of all GameServers across the GameServerSet in the Fleet
var aggAllocatedCount int64 // The Aggregate Count of the specified Counter or List of GameServers in an Allocated state across the GameServerSet in the Fleet
var minCapacity int64 // The Minimum Aggregate Capacity
var maxCapacity int64 // The Maximum Aggregate Capacity
var bufferSize intstr.IntOrString
if c != nil {
isCounter = true
counter, ok := f.Spec.Template.Spec.Counters[c.Key]
if !ok {
return 0, false, errors.Errorf("cannot apply CounterPolicy as Counter key %s does not exist in the Fleet Spec", c.Key)
}
aggCounter, ok := f.Status.Counters[c.Key]
if !ok {
return 0, false, errors.Errorf("cannot apply CounterPolicy as Counter key %s does not exist in the Fleet Status", c.Key)
}
key = c.Key
count = counter.Count
capacity = counter.Capacity
aggCount = aggCounter.Count
aggCapacity = aggCounter.Capacity
aggAllocatedCount = aggCounter.AllocatedCount
minCapacity = c.MinCapacity
maxCapacity = c.MaxCapacity
bufferSize = c.BufferSize
} else {
isCounter = false
list, ok := f.Spec.Template.Spec.Lists[l.Key]
if !ok {
return 0, false, errors.Errorf("cannot apply ListPolicy as List key %s does not exist in the Fleet Spec", l.Key)
}
aggList, ok := f.Status.Lists[l.Key]
if !ok {
return 0, false, errors.Errorf("cannot apply ListPolicy as List key %s does not exist in the Fleet Status", l.Key)
}
key = l.Key
count = int64(len(list.Values))
capacity = list.Capacity
aggCount = aggList.Count
aggCapacity = aggList.Capacity
aggAllocatedCount = aggList.AllocatedCount
minCapacity = l.MinCapacity
maxCapacity = l.MaxCapacity
bufferSize = l.BufferSize
}
// Checks if we've limited by TOTAL capacity
limited, scale := isLimited(aggCapacity, minCapacity, maxCapacity)
// Total current number of Replicas
replicas := f.Status.Replicas
// The buffer is the desired available capacity
var buffer int64
switch {
// Desired replicas based on BufferSize specified as an absolute value (i.e. 5)
case bufferSize.Type == intstr.Int:
buffer = int64(bufferSize.IntValue())
// Desired replicas based on BufferSize specified as a percent (i.e. 5%)
case bufferSize.Type == intstr.String:
bufferPercent, err := intstr.GetValueFromIntOrPercent(&bufferSize, 100, isCounter)
if err != nil {
return 0, false, err
}
// If the Aggregated Allocated Counts is 0 then desired capacity gets calculated as 0. If the
// capacity of 1 replica is equal to or greater than minimum capacity we can exit early.
if aggAllocatedCount <= 0 && capacity >= minCapacity {
return 1, true, nil
}
// The desired TOTAL capacity based on the Aggregated Allocated Counts (see applyBufferPolicy for explanation)
desiredCapacity := int64(math.Ceil(float64(aggAllocatedCount*100) / float64(100-bufferPercent)))
// Convert into a desired AVAILABLE capacity aka the buffer
buffer = desiredCapacity - aggAllocatedCount
}
// Current available capacity across the TOTAL fleet
switch availableCapacity := aggCapacity - aggCount; {
case availableCapacity == buffer:
if limited {
return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas,
capacity, aggCapacity, minCapacity, maxCapacity)
}
return replicas, false, nil
case availableCapacity < buffer: // Scale Up
if limited && scale == -1 { // Case where we want to scale up but we're already limited by MaxCapacity
return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas,
capacity, aggCapacity, minCapacity, maxCapacity)
}
return scaleUp(replicas, capacity, count, aggCapacity, availableCapacity, maxCapacity,
minCapacity, buffer)
case availableCapacity > buffer: // Scale Down
if limited && scale == 1 { // Case where we want to scale down but we're already limited by MinCapacity
return scaleLimited(scale, f, gameServerLister, nodeCounts, key, isCounter, replicas,
capacity, aggCapacity, minCapacity, maxCapacity)
}
return scaleDown(f, gameServerLister, nodeCounts, key, isCounter, replicas, aggCount,
aggCapacity, minCapacity, buffer)
}
if isCounter {
return 0, false, errors.Errorf("unable to apply CounterPolicy %v", c)
}
return 0, false, errors.Errorf("unable to apply ListPolicy %v", l)
}
func applySchedulePolicy(s *autoscalingv1.SchedulePolicy, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) {
// Ensure the scheduled autoscaler feature gate is enabled
if !runtime.FeatureEnabled(runtime.FeatureScheduledAutoscaler) {
return 0, false, errors.Errorf("cannot apply SchedulePolicy unless feature flag %s is enabled", runtime.FeatureScheduledAutoscaler)
}
if isScheduleActive(s, currentTime) {
return computeDesiredFleetSize(s.Policy, f, gameServerLister, nodeCounts)
}
// If the schedule wasn't active then return the current replica amount of the fleet
return f.Status.Replicas, false, &InactiveScheduleError{}
}
func applyChainPolicy(c autoscalingv1.ChainPolicy, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister, nodeCounts map[string]gameservers.NodeCount, currentTime time.Time) (int32, bool, error) {
// Ensure the scheduled autoscaler feature gate is enabled
if !runtime.FeatureEnabled(runtime.FeatureScheduledAutoscaler) {
return 0, false, errors.Errorf("cannot apply ChainPolicy unless feature flag %s is enabled", runtime.FeatureScheduledAutoscaler)
}
replicas := f.Status.Replicas
var limited bool
var err error
// Loop over all entries in the chain
for _, entry := range c {
switch entry.Type {
case autoscalingv1.SchedulePolicyType:
replicas, limited, err = applySchedulePolicy(entry.Schedule, f, gameServerLister, nodeCounts, currentTime)
// If no error was returned from the schedule policy (schedule is active and/or webhook policy within schedule was successful), then return the values given
if err == nil {
return replicas, limited, nil
}
case autoscalingv1.WebhookPolicyType:
replicas, limited, err = applyWebhookPolicy(entry.Webhook, f)
// If no error was returned from the webhook policy, then return the values given
if err == nil {
return replicas, limited, nil
}
default:
// Every other policy type we just want to compute the desired fleet and return it
return computeDesiredFleetSize(entry.FleetAutoscalerPolicy, f, gameServerLister, nodeCounts)
}
}
// Fall off the chain
return replicas, limited, err
}
// isScheduleActive checks if a chain entry's is active and returns a boolean, true if active, false otherwise
func isScheduleActive(s *autoscalingv1.SchedulePolicy, currentTime time.Time) bool {
// Used for checking ahead of the schedule for daylight savings purposes
cronDelta := (time.Minute * -1) + (time.Second * -30)
// If the current time is before the start time, the schedule is inactive so return false
startTime := s.Between.Start.Time
if currentTime.Before(startTime) {
return false
}
// If an end time is present and the current time is after the end time, the schedule is inactive so return false
endTime := s.Between.End.Time
if !endTime.IsZero() && currentTime.After(endTime) {
return false
}
// If no startCron field is specified, then it's automatically true (duration is no longer relevant since we're always running)
if s.ActivePeriod.StartCron == "" {
return true
}
// Ignore the error as validation is already done within the validateChainPolicy after being unmarshalled
location, _ := time.LoadLocation(s.ActivePeriod.Timezone)
// Ignore the error as validation is already done within the validateChainPolicy after being unmarshalled
startCron, _ := cron.ParseStandard(s.ActivePeriod.StartCron)
// Ignore the error as validation is already done within the validateChainPolicy after being unmarshalled.
// If the duration is empty set it to the largest duration possible (290 years)
duration, _ := time.ParseDuration(s.ActivePeriod.Duration)
if s.ActivePeriod.Duration == "" {
duration, _ = time.ParseDuration(maxDuration)
}
// Get the current time - duration
currentTimeMinusDuration := currentTime.Add(duration * -1)
// Take (current time - duration) to get the first available start time
cronStartTime := startCron.Next(currentTimeMinusDuration.In(location))
// Take the (cronStartTime + duration) to get the end time
cronEndTime := cronStartTime.Add(duration)
// If the current time is after the cronStartTime - 90 seconds (for daylight saving purposes) AND the current time before the cronEndTime
// then return true
// Example: startCron = 0 14 * * * // 2:00 PM Everyday | duration = 1 hr | cronDelta = 90 seconds | currentTime = 2024-08-01T14:30:00Z | currentTimeMinusDuration = 2024-08-01T13:30:00Z
// then cronStartTime = 2024-08-01T14:00:00Z and cronEndTime = 2024-08-01T15:00:00Z
// and since currentTime > cronStartTime + cronDelta AND currentTime < cronEndTime, we return true
if currentTime.After(cronStartTime.Add(cronDelta)) && currentTime.Before(cronEndTime) {
return true
}
return false
}
// getSortedGameServers returns the list of Game Servers for the Fleet in the order in which the
// Game Servers would be deleted.
func getSortedGameServers(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister,
nodeCounts map[string]gameservers.NodeCount) ([]*agonesv1.GameServer, error) {
gsList, err := fleets.ListGameServersByFleetOwner(gameServerLister, f)
if err != nil {
return nil, err
}
gameServers := gssets.SortGameServersByStrategy(f.Spec.Scheduling, gsList, nodeCounts, f.Spec.Priorities)
return gameServers, nil
}
// isLimited indicates that the calculated scale would be above or below the range defined by
// MinCapacity and MaxCapacity in the ListPolicy or CounterPolicy.
// Return 1 if the fleet needs to scale up, -1 if the fleets need to scale down, 0 if the fleet does
// not need to scale, or if the fleet is not limited.
func isLimited(aggCapacity, minCapacity, maxCapacity int64) (bool, int) {
if aggCapacity < minCapacity { // Scale up
return true, 1
}
if aggCapacity > maxCapacity { // Scale down
return true, -1
}
return false, 0
}
// scaleUpLimited scales up the fleet to meet the MinCapacity
func scaleUpLimited(replicas int32, capacity, aggCapacity, minCapacity int64) (int32, bool, error) {
if capacity == 0 {
return 0, false, errors.Errorf("cannot scale up as Capacity is equal to 0")
}
for aggCapacity < minCapacity {
aggCapacity += capacity
replicas++
}
return replicas, true, nil
}
// scaleDownLimited scales down the fleet to meet the MaxCapacity
func scaleDownLimited(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister,
nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32,
aggCapacity, maxCapacity int64) (int32, bool, error) {
// Game Servers in order of deletion on scale down
gameServers, err := getSortedGameServers(f, gameServerLister, nodeCounts)
if err != nil {
return 0, false, err
}
for _, gs := range gameServers {
if aggCapacity <= maxCapacity {
break
}
switch isCounter {
case true:
if counter, ok := gs.Status.Counters[key]; ok {
aggCapacity -= counter.Capacity
}
case false:
if list, ok := gs.Status.Lists[key]; ok {
aggCapacity -= list.Capacity
}
}
replicas--
}
// We are not currently able to scale down to zero replicas, so one replica is the minimum allowed
if replicas < 1 {
replicas = 1
}
return replicas, true, nil
}
func scaleLimited(scale int, f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister,
nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32,
capacity, aggCapacity, minCapacity, maxCapacity int64) (int32, bool, error) {
switch scale {
case 1: // scale up
return scaleUpLimited(replicas, capacity, aggCapacity, minCapacity)
case -1: // scale down
return scaleDownLimited(f, gameServerLister, nodeCounts, key, isCounter, replicas,
aggCapacity, maxCapacity)
case 0:
return replicas, false, nil
}
return 0, false, errors.Errorf("cannot scale due to error in scaleLimited function")
}
// scaleUp scales up for either Integer or Percentage Buffer.
func scaleUp(replicas int32, capacity, count, aggCapacity, availableCapacity, maxCapacity,
minCapacity, buffer int64) (int32, bool, error) {
// How much capacity is gained by adding one more replica to the fleet.
replicaCapacity := capacity - count
if replicaCapacity <= 0 {
return 0, false, errors.Errorf("cannot scale up as adding additional replicas does not increase available Capacity")
}
additionalReplicas := int32(math.Ceil((float64(buffer) - float64(availableCapacity)) / float64(replicaCapacity)))
// Check to make sure we're not limited (over Max Capacity)
limited, _ := isLimited(aggCapacity+(int64(additionalReplicas)*capacity), minCapacity, maxCapacity)
if limited {
additionalReplicas = int32((maxCapacity - aggCapacity) / capacity)
}
return replicas + additionalReplicas, limited, nil
}
// scaleDown scales down for either Integer or Percentage Buffer.
func scaleDown(f *agonesv1.Fleet, gameServerLister listeragonesv1.GameServerLister,
nodeCounts map[string]gameservers.NodeCount, key string, isCounter bool, replicas int32,
aggCount, aggCapacity, minCapacity, buffer int64) (int32, bool, error) {
// Exit early if we're already at MinCapacity to avoid calling getSortedGameServers if unnecessary
if aggCapacity == minCapacity {
return replicas, true, nil
}
// We first need to get the individual game servers in order of deletion on scale down, as any
// game server may have a unique value for counts and / or capacity.
gameServers, err := getSortedGameServers(f, gameServerLister, nodeCounts)
if err != nil {
return 0, false, err
}
var availableCapacity int64
// "Remove" one game server at a time in order of potential deletion. (Not actually removed here,
// that's done later, if possible, by the fleetautoscaler controller.)
for _, gs := range gameServers {
replicas--
switch isCounter {
case true:
if counter, ok := gs.Status.Counters[key]; ok {
aggCount -= counter.Count
aggCapacity -= counter.Capacity
} else {
continue
}
case false:
if list, ok := gs.Status.Lists[key]; ok {
aggCount -= int64(len(list.Values))
aggCapacity -= list.Capacity
} else {
continue
}
}
availableCapacity = aggCapacity - aggCount
// Check if we've overshot our buffer
if availableCapacity < buffer {
return replicas + 1, false, nil
}
// Check if we're Limited (Below MinCapacity)
if aggCapacity < minCapacity {
return replicas + 1, true, nil
}
// Check if we're at our desired Buffer
if availableCapacity == buffer {
return replicas, false, nil
}
// Check if we're at Limited
if aggCapacity == minCapacity {
return replicas, true, nil
}
}
// We are not currently able to scale down to zero replicas, so one replica is the minimum allowed.
if replicas < 1 {
replicas = 1
}
return replicas, false, nil
}