Skip to content

Commit d44d5f5

Browse files
fiunchinhoAndiDog
andcommitted
There is no need to check instance refresh if ASG is not found
Co-authored-by: Andreas Sommer <[email protected]>
1 parent b4506f8 commit d44d5f5

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

exp/controllers/awsmachinepool_controller.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,30 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
227227
ec2Svc := r.getEC2Service(ec2Scope)
228228
asgsvc := r.getASGService(clusterScope)
229229

230+
// Find existing ASG
231+
asg, err := r.findASG(machinePoolScope, asgsvc)
232+
if err != nil {
233+
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
234+
return err
235+
}
236+
230237
canUpdateLaunchTemplate := func() (bool, error) {
231238
// If there is a change: before changing the template, check if there exist an ongoing instance refresh,
232239
// because only 1 instance refresh can be "InProgress". If template is updated when refresh cannot be started,
233240
// that change will not trigger a refresh. Do not start an instance refresh if only userdata changed.
241+
if asg == nil {
242+
// If the ASG hasn't been created yet, there is no need to check if we can start the instance refresh.
243+
// But we want to update the LaunchTemplate because an error in the LaunchTemplate may be blocking the ASG creation.
244+
return true, nil
245+
}
234246
return asgsvc.CanStartASGInstanceRefresh(machinePoolScope)
235247
}
236248
runPostLaunchTemplateUpdateOperation := func() error {
249+
// skip instance refresh if ASG is not created yet
250+
if asg == nil {
251+
machinePoolScope.Debug("ASG does not exist yet, skipping instance refresh")
252+
return nil
253+
}
237254
// skip instance refresh if explicitly disabled
238255
if machinePoolScope.AWSMachinePool.Spec.RefreshPreferences != nil && machinePoolScope.AWSMachinePool.Spec.RefreshPreferences.Disable {
239256
machinePoolScope.Debug("instance refresh disabled, skipping instance refresh")
@@ -260,13 +277,6 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
260277
// set the LaunchTemplateReady condition
261278
conditions.MarkTrue(machinePoolScope.AWSMachinePool, expinfrav1.LaunchTemplateReadyCondition)
262279

263-
// Find existing ASG
264-
asg, err := r.findASG(machinePoolScope, asgsvc)
265-
if err != nil {
266-
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
267-
return err
268-
}
269-
270280
if asg == nil {
271281
// Create new ASG
272282
if err := r.createPool(machinePoolScope, clusterScope); err != nil {

exp/controllers/awsmachinepool_controller_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,6 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
208208
defer teardown(t, g)
209209
getASG(t, g)
210210

211-
ec2Svc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any())
212-
213211
_ = reconciler.reconcileNormal(context.Background(), ms, cs, cs)
214212

215213
g.Expect(ms.AWSMachinePool.Finalizers).To(ContainElement(expinfrav1.MachinePoolFinalizer))
@@ -253,11 +251,18 @@ func TestAWSMachinePoolReconciler(t *testing.T) {
253251
t.Helper()
254252
ms.AWSMachinePool.Spec.ProviderID = id
255253
}
254+
getASG := func(t *testing.T, g *WithT) {
255+
t.Helper()
256+
257+
ec2Svc.EXPECT().GetLaunchTemplate(gomock.Any()).Return(nil, "", nil).AnyTimes()
258+
asgSvc.EXPECT().GetASGByName(gomock.Any()).Return(nil, nil).AnyTimes()
259+
}
256260
t.Run("should look up by provider ID when one exists", func(t *testing.T) {
257261
g := NewWithT(t)
258262
setup(t, g)
259263
defer teardown(t, g)
260264
setProviderID(t, g)
265+
getASG(t, g)
261266

262267
expectedErr := errors.New("no connection available ")
263268
ec2Svc.EXPECT().ReconcileLaunchTemplate(gomock.Any(), gomock.Any(), gomock.Any()).Return(expectedErr)

pkg/cloud/services/autoscaling/autoscalinggroup.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@ func (s *Service) CanStartASGInstanceRefresh(scope *scope.MachinePoolScope) (boo
323323
describeInput := &autoscaling.DescribeInstanceRefreshesInput{AutoScalingGroupName: aws.String(scope.Name())}
324324
refreshes, err := s.ASGClient.DescribeInstanceRefreshesWithContext(context.TODO(), describeInput)
325325
if err != nil {
326-
if awserrors.IsNotFound(err) {
327-
return false, nil
328-
}
329326
return false, err
330327
}
331328
hasUnfinishedRefresh := false

pkg/cloud/services/autoscaling/autoscalinggroup_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,17 +1094,6 @@ func TestServiceCanStartASGInstanceRefresh(t *testing.T) {
10941094
Return(nil, awserrors.NewConflict("some error"))
10951095
},
10961096
},
1097-
{
1098-
name: "should NOT return error if describe instance failed due to 'not found'",
1099-
wantErr: false,
1100-
canStart: false,
1101-
expect: func(m *mock_autoscalingiface.MockAutoScalingAPIMockRecorder) {
1102-
m.DescribeInstanceRefreshesWithContext(context.TODO(), gomock.Eq(&autoscaling.DescribeInstanceRefreshesInput{
1103-
AutoScalingGroupName: aws.String("machinePoolName"),
1104-
})).
1105-
Return(nil, awserrors.NewNotFound("not found"))
1106-
},
1107-
},
11081097
{
11091098
name: "should return true if no instance available for refresh",
11101099
wantErr: false,

0 commit comments

Comments
 (0)