forked from cloudfoundry/auctioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delta_force.go
67 lines (53 loc) · 1.23 KB
/
delta_force.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
package delta_force
type ActualInstance struct {
Index int
Guid string
}
type Result struct {
IndicesToStart []int
GuidsToStop []string
IndicesToStopAllButOne []int
}
func (r Result) Empty() bool {
return len(r.IndicesToStart) == 0 && len(r.GuidsToStop) == 0 && len(r.IndicesToStopAllButOne) == 0
}
type ActualInstances []ActualInstance
func (a ActualInstances) hasIndex(index int) bool {
for _, actual := range a {
if actual.Index == index {
return true
}
}
return false
}
func (a ActualInstances) numAtIndex(index int) int {
count := 0
for _, actual := range a {
if actual.Index == index {
count++
}
}
return count
}
func Reconcile(numDesired int, actuals ActualInstances) Result {
result := Result{}
for i := 0; i < numDesired; i++ {
if !actuals.hasIndex(i) {
result.IndicesToStart = append(result.IndicesToStart, i)
}
}
if len(result.IndicesToStart) > 0 {
return result
}
for _, actual := range actuals {
if actual.Index >= numDesired {
result.GuidsToStop = append(result.GuidsToStop, actual.Guid)
}
}
for i := 0; i < numDesired; i++ {
if actuals.numAtIndex(i) > 1 {
result.IndicesToStopAllButOne = append(result.IndicesToStopAllButOne, i)
}
}
return result
}