Skip to content

Commit 28433ea

Browse files
committed
Wait for eventual consistency on AWS spot instances
Also requesting a spot instance is subject to eventual consistency, this patch implements a retry method around spot instance waiter. Signed-off-by: Alessio Caiazza <acaiazza@gitlab.com>
1 parent 6f12672 commit 28433ea

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

drivers/amazonec2/amazonec2.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ const (
4545
)
4646

4747
const (
48-
keypairNotFoundCode = "InvalidKeyPair.NotFound"
48+
keypairNotFoundCode = "InvalidKeyPair.NotFound"
49+
spotInstanceRequestNotFoundCode = "InvalidSpotInstanceRequestID.NotFound"
4950
)
5051

5152
var (
@@ -640,11 +641,21 @@ func (d *Driver) Create() error {
640641
}
641642

642643
log.Info("Waiting for spot instance...")
643-
err = d.getClient().WaitUntilSpotInstanceRequestFulfilled(&ec2.DescribeSpotInstanceRequestsInput{
644-
SpotInstanceRequestIds: []*string{spotInstanceRequest.SpotInstanceRequests[0].SpotInstanceRequestId},
645-
})
646-
if err != nil {
647-
return fmt.Errorf("Error fulfilling spot request: %v", err)
644+
for i := 0; i < 3; i++ {
645+
// AWS eventual consistency means we could not have SpotInstanceRequest ready yet
646+
err = d.getClient().WaitUntilSpotInstanceRequestFulfilled(&ec2.DescribeSpotInstanceRequestsInput{
647+
SpotInstanceRequestIds: []*string{spotInstanceRequest.SpotInstanceRequests[0].SpotInstanceRequestId},
648+
})
649+
if err != nil {
650+
if awsErr, ok := err.(awserr.Error); ok {
651+
if awsErr.Code() == spotInstanceRequestNotFoundCode {
652+
time.Sleep(5 * time.Second)
653+
continue
654+
}
655+
}
656+
return fmt.Errorf("Error fulfilling spot request: %v", err)
657+
}
658+
break
648659
}
649660
log.Info("Created spot instance request %v", *spotInstanceRequest.SpotInstanceRequests[0].SpotInstanceRequestId)
650661
// resolve instance id

0 commit comments

Comments
 (0)