Skip to content

Commit 9535584

Browse files
authored
Merge pull request #4380 from cnmcavoy/cnmcavoy/asg-tidyup
Switch to constants for asg not found events, simplify asg lookup
2 parents 9154226 + 840a92d commit 9535584

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

exp/controllers/awsmachinepool_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (r *AWSMachinePoolReconciler) reconcileDelete(machinePoolScope *scope.Machi
347347

348348
if asg == nil {
349349
machinePoolScope.Debug("Unable to locate ASG")
350-
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeNormal, "NoASGFound", "Unable to find matching ASG")
350+
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeNormal, expinfrav1.ASGNotFoundReason, "Unable to find matching ASG")
351351
} else {
352352
machinePoolScope.SetASGStatus(asg.Status)
353353
switch asg.Status {
@@ -374,7 +374,7 @@ func (r *AWSMachinePoolReconciler) reconcileDelete(machinePoolScope *scope.Machi
374374

375375
if launchTemplate == nil {
376376
machinePoolScope.Debug("Unable to locate launch template")
377-
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeNormal, "NoASGFound", "Unable to find matching ASG")
377+
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeNormal, expinfrav1.ASGNotFoundReason, "Unable to find matching ASG")
378378
controllerutil.RemoveFinalizer(machinePoolScope.AWSMachinePool, expinfrav1.MachinePoolFinalizer)
379379
return ctrl.Result{}, nil
380380
}

exp/controllers/awsmachinepool_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
468468
g.Expect(err).To(BeNil())
469469
g.Expect(buf.String()).To(ContainSubstring("Unable to locate ASG"))
470470
g.Expect(ms.AWSMachinePool.Finalizers).To(ConsistOf(metav1.FinalizerDeleteDependents))
471-
g.Eventually(recorder.Events).Should(Receive(ContainSubstring("NoASGFound")))
471+
g.Eventually(recorder.Events).Should(Receive(ContainSubstring(expinfrav1.ASGNotFoundReason)))
472472
})
473473
t.Run("should cause AWSMachinePool to go into NotReady", func(t *testing.T) {
474474
g := NewWithT(t)

pkg/cloud/services/autoscaling/autoscalinggroup.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/aws/aws-sdk-go/service/autoscaling"
2525
"github.com/aws/aws-sdk-go/service/ec2"
2626
"github.com/pkg/errors"
27+
corev1 "k8s.io/api/core/v1"
2728
"k8s.io/utils/pointer"
2829

2930
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
@@ -126,34 +127,17 @@ func (s *Service) ASGIfExists(name *string) (*expinfrav1.AutoScalingGroup, error
126127
case err != nil:
127128
record.Eventf(s.scope.InfraCluster(), "FailedDescribeAutoScalingGroups", "failed to describe ASG %q: %v", *name, err)
128129
return nil, errors.Wrapf(err, "failed to describe AutoScaling Group: %q", *name)
130+
case len(out.AutoScalingGroups) == 0:
131+
record.Eventf(s.scope.InfraCluster(), corev1.EventTypeNormal, expinfrav1.ASGNotFoundReason, "Unable to find ASG matching %q", *name)
132+
return nil, nil
129133
}
130-
//TODO: double check if you're handling nil vals
131134
return s.SDKToAutoScalingGroup(out.AutoScalingGroups[0])
132135
}
133136

134137
// GetASGByName returns the existing ASG or nothing if it doesn't exist.
135138
func (s *Service) GetASGByName(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
136-
s.scope.Debug("Looking for existing AutoScalingGroup by name")
137-
138-
input := &autoscaling.DescribeAutoScalingGroupsInput{
139-
AutoScalingGroupNames: []*string{
140-
aws.String(scope.Name()),
141-
},
142-
}
143-
144-
out, err := s.ASGClient.DescribeAutoScalingGroups(input)
145-
switch {
146-
case awserrors.IsNotFound(err):
147-
return nil, nil
148-
case err != nil:
149-
record.Eventf(s.scope.InfraCluster(), "FailedDescribeInstances", "Failed to describe instances by tags: %v", err)
150-
return nil, errors.Wrap(err, "failed to describe instances by tags")
151-
case len(out.AutoScalingGroups) == 0:
152-
record.Eventf(scope.AWSMachinePool, "FailedDescribeInstances", "No Auto Scaling Groups with %s found", scope.Name())
153-
return nil, nil
154-
}
155-
156-
return s.SDKToAutoScalingGroup(out.AutoScalingGroups[0])
139+
name := scope.Name()
140+
return s.ASGIfExists(&name)
157141
}
158142

159143
// CreateASG runs an autoscaling group.

0 commit comments

Comments
 (0)