forked from Wattpad/kube-sqs-autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (77 loc) · 2.85 KB
/
main.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
package main
import (
"flag"
"time"
log "github.com/Sirupsen/logrus"
"github.com/Wattpad/kube-sqs-autoscaler/scale"
"github.com/Wattpad/kube-sqs-autoscaler/sqs"
)
var (
pollInterval time.Duration
scaleDownCoolPeriod time.Duration
scaleUpCoolPeriod time.Duration
scaleUpMessages int
scaleDownMessages int
maxPods int
minPods int
awsRegion string
sqsQueueUrl string
kubernetesDeploymentName string
kubernetesNamespace string
)
func Run(p *scale.PodAutoScaler, sqs *sqs.SqsClient) {
lastScaleUpTime := time.Now()
lastScaleDownTime := time.Now()
for {
select {
case <-time.After(pollInterval):
{
numMessages, err := sqs.NumMessages()
if err != nil {
log.Errorf("Failed to get SQS messages: %v", err)
continue
}
if numMessages >= scaleUpMessages {
if lastScaleUpTime.Add(scaleUpCoolPeriod).After(time.Now()) {
log.Info("Waiting for cool down, skipping scale up ")
continue
}
if err := p.ScaleUp(); err != nil {
log.Errorf("Failed scaling up: %v", err)
continue
}
lastScaleUpTime = time.Now()
}
if numMessages <= scaleDownMessages {
if lastScaleDownTime.Add(scaleDownCoolPeriod).After(time.Now()) {
log.Info("Waiting for cool down, skipping scale down")
continue
}
if err := p.ScaleDown(); err != nil {
log.Errorf("Failed scaling down: %v", err)
continue
}
lastScaleDownTime = time.Now()
}
}
}
}
}
func main() {
flag.DurationVar(&pollInterval, "poll-period", 5*time.Second, "The interval in seconds for checking if scaling is required")
flag.DurationVar(&scaleDownCoolPeriod, "scale-down-cool-down", 30*time.Second, "The cool down period for scaling down")
flag.DurationVar(&scaleUpCoolPeriod, "scale-up-cool-down", 10*time.Second, "The cool down period for scaling up")
flag.IntVar(&scaleUpMessages, "scale-up-messages", 100, "Number of sqs messages queued up required for scaling up")
flag.IntVar(&scaleDownMessages, "scale-down-messages", 10, "Number of messages required to scale down")
flag.IntVar(&maxPods, "max-pods", 5, "Max pods that kube-sqs-autoscaler can scale")
flag.IntVar(&minPods, "min-pods", 1, "Min pods that kube-sqs-autoscaler can scale")
flag.StringVar(&awsRegion, "aws-region", "", "Your AWS region")
flag.StringVar(&sqsQueueUrl, "sqs-queue-url", "", "The sqs queue url")
flag.StringVar(&kubernetesDeploymentName, "kubernetes-deployment", "", "Kubernetes Deployment to scale. This field is required")
flag.StringVar(&kubernetesNamespace, "kubernetes-namespace", "default", "The namespace your deployment is running in")
flag.Parse()
p := scale.NewPodAutoScaler(kubernetesDeploymentName, kubernetesNamespace, maxPods, minPods)
sqs := sqs.NewSqsClient(sqsQueueUrl, awsRegion)
log.Info("Starting kube-sqs-autoscaler")
Run(p, sqs)
}