Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set last scale in and out event on autoscaler activity #52

Merged
merged 22 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lambda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,20 @@ func Handler(ctx context.Context, evt json.RawMessage) (string, error) {
}

client := buildkite.NewClient(token)

asg := &scaler.ASGDriver{
Name: mustGetEnv(`ASG_NAME`),
Sess: sess,
}
scaleOutOutput, scaleInOutput, err := asg.GetLastScalingInAndOutActivity()
keithduncan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Printf("Failed to get last scale in and out activity because %s", err)
}
if scaleInOutput != nil {
lastScaleIn = *scaleInOutput.StartTime
}
if scaleOutOutput != nil {
lastScaleOut = *scaleOutOutput.StartTime
}
params := scaler.Params{
BuildkiteQueue: mustGetEnv(`BUILDKITE_QUEUE`),
AutoScalingGroupName: mustGetEnv(`ASG_NAME`),
Expand Down
69 changes: 59 additions & 10 deletions scaler/asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ import (
"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
type ASGDriver struct {
Name string
Sess *session.Session
}

func (a *asgDriver) Describe() (AutoscaleGroupDetails, error) {
log.Printf("Collecting AutoScaling details for ASG %q", a.name)
func (a *ASGDriver) Describe() (AutoscaleGroupDetails, error) {
log.Printf("Collecting AutoScaling details for ASG %q", a.Name)

svc := autoscaling.New(a.sess)
svc := autoscaling.New(a.Sess)
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(a.name),
aws.String(a.Name),
},
}

Expand Down Expand Up @@ -64,10 +69,10 @@ func (a *asgDriver) Describe() (AutoscaleGroupDetails, error) {
return details, nil
}

func (a *asgDriver) SetDesiredCapacity(count int64) error {
svc := autoscaling.New(a.sess)
func (a *ASGDriver) SetDesiredCapacity(count int64) error {
svc := autoscaling.New(a.Sess)
input := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: aws.String(a.name),
AutoScalingGroupName: aws.String(a.Name),
DesiredCapacity: aws.Int64(count),
HonorCooldown: aws.Bool(false),
}
Expand All @@ -80,6 +85,50 @@ func (a *asgDriver) SetDesiredCapacity(count int64) error {
return nil
}

func (a *ASGDriver) GetAutoscalingActivities(nextToken *string) (*autoscaling.DescribeScalingActivitiesOutput, error) {
svc := autoscaling.New(a.Sess)
input := &autoscaling.DescribeScalingActivitiesInput{
AutoScalingGroupName: aws.String(a.Name),
NextToken: nextToken,
}
return svc.DescribeScalingActivities(input)
}

func (a *ASGDriver) GetLastScalingInAndOutActivity() (*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 !hasFoundScalingActivities {
output, err := a.GetAutoscalingActivities(nextToken)
if err != nil {
return nil, nil, 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 lastScalingOutActivity != nil && lastScalingInActivity != nil {
hasFoundScalingActivities = true
break
}
}
nextToken = output.NextToken
if nextToken == nil {
break
}
}
return lastScalingOutActivity, lastScalingInActivity, nil
}

type dryRunASG struct {
}

Expand Down
6 changes: 3 additions & 3 deletions scaler/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func NewScaler(client *buildkite.Client, sess *session.Session, params Params) (
scaler.metrics = &dryRunMetricsPublisher{}
}
} else {
scaler.autoscaling = &asgDriver{
name: params.AutoScalingGroupName,
sess: sess,
scaler.autoscaling = &ASGDriver{
Name: params.AutoScalingGroupName,
Sess: sess,
}

if params.PublishCloudWatchMetrics {
Expand Down