diff --git a/controllers/helpers_test.go b/controllers/helpers_test.go index b975a7e7..380c23f1 100644 --- a/controllers/helpers_test.go +++ b/controllers/helpers_test.go @@ -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{ @@ -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, @@ -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"), } } diff --git a/controllers/upgrade.go b/controllers/upgrade.go index ade49b48..cc1612e0 100644 --- a/controllers/upgrade.go +++ b/controllers/upgrade.go @@ -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) ) // replace latest string with latest version number diff --git a/controllers/upgrade_test.go b/controllers/upgrade_test.go index 948fd999..35d8e718 100644 --- a/controllers/upgrade_test.go +++ b/controllers/upgrade_test.go @@ -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)