Skip to content

Commit 7b926f4

Browse files
committed
feat: add retry logic for insufficient spot capacity errors
1 parent 96bd5b2 commit 7b926f4

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

builder/common/step_run_source_instance.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
285285
err = retry.Config{
286286
Tries: 11,
287287
ShouldRetry: func(err error) bool {
288-
if awserrors.Matches(err, "InvalidParameterValue", "iamInstanceProfile") {
289-
return true
290-
}
291-
return false
288+
return awserrors.Matches(err, "InvalidParameterValue", "iamInstanceProfile")
292289
},
293290
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
294291
}.Run(ctx, func(ctx context.Context) error {
@@ -326,10 +323,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
326323

327324
var r *ec2.DescribeInstancesOutput
328325
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
329-
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
330-
return true
331-
}
332-
return false
326+
return awserrors.Matches(err, "InvalidInstanceID.NotFound", "")
333327
},
334328
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
335329
}.Run(ctx, func(ctx context.Context) error {

builder/common/step_run_spot_instance.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
412412
// we can wait on those operations, this can be removed.
413413
return true
414414
}
415+
if err.Error() == "InsufficientInstanceCapacity" {
416+
return true
417+
}
415418
return false
416419
},
417420
RetryDelay: (&retry.Backoff{InitialBackoff: 500 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
@@ -432,6 +435,12 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
432435
if err != nil {
433436
log.Printf("create request failed %v", err)
434437
}
438+
// We can end up unavailable Spot capacity, we keep retrying
439+
for _, err := range createOutput.Errors {
440+
if err.ErrorCode != nil && *err.ErrorCode == "InsufficientInstanceCapacity" {
441+
return fmt.Errorf(*err.ErrorCode)
442+
}
443+
}
435444
return err
436445
})
437446

@@ -512,10 +521,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
512521

513522
// Retry creating tags for about 2.5 minutes
514523
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
515-
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
516-
return true
517-
}
518-
return false
524+
return awserrors.Matches(err, "InvalidInstanceID.NotFound", "")
519525
},
520526
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
521527
}.Run(ctx, func(ctx context.Context) error {

0 commit comments

Comments
 (0)