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

fix panic when using MixedInstancesPolicy #298

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 47 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ func createASGInstance(instanceID string, launchConfigName string) *autoscaling.
}
}

func createASGInstanceWithLaunchTemplate(instanceID string, launchTemplateName string) *autoscaling.Instance {
return &autoscaling.Instance{
InstanceId: &instanceID,
LaunchTemplate: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: &launchTemplateName,
},
AvailabilityZone: aws.String("az-1"),
LifecycleState: aws.String("InService"),
}
}

func createEc2Instances() []*ec2.Instance {
return []*ec2.Instance{
&ec2.Instance{
Expand Down Expand Up @@ -203,6 +214,40 @@ func createASG(asgName string, launchConfigName string) *autoscaling.Group {
}
}

func createASGWithLaunchTemplate(asgName string, launchTemplate string) *autoscaling.Group {
return &autoscaling.Group{
AutoScalingGroupName: &asgName,
LaunchTemplate: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: &asgName,
},
Instances: []*autoscaling.Instance{
createASGInstance("mock-instance-1", launchTemplate),
createASGInstance("mock-instance-2", launchTemplate),
createASGInstance("mock-instance-3", launchTemplate),
},
DesiredCapacity: func(x int) *int64 { i := int64(x); return &i }(3),
}
}

func createASGWithMixedInstanceLaunchTemplate(asgName string, launchTemplate string) *autoscaling.Group {
return &autoscaling.Group{
AutoScalingGroupName: &asgName,
MixedInstancesPolicy: &autoscaling.MixedInstancesPolicy{
LaunchTemplate: &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: &asgName,
},
},
},
Instances: []*autoscaling.Instance{
createASGInstance("mock-instance-1", launchTemplate),
createASGInstance("mock-instance-2", launchTemplate),
createASGInstance("mock-instance-3", launchTemplate),
},
DesiredCapacity: func(x int) *int64 { i := int64(x); return &i }(3),
}
}

func createDriftedASG(asgName string, launchConfigName string) *autoscaling.Group {
return &autoscaling.Group{
AutoScalingGroupName: &asgName,
Expand All @@ -221,6 +266,8 @@ func createASGs() []*autoscaling.Group {
createASG("mock-asg-1", "mock-launch-config-1"),
createDriftedASG("mock-asg-2", "mock-launch-config-2"),
createASG("mock-asg-3", "mock-launch-config-3"),
createASGWithLaunchTemplate("mock-asg-4", "mock-launch-template-4"),
createASGWithMixedInstanceLaunchTemplate("mock-asg-5", "mock-launch-template-5"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (r *RollingUpgradeContext) IsInstanceDrifted(instance *autoscaling.Instance
launchTemplateName = aws.StringValue(scalingGroup.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateName)
instanceTemplateName = aws.StringValue(instance.LaunchTemplate.LaunchTemplateName)
instanceTemplateVersion = aws.StringValue(instance.LaunchTemplate.Version)
templateVersion = aws.StringValue(scalingGroup.LaunchTemplate.Version)
templateVersion = aws.StringValue(scalingGroup.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.Version)
backjo marked this conversation as resolved.
Show resolved Hide resolved
)

// replace latest string with latest version number
Expand Down
19 changes: 19 additions & 0 deletions controllers/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,49 @@ func TestIsInstanceDrifted(t *testing.T) {
TestDescription string
Reconciler *RollingUpgradeReconciler
Instance *autoscaling.Instance
AsgName *string
ExpectedValue bool
}{
{
"Instance has the same launch config as the ASG, expect false from IsInstanceDrifted",
createRollingUpgradeReconciler(t),
createASGInstance("mock-instance-1", "mock-launch-config-1"),
aws.String("mock-asg-1"),
false,
},
{
"Instance has different launch config from the ASG, expect true from IsInstanceDrifted",
createRollingUpgradeReconciler(t),
createASGInstance("mock-instance-1", "different-launch-config"),
aws.String("mock-asg-1"),
true,
},
{
"Instance has no launch config, expect true from IsInstanceDrifted",
createRollingUpgradeReconciler(t),
createASGInstance("mock-instance-1", ""),
aws.String("mock-asg-1"),
true,
},
{
"Instance has launch template, expect true from IsInstanceDrifted",
createRollingUpgradeReconciler(t),
createASGInstanceWithLaunchTemplate("mock-instance-1", "mock-launch-template-4"),
aws.String("mock-asg-4"),
true,
},
{
"Instance has mixed instances launch template, expect true from IsInstanceDrifted",
createRollingUpgradeReconciler(t),
createASGInstanceWithLaunchTemplate("mock-instance-1", "mock-launch-template-5"),
aws.String("mock-asg-5"),
true,
},
}
for _, test := range tests {
rollupCtx := createRollingUpgradeContext(test.Reconciler)
rollupCtx.Cloud.ScalingGroups = createASGs()
rollupCtx.RollingUpgrade.Spec.AsgName = *test.AsgName
actualValue := rollupCtx.IsInstanceDrifted(test.Instance)
if actualValue != test.ExpectedValue {
t.Errorf("Test Description: %s \n expected value: %v, actual value: %v", test.TestDescription, test.ExpectedValue, actualValue)
Expand Down