-
Notifications
You must be signed in to change notification settings - Fork 1
/
shrinker.go
130 lines (115 loc) · 3.38 KB
/
shrinker.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
package sigmund
import (
log "github.com/sirupsen/logrus"
"github.com/darkraiden/sigmund/pkg/autoscaling"
"github.com/darkraiden/sigmund/pkg/storage/dynamo"
)
// Shrink is the core function of the package
// which Executes an Autoscaling Group Policy
// when requirements are met
func (s *Sigmund) Shrink() {
dbClient, err := s.newDBClient(metricsTodbKey[s.Key].metric)
if err != nil {
s.Log.WithError(err).Panic("Could not connect to DynamoDB")
}
// Run a Select query
item, err = s.readDynamo(dbClient)
if err != nil {
s.Log.WithError(err).Panic("Could not read from DynamoDB")
}
switch s.Dynamo.Key.String() {
case "LowMemory":
err := dbClient.WriteToTable(s.Dynamo.TableName, "isLowMemory", true)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowMemory == true",
}).Panic()
}
item.IsLowMemory = true
case "OkMemory":
err := dbClient.WriteToTable(s.Dynamo.TableName, "isLowMemory", false)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowMemory == false",
}).Panic()
}
item.IsLowMemory = false
case "LowCPU":
err := dbClient.WriteToTable(s.Dynamo.TableName, "isLowCPU", true)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowCPU == true",
}).Panic()
}
item.IsLowCPU = true
case "OkCPU":
err := dbClient.WriteToTable(s.Dynamo.TableName, "isLowCPU", false)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowCPU == false",
}).Panic()
}
}
if item.IsLowCPU && item.IsLowMemory {
err = dbClient.WriteToTable(s.Dynamo.TableName, "isLowCPU", false)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowCPU == false",
}).Panic()
}
err = dbClient.WriteToTable(s.Dynamo.TableName, "isLowMemory", false)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"table name": s.Dynamo.TableName,
"metric": "isLowMemory == false",
}).Panic()
}
s.execASGPolicy()
}
}
func (s *Sigmund) newDBClient() (*dynamo.Client, error) {
dynamo, err := dynamo.New(s.Dynamo.TableName, s.Dynamo.Region, s.Dynamo.Key)
if err != nil {
return nil, err
}
client, err := dynamo.NewClient()
if err != nil {
return nil, err
}
return client, nil
}
func (s *Sigmund) readDynamo(cli *dynamo.Client) (*DBItem, error) {
item, err := cli.ReadFromTable(s.Dynamo.TableName)
if err != nil {
return nil, err
}
return &DBItem{
ID: item.ID,
IsLowCPU: item.IsLowCPU,
IsLowMemory: item.IsLowMemory,
}, nil
}
func (s *Sigmund) execASGPolicy() {
var client *autoscaling.Client
asg, err := autoscaling.New(s.Autoscaling.AutoScalingGroupName, s.Autoscaling.PolicyName, s.Autoscaling.Region)
if err != nil {
s.Log.WithError(err).WithFields(log.Fields{
"asg name": s.Autoscaling.AutoScalingGroupName,
"policy name": s.Autoscaling.PolicyName,
"region": s.Autoscaling.Region,
}).Panic("Failed to create autoscaling config")
}
client, err = asg.NewClient()
if err != nil {
s.Log.WithError(err).Panic("Failed to create autoscaling client")
}
err = client.TriggerPolicy(asg)
if err != nil {
s.Log.WithError(err).Panic("Failed to trigger asg policy")
}
}