Skip to content

Commit

Permalink
Merge pull request #5 from TwinProduction/mixed-instance-policy-overr…
Browse files Browse the repository at this point in the history
…ides

Classify instance as outdated if instance type is no longer part of MixedInstancePolicy's instance types overrides
  • Loading branch information
TwiN authored May 13, 2020
2 parents dfe78b1 + 5e6a779 commit 3e730cc
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ be spun up before draining the old nodes. This is much better, because simply us
completely useless in the event that the ASG's update on the launch configuration/template is a change of instance type.


## Behavior

On interval, this application:
1. Iterates over each ASG defined by the `AUTO_SCALING_GROUP_NAMES` environment variable
2. Iterates over each instances of each ASGs
3. Checks if there's any instances with an outdated launch template version
4. **If ASG uses MixedInstancesPolicy**, checks if there's any instances with an instance type that isn't part of the list of instance type overrides
5. Checks if there's any instances with an outdated launch configuration
6. If any of the conditions defined in the step 3, 4 or 5 are met for any instance, begin the rolling update process for that instance


## Usage

| Environment variable | Description | Required | Default |
Expand Down
20 changes: 17 additions & 3 deletions cloudtest/cloudtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (m *MockAutoScalingService) UpdateAutoScalingGroup(_ *autoscaling.UpdateAut
return &autoscaling.UpdateAutoScalingGroupOutput{}, nil
}

func CreateTestAutoScalingGroup(name, launchConfigurationName string, launchTemplateSpecification *autoscaling.LaunchTemplateSpecification, instances []*autoscaling.Instance) *autoscaling.Group {
func CreateTestAutoScalingGroup(name, launchConfigurationName string, launchTemplateSpecification *autoscaling.LaunchTemplateSpecification, instances []*autoscaling.Instance, withMixedInstancesPolicy bool) *autoscaling.Group {
asg := &autoscaling.Group{
AutoScalingGroupName: aws.String(name),
Instances: instances,
Expand All @@ -111,8 +111,21 @@ func CreateTestAutoScalingGroup(name, launchConfigurationName string, launchTemp
if len(launchConfigurationName) != 0 {
asg.SetLaunchConfigurationName(launchConfigurationName)
}
if launchTemplateSpecification != nil {
asg.SetLaunchTemplate(launchTemplateSpecification)
if withMixedInstancesPolicy {
asg.SetMixedInstancesPolicy(&autoscaling.MixedInstancesPolicy{
LaunchTemplate: &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: launchTemplateSpecification,
Overrides: []*autoscaling.LaunchTemplateOverrides{
{InstanceType: aws.String("c5.2xlarge")},
{InstanceType: aws.String("c5n.2xlarge")},
{InstanceType: aws.String("c5d.2xlarge")},
},
},
})
} else {
if launchTemplateSpecification != nil {
asg.SetLaunchTemplate(launchTemplateSpecification)
}
}
return asg
}
Expand All @@ -121,6 +134,7 @@ func CreateTestAutoScalingInstance(id, launchConfigurationName string, launchTem
instance := &autoscaling.Instance{
LifecycleState: aws.String(lifeCycleState),
InstanceId: aws.String(id),
InstanceType: aws.String("c5.2xlarge"),
}
if len(launchConfigurationName) != 0 {
instance.SetLaunchConfigurationName(launchConfigurationName)
Expand Down
1 change: 0 additions & 1 deletion k8s/client_test.go

This file was deleted.

17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,23 @@ func SeparateOutdatedFromUpdatedInstances(asg *autoscaling.Group, ec2Svc ec2ifac
}
targetLaunchConfiguration := asg.LaunchConfigurationName
targetLaunchTemplate := asg.LaunchTemplate
var targetLaunchTemplateOverrides []*autoscaling.LaunchTemplateOverrides
if targetLaunchTemplate == nil && asg.MixedInstancesPolicy != nil && asg.MixedInstancesPolicy.LaunchTemplate != nil {
if config.Get().Debug {
log.Printf("[%s] using mixed instances policy launch template", aws.StringValue(asg.AutoScalingGroupName))
}
targetLaunchTemplate = asg.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification
targetLaunchTemplateOverrides = asg.MixedInstancesPolicy.LaunchTemplate.Overrides
}
if targetLaunchTemplate != nil {
return SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplate, asg.Instances, ec2Svc)
return SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplate, targetLaunchTemplateOverrides, asg.Instances, ec2Svc)
} else if targetLaunchConfiguration != nil {
return SeparateOutdatedFromUpdatedInstancesUsingLaunchConfiguration(targetLaunchConfiguration, asg.Instances)
}
return nil, nil, errors.New("AutoScalingGroup has neither launch template nor launch configuration")
}

func SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplate *autoscaling.LaunchTemplateSpecification, instances []*autoscaling.Instance, ec2Svc ec2iface.EC2API) ([]*autoscaling.Instance, []*autoscaling.Instance, error) {
func SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplate *autoscaling.LaunchTemplateSpecification, overrides []*autoscaling.LaunchTemplateOverrides, instances []*autoscaling.Instance, ec2Svc ec2iface.EC2API) ([]*autoscaling.Instance, []*autoscaling.Instance, error) {
var (
oldInstances []*autoscaling.Instance
newInstances []*autoscaling.Instance
Expand Down Expand Up @@ -319,6 +321,8 @@ func SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplat
case aws.StringValue(instance.LaunchTemplate.LaunchTemplateId) != aws.StringValue(targetLaunchTemplate.LaunchTemplateId):
fallthrough
case !compareLaunchTemplateVersions(targetTemplate, targetLaunchTemplate, instance.LaunchTemplate):
fallthrough
case overrides != nil && len(overrides) > 0 && !isInstanceTypePartOfLaunchTemplateOverrides(overrides, instance.InstanceType):
oldInstances = append(oldInstances, instance)
default:
newInstances = append(newInstances, instance)
Expand All @@ -327,6 +331,15 @@ func SeparateOutdatedFromUpdatedInstancesUsingLaunchTemplate(targetLaunchTemplat
return oldInstances, newInstances, nil
}

func isInstanceTypePartOfLaunchTemplateOverrides(overrides []*autoscaling.LaunchTemplateOverrides, instanceType *string) bool {
for _, override := range overrides {
if aws.StringValue(override.InstanceType) == aws.StringValue(instanceType) {
return true
}
}
return false
}

func SeparateOutdatedFromUpdatedInstancesUsingLaunchConfiguration(targetLaunchConfigurationName *string, instances []*autoscaling.Instance) ([]*autoscaling.Instance, []*autoscaling.Instance, error) {
var (
oldInstances []*autoscaling.Instance
Expand Down
Loading

0 comments on commit 3e730cc

Please sign in to comment.