-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathasg.go
155 lines (128 loc) · 4.5 KB
/
asg.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
package scaler
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
const (
activitySucessfulStatusCode = "Successful"
userRequestForChangingDesiredCapacity = "a user request explicitly set group desired capacity changing the desired capacity"
)
type AutoscaleGroupDetails struct {
Pending int64
DesiredCount int64
MinSize int64
MaxSize int64
}
type ASGDriver struct {
Name string
Sess *session.Session
MaxDescribeScalingActivitiesPages int
}
func (a *ASGDriver) Describe() (AutoscaleGroupDetails, error) {
log.Printf("Collecting AutoScaling details for ASG %q", a.Name)
svc := autoscaling.New(a.Sess)
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(a.Name),
},
}
t := time.Now()
result, err := svc.DescribeAutoScalingGroups(input)
if err != nil {
return AutoscaleGroupDetails{}, err
}
queryDuration := time.Now().Sub(t)
asg := result.AutoScalingGroups[0]
var pending int64
for _, instance := range asg.Instances {
lifecycleState := aws.StringValue(instance.LifecycleState)
if strings.HasPrefix(lifecycleState, "Pending") {
pending += 1
}
}
details := AutoscaleGroupDetails{
Pending: pending,
DesiredCount: int64(*result.AutoScalingGroups[0].DesiredCapacity),
MinSize: int64(*result.AutoScalingGroups[0].MinSize),
MaxSize: int64(*result.AutoScalingGroups[0].MaxSize),
}
log.Printf("↳ Got pending=%d, desired=%d, min=%d, max=%d (took %v)",
details.Pending, details.DesiredCount, details.MinSize, details.MaxSize, queryDuration)
return details, nil
}
func (a *ASGDriver) SetDesiredCapacity(count int64) error {
svc := autoscaling.New(a.Sess)
input := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: aws.String(a.Name),
DesiredCapacity: aws.Int64(count),
HonorCooldown: aws.Bool(false),
}
_, err := svc.SetDesiredCapacity(input)
if err != nil {
return err
}
return nil
}
func (a *ASGDriver) GetAutoscalingActivities(ctx context.Context, nextToken *string) (*autoscaling.DescribeScalingActivitiesOutput, error) {
svc := autoscaling.New(a.Sess)
input := &autoscaling.DescribeScalingActivitiesInput{
AutoScalingGroupName: aws.String(a.Name),
NextToken: nextToken,
}
return svc.DescribeScalingActivitiesWithContext(ctx, input)
}
func (a *ASGDriver) GetLastScalingInAndOutActivity(ctx context.Context, findScaleOut, findScaleIn bool) (*autoscaling.Activity, *autoscaling.Activity, error) {
const scalingOutKey = "increasing the capacity"
const shrinkingKey = "shrinking the capacity"
var nextToken *string
var lastScalingOutActivity *autoscaling.Activity
var lastScalingInActivity *autoscaling.Activity
hasFoundScalingActivities := false
for i := 0; !hasFoundScalingActivities; {
i++
if a.MaxDescribeScalingActivitiesPages >= 0 && i >= a.MaxDescribeScalingActivitiesPages {
return lastScalingOutActivity, lastScalingInActivity, fmt.Errorf("%d exceedes allowed pages for autoscaling:DescribeScalingActivities, %d", i, a.MaxDescribeScalingActivitiesPages)
}
output, err := a.GetAutoscalingActivities(ctx, nextToken)
if err != nil {
return lastScalingOutActivity, lastScalingInActivity, err
}
for _, activity := range output.Activities {
// Filter for successful activity and explicit desired count changes
if *activity.StatusCode == activitySucessfulStatusCode &&
strings.Contains(*activity.Cause, userRequestForChangingDesiredCapacity) {
if lastScalingOutActivity == nil && strings.Contains(*activity.Cause, scalingOutKey) {
lastScalingOutActivity = activity
} else if lastScalingInActivity == nil && strings.Contains(*activity.Cause, shrinkingKey) {
lastScalingInActivity = activity
}
}
if findScaleOut && findScaleIn {
hasFoundScalingActivities = lastScalingOutActivity != nil && lastScalingInActivity != nil
} else if findScaleOut {
hasFoundScalingActivities = lastScalingOutActivity != nil
} else if findScaleIn {
hasFoundScalingActivities = lastScalingInActivity != nil
}
}
nextToken = output.NextToken
if nextToken == nil {
break
}
}
return lastScalingOutActivity, lastScalingInActivity, nil
}
type dryRunASG struct {
}
func (a *dryRunASG) Describe() (AutoscaleGroupDetails, error) {
return AutoscaleGroupDetails{}, nil
}
func (a *dryRunASG) SetDesiredCapacity(count int64) error {
return nil
}