Skip to content

Commit baabf33

Browse files
authored
Merge pull request #4698 from AndiDog/asg-recon-patch-cherrypick
🐛 There is no need to check instance refresh if ASG is not found (cherry-pick to release-2.3)
2 parents f7d9dda + bfc0a4d commit baabf33

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

exp/controllers/awsmachinepool_controller.go

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

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

262-
// Find existing ASG
263-
asg, err := r.findASG(machinePoolScope, asgsvc)
264-
if err != nil {
265-
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
266-
return err
267-
}
268-
269279
if asg == nil {
270280
// Create new ASG
271281
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_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ func TestServiceCanStartASGInstanceRefresh(t *testing.T) {
10911091
m.DescribeInstanceRefreshesWithContext(context.TODO(), gomock.Eq(&autoscaling.DescribeInstanceRefreshesInput{
10921092
AutoScalingGroupName: aws.String("machinePoolName"),
10931093
})).
1094-
Return(nil, awserrors.NewNotFound("not found"))
1094+
Return(nil, awserrors.NewConflict("some error"))
10951095
},
10961096
},
10971097
{

0 commit comments

Comments
 (0)