-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.go
185 lines (159 loc) · 4.24 KB
/
scale.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
package main
import (
"net/url"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
rabbithole "github.com/michaelklishin/rabbit-hole"
"github.com/prometheus/client_golang/prometheus"
)
func scaleRunner() {
//Autoscale Group Struct
asg := cfg.ASG
for _, asg := range asg {
switch method := asg.Method; method {
case "scale":
Info.Println("scale method firing")
scaleCluster(asg)
default:
Info.Println("Nothing to do")
}
}
}
var (
scaleUpBy = 4
scaleDownBy = 2
asgDesired int
asgMin int
asgMax int
)
func scaleCluster(asg ASG) {
for {
asgDesired, asgMin, asgMax = getAutoScaleDesired(asg)
// Report current count to prometheus exporter
promASGcount.With(prometheus.Labels{"name": asg.AsGroupName}).Set(float64(asgDesired))
rmqc := amqScaleConnection(asg)
// Need to handle timeouts...
mq, err := rmqc.GetQueue("/", asg.Queue)
if err != nil {
Warning.Printf("Error : %s", err)
}
Info.Printf("%s = %d\n", asg.Queue, mq.Messages)
if mq.Messages < asg.Watermark {
scaleDown(asg)
} else if mq.Messages >= asg.Threshold {
scaleUp(asg)
} else {
Info.Printf("Queue within normal range, doing nothing\n")
}
Info.Printf("ASG: %s, Current: %d, Min: %d, Max: %d\n", asg.AsGroupName, asgDesired, asgMin, asgMax)
time.Sleep(95 * time.Second)
}
}
func scaleUp(asg ASG) {
var d int
d = asgDesired + scaleUpBy
if d >= asgMax && asgDesired < asgMax {
scaleASG(asg, asgMax)
} else if d < asgMax {
scaleASG(asg, d)
} else {
Info.Printf("Workers Already Scaled up to Max\n")
}
}
func scaleDown(asg ASG) {
var d int
d = asgDesired - scaleDownBy
if d < asgMin && asgDesired > asgMin {
scaleASG(asg, asgMin)
} else if d >= asgMin {
scaleASG(asg, d)
} else {
Info.Printf("Workers Already Scaled to Min\n")
}
}
func amqScaleConnection(q ASG) *rabbithole.Client {
amqURL := os.Getenv(q.AmqHost)
if amqURL == "" {
Warning.Panic("Missing AMQ Environment Variable")
}
u, err := url.Parse(amqURL)
if err != nil {
Warning.Panic(err)
}
password, _ := u.User.Password()
rmqc, _ := rabbithole.NewClient(u.Scheme+"://"+u.Host, u.User.Username(), password)
return rmqc
}
func getAutoScaleDesired(asg ASG) (int, int, int) {
var desired int
var min int
var max int
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
Warning.Panic("failed to load config, " + err.Error())
}
cfg.Region = asg.AWSRegion
svc := autoscaling.New(cfg)
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []string{
asg.AsGroupName,
},
}
req := svc.DescribeAutoScalingGroupsRequest(input)
result, err := req.Send()
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeInvalidNextToken:
Warning.Println(autoscaling.ErrCodeInvalidNextToken, aerr.Error())
case autoscaling.ErrCodeResourceContentionFault:
Warning.Println(autoscaling.ErrCodeResourceContentionFault, aerr.Error())
default:
Warning.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
Warning.Println(err.Error())
}
}
for _, g := range result.AutoScalingGroups {
desired = int(*g.DesiredCapacity)
min = int(*g.MinSize)
max = int(*g.MaxSize)
}
return desired, min, max
}
func scaleASG(asg ASG, desired int) {
Info.Printf("I will scale ASG to: %d\n", desired)
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
Warning.Panic("failed to load config, " + err.Error())
}
cfg.Region = asg.AWSRegion
svc := autoscaling.New(cfg)
var coolDown = true
if asg.DisableCoolDown {
coolDown = false
}
desiredCapacity := int64(desired)
input := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: &asg.AsGroupName,
DesiredCapacity: &desiredCapacity,
HonorCooldown: &coolDown,
}
if asg.Enabled {
promASGscale.With(prometheus.Labels{"name": asg.AsGroupName}).Inc()
req := svc.SetDesiredCapacityRequest(input)
resp, err := req.Send()
if err != nil {
Info.Println("Scaling Failed, Cooldown window may be active")
Info.Println(resp)
}
} else {
Info.Println("Scaling is disabled: envvar Enabled: True to enable")
}
}