This repository was archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasg.go
More file actions
204 lines (175 loc) · 5.33 KB
/
asg.go
File metadata and controls
204 lines (175 loc) · 5.33 KB
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
package possum
import (
"context"
"fmt"
"strconv"
"time"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
)
const minSizeTag = "possum:min_size"
func DoAutoScalingGroups(ctx context.Context, client autoscalingiface.AutoScalingAPI, ts time.Time, schedules Schedules) (Changes, error) {
groups, err := getAutoScalingGroups(ctx, client)
if err != nil {
return nil, err
}
changes := getASGGroupChanges(groups, ts, schedules)
err = performASGChanges(client, changes)
return changes, err
}
type GroupSchedule struct {
resource *autoscaling.Group
schedule string
}
func getAutoScalingGroups(ctx context.Context, client autoscalingiface.AutoScalingAPI) ([]*GroupSchedule, error) {
// first we need to get all autoscaling tag description that has the tag key in it, since DescribeAutoScalingGroups
// doesn't have a filter for tags
tagParams := &autoscaling.DescribeTagsInput{
Filters: []*autoscaling.Filter{
{Name: aws.String("key"), Values: []*string{aws.String(scheduleTag)}},
},
}
var groupNames []*string
var schemas []string
tagCollector := func(page *autoscaling.DescribeTagsOutput, lastPage bool) bool {
for _, tag := range page.Tags {
groupNames = append(groupNames, tag.ResourceId)
schemas = append(schemas, *tag.Value)
}
return true
}
err := client.DescribeTagsPagesWithContext(ctx, tagParams, tagCollector)
if err != nil {
return nil, err
}
// no autoscaling group has been tagged with scheduleTag
if len(groupNames) == 0 {
return nil, nil
}
// now we have a list of asg
params := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: groupNames,
}
var result []*GroupSchedule
collector := func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for i, group := range page.AutoScalingGroups {
result = append(result, &GroupSchedule{
resource: group,
schedule: schemas[i],
})
}
return true
}
err = client.DescribeAutoScalingGroupsPagesWithContext(ctx, params, collector)
return result, err
}
func getASGGroupChanges(list []*GroupSchedule, ts time.Time, schedules Schedules) Changes {
var changes Changes
for _, a := range list {
group := a.resource
// skip groups that are in a transitional state
if *group.DesiredCapacity != int64(len(group.Instances)) {
continue
}
// if any scaling processes are suspended, we back off to ensure we don't muck with any active transitions
if len(group.SuspendedProcesses) > 0 {
continue
}
effectiveSchedule := schedules.Find(a.schedule)
if effectiveSchedule == nil {
log.Printf("WARN could not find schedule %s for group %s", a.schedule, *getASGName(group))
continue
}
isRunning := len(group.Instances) != 0
act := effectiveSchedule.Action(ts, isRunning)
if act == NoopAction {
continue
}
changes = append(changes, Change{
ID: group.AutoScalingGroupName,
Name: *getASGName(group),
Action: act,
Type: "asg",
minSize: getASGTagInt64(group.Tags, minSizeTag, 1),
currentMinSize: *group.MinSize,
})
}
return changes
}
func performASGChanges(client autoscalingiface.AutoScalingAPI, changes []Change) error {
for _, change := range changes {
switch change.Action {
case StartAction:
err := updateASGSize(client, change.ID, change.minSize)
if err != nil {
return err
}
case StopAction:
err := updateASGSize(client, change.ID, 0)
if err != nil {
return err
}
// tag current min size so that the StartAction can reset the value to this value
if err := tagASGGroupSize(client, change.ID, change.currentMinSize); err != nil {
return err
}
}
}
return nil
}
func updateASGSize(client autoscalingiface.AutoScalingAPI, name *string, minSize int64) error {
params := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: name,
MinSize: aws.Int64(minSize),
DesiredCapacity: aws.Int64(minSize),
}
_, err := client.UpdateAutoScalingGroup(params)
return err
}
func tagASGGroupSize(client autoscalingiface.AutoScalingAPI, name *string, minSize int64) error {
_, err := client.CreateOrUpdateTags(&autoscaling.CreateOrUpdateTagsInput{
Tags: []*autoscaling.Tag{
{
ResourceId: name,
Key: aws.String(minSizeTag),
Value: aws.String(fmt.Sprintf("%d", minSize)),
PropagateAtLaunch: aws.Bool(false),
ResourceType: aws.String("auto-scaling-group"),
},
},
})
return err
}
// getASGTagInt64 returns a int64 value parsed from the a specific AutoScalingGroups tag key, if parsing fails, return the defaultVal
func getASGTagInt64(tags []*autoscaling.TagDescription, tagKey string, defaultVal int64) int64 {
val := getASGTagValue(tags, tagKey)
if val == nil {
return defaultVal
}
i, err := strconv.ParseInt(*val, 10, 64)
if err != nil {
return defaultVal
}
return i
}
// helper to get a specific value out of autoscaling tag descriptions
func getASGTagValue(tags []*autoscaling.TagDescription, keyName string) *string {
if tags == nil {
return nil
}
for _, tag := range tags {
if *tag.Key == keyName {
return tag.Value
}
}
return nil
}
func getASGName(group *autoscaling.Group) *string {
name := getASGTagValue(group.Tags, "Name")
if name != nil {
return name
}
return group.AutoScalingGroupName
}