-
Notifications
You must be signed in to change notification settings - Fork 179
/
handler.go
349 lines (276 loc) · 7.68 KB
/
handler.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
package main
import (
"context"
"fmt"
"math"
"os"
"strconv"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ecs"
)
var (
CloudFormation = cloudformation.New(session.New(), nil)
ECS = ecs.New(session.New(), nil)
)
type Metrics struct {
Cpu int64
Memory int64
Width int64
}
func Handler(ctx context.Context) error {
largest, total, err := clusterMetrics()
if err != nil {
return err
}
log("largest = %+v\n", largest)
log("total = %+v\n", total)
desired, err := desiredCapacity(largest, total)
if err != nil {
return err
}
if err := autoscale(desired); err != nil {
return err
}
return nil
}
func autoscale(desired int64) error {
stack := os.Getenv("STACK")
debug("desired = %+v\n", desired)
debug("stack = %+v\n", stack)
ds := fmt.Sprintf("%d", desired)
res, err := CloudFormation.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: aws.String(stack),
})
if err != nil {
return err
}
if len(res.Stacks) < 1 {
return fmt.Errorf("could not find stack: %s", stack)
}
req := &cloudformation.UpdateStackInput{
Capabilities: []*string{aws.String("CAPABILITY_IAM")},
Parameters: []*cloudformation.Parameter{},
StackName: aws.String(stack),
UsePreviousTemplate: aws.Bool(true),
}
for _, p := range res.Stacks[0].Parameters {
switch *p.ParameterKey {
case "InstanceCount":
debug("ds = %+v\n", ds)
debug("*p.ParameterValue = %+v\n", *p.ParameterValue)
if ds == *p.ParameterValue {
fmt.Println("no change")
return nil
}
req.Parameters = append(req.Parameters, &cloudformation.Parameter{
ParameterKey: p.ParameterKey,
ParameterValue: aws.String(ds),
})
default:
req.Parameters = append(req.Parameters, &cloudformation.Parameter{
ParameterKey: p.ParameterKey,
UsePreviousValue: aws.Bool(true),
})
}
}
if _, err := CloudFormation.UpdateStack(req); err != nil {
return err
}
return nil
}
func clusterMetrics() (*Metrics, *Metrics, error) {
// start with enough room for a single one-off run
largest := &Metrics{Cpu: 128, Memory: 512}
total := &Metrics{Cpu: 128, Memory: 512}
req := &ecs.ListServicesInput{
Cluster: aws.String(os.Getenv("CLUSTER")),
LaunchType: aws.String("EC2"),
}
for {
res, err := ECS.ListServices(req)
if err != nil {
return nil, nil, err
}
debug("ListServices page: %d\n", len(res.ServiceArns))
dres, err := ECS.DescribeServices(&ecs.DescribeServicesInput{
Cluster: aws.String(os.Getenv("CLUSTER")),
Services: res.ServiceArns,
})
if err != nil {
return nil, nil, err
}
for _, s := range dres.Services {
debug("*s.ServiceName = %+v\n", *s.ServiceName)
for _, d := range s.Deployments {
res, err := ECS.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: d.TaskDefinition,
})
if err != nil {
return nil, nil, err
}
cpu := int64(0)
mem := int64(0)
width := int64(0)
for _, cd := range res.TaskDefinition.ContainerDefinitions {
cpu += ci(cd.Cpu)
mem += max(ci(cd.Memory), ci(cd.MemoryReservation))
if g := cd.DockerLabels["convox.generation"]; g == nil || *g != "2" {
agent := false
for _, pc := range s.PlacementConstraints {
if pc.Type != nil && *pc.Type == "distinctInstance" {
agent = true
break
}
}
debug("agent = %+v\n", agent)
debug("len(cd.PortMappings) = %+v\n", len(cd.PortMappings))
debug("*s.DesiredCount = %+v\n", *s.DesiredCount)
if len(cd.PortMappings) > 0 && !agent {
width = *s.DesiredCount
}
}
}
if *s.DesiredCount > 0 {
if cpu > largest.Cpu {
largest.Cpu = cpu
}
if mem > largest.Memory {
largest.Memory = mem
}
if width > largest.Width {
largest.Width = width
}
}
debug("cpu = %+v\n", cpu)
debug("mem = %+v\n", mem)
debug("width = %+v\n", width)
debug("largest = %+v\n", largest)
total.Cpu += (cpu * *s.DesiredCount)
total.Memory += (mem * *s.DesiredCount)
}
}
if res.NextToken == nil {
break
}
req.NextToken = res.NextToken
}
return largest, total, nil
}
func desiredCapacity(largest, total *Metrics) (int64, error) {
req := &ecs.ListContainerInstancesInput{
Cluster: aws.String(os.Getenv("CLUSTER")),
Status: aws.String("ACTIVE"),
}
// the total number of instances
totalCount := int64(0)
// the number of instances that can fit the largest container
extraFit := int64(0)
// the attributes of a single instance
single := map[string]int64{}
// the total capacity of the cluster
capacity := map[string]int64{}
for {
res, err := ECS.ListContainerInstances(req)
if err != nil {
return 0, err
}
debug("ListContainerInstances page: %d\n", len(res.ContainerInstanceArns))
totalCount += int64(len(res.ContainerInstanceArns))
dres, err := ECS.DescribeContainerInstances(&ecs.DescribeContainerInstancesInput{
Cluster: aws.String(os.Getenv("CLUSTER")),
ContainerInstances: res.ContainerInstanceArns,
})
if err != nil {
return 0, err
}
for _, ci := range dres.ContainerInstances {
remaining := map[string]int64{}
for _, rr := range ci.RegisteredResources {
if *rr.Type == "INTEGER" {
single[*rr.Name] = *rr.IntegerValue
capacity[*rr.Name] += *rr.IntegerValue
}
}
for _, rr := range ci.RemainingResources {
if *rr.Type == "INTEGER" {
remaining[*rr.Name] = *rr.IntegerValue
}
}
if remaining["CPU"] >= largest.Cpu && remaining["MEMORY"] >= largest.Memory {
extraFit += 1
}
}
debug("capacity = %+v\n", capacity)
debug("single = %+v\n", single)
if res.NextToken == nil {
break
}
req.NextToken = res.NextToken
}
if largest.Memory > single["MEMORY"] || largest.Cpu > single["CPU"] {
return 0, fmt.Errorf("largest container is bigger than a single instance")
}
// calculate the amount of extra capacity available in the cluster as a number of instances
capcpu := int64(math.Floor((float64(capacity["CPU"]) - float64(total.Cpu)) / float64(single["CPU"])))
capmem := int64(math.Floor((float64(capacity["MEMORY"]) - float64(total.Memory)) / float64(single["MEMORY"])))
// the extra aggregate instance capacity is the smaller of extra cpu and extra memory values
extraCapacity := min(capcpu, capmem)
// the number of instances over the amount required to fit the widest service (gen1 loadbalancer-facing)
extraWidth := totalCount - largest.Width
debug("extraCapacity = %+v\n", extraCapacity)
debug("extraFit = %+v\n", extraFit)
debug("extraWidth = %+v\n", extraWidth)
// comes from AutoscaleExtra
extra, err := strconv.ParseInt(os.Getenv("EXTRA"), 10, 64)
if err != nil {
return 0, err
}
// total desired count is the current instance count minus the smallest calculated extra type plus the number of desired extra instances
desired := totalCount - min(extraCapacity, extraFit, extraWidth) + extra
debug("desired = %+v\n", desired)
// minimum instance count is 3
if desired < 3 {
desired = 3
}
return desired, nil
}
func ci(ii ...*int64) int64 {
for _, i := range ii {
if i != nil && *i > 0 {
return *i
}
}
return 0
}
func debug(format string, a ...interface{}) {
if os.Getenv("DEBUG") == "true" {
fmt.Printf(format, a...)
}
}
func log(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
func max(ii ...int64) int64 {
m := int64(0)
for _, i := range ii {
if i > m {
m = i
}
}
return m
}
func min(ii ...int64) int64 {
m := int64(math.MaxInt64)
for _, i := range ii {
if i < m {
m = i
}
}
return m
}
func main() {
lambda.Start(Handler)
}