-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscale.go
More file actions
48 lines (43 loc) · 1.27 KB
/
scale.go
File metadata and controls
48 lines (43 loc) · 1.27 KB
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
package fleet
import (
"errors"
"math"
"strconv"
"strings"
)
// Scale creates or destroys units to match the desired number
func (c *FleetClient) Scale(component string, requested int) error {
if requested < 0 {
return errors.New("cannot scale below 0")
}
// check how many currently exist
components, err := c.Units(component)
if err != nil {
// skip checking the first time; we just want a tally
if !strings.Contains(err.Error(), "could not find unit") {
return err
}
}
timesToScale := int(math.Abs(float64(requested - len(components))))
if requested-len(components) > 0 {
return scaleUp(c, component, len(components), timesToScale)
} else {
return scaleDown(c, component, len(components), timesToScale)
}
}
func scaleUp(c *FleetClient, component string, numExistingContainers, numTimesToScale int) error {
for i := 0; i < numTimesToScale; i++ {
if err := c.Create([]string{component + "@" + strconv.Itoa(numExistingContainers+i+1)}); err != nil {
return err
}
}
return nil
}
func scaleDown(c *FleetClient, component string, numExistingContainers, numTimesToScale int) error {
for i := 0; i < numTimesToScale; i++ {
if err := c.Destroy([]string{component + "@" + strconv.Itoa(numExistingContainers-i)}); err != nil {
return err
}
}
return nil
}