Skip to content

Commit f2956f7

Browse files
authored
Merge pull request #4662 from giantswarm/ignore-notfound-asg
🐛 There is no need to check instance refresh if ASG is not found
2 parents 5780796 + d44d5f5 commit f2956f7

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
@@ -228,13 +228,30 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
228228
ec2Svc := r.getEC2Service(ec2Scope)
229229
asgsvc := r.getASGService(clusterScope)
230230

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

264-
// Find existing ASG
265-
asg, err := r.findASG(machinePoolScope, asgsvc)
266-
if err != nil {
267-
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
268-
return err
269-
}
270-
271281
if asg == nil {
272282
// Create new ASG
273283
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
@@ -324,9 +324,6 @@ func (s *Service) CanStartASGInstanceRefresh(scope *scope.MachinePoolScope) (boo
324324
describeInput := &autoscaling.DescribeInstanceRefreshesInput{AutoScalingGroupName: aws.String(scope.Name())}
325325
refreshes, err := s.ASGClient.DescribeInstanceRefreshesWithContext(context.TODO(), describeInput)
326326
if err != nil {
327-
if awserrors.IsNotFound(err) {
328-
return false, nil
329-
}
330327
return false, err
331328
}
332329
hasUnfinishedRefresh := false

pkg/cloud/services/autoscaling/autoscalinggroup_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,17 +1126,6 @@ func TestServiceCanStartASGInstanceRefresh(t *testing.T) {
11261126
Return(nil, awserrors.NewConflict("some error"))
11271127
},
11281128
},
1129-
{
1130-
name: "should NOT return error if describe instance failed due to 'not found'",
1131-
wantErr: false,
1132-
canStart: false,
1133-
expect: func(m *mock_autoscalingiface.MockAutoScalingAPIMockRecorder) {
1134-
m.DescribeInstanceRefreshesWithContext(context.TODO(), gomock.Eq(&autoscaling.DescribeInstanceRefreshesInput{
1135-
AutoScalingGroupName: aws.String("machinePoolName"),
1136-
})).
1137-
Return(nil, awserrors.NewNotFound("not found"))
1138-
},
1139-
},
11401129
{
11411130
name: "should return true if no instance available for refresh",
11421131
wantErr: false,

0 commit comments

Comments
 (0)