Skip to content

Commit 5db3084

Browse files
deliahuvishalbollu
authored andcommitted
Add retries for resource tagging (#1188)
(cherry picked from commit 8b5d3a3)
1 parent 80d977e commit 5db3084

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

cli/cmd/cluster.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
2526
"github.com/cortexlabs/cortex/cli/cluster"
2627
"github.com/cortexlabs/cortex/cli/types/cliconfig"
2728
"github.com/cortexlabs/cortex/pkg/consts"
@@ -138,20 +139,12 @@ var _upCmd = &cobra.Command{
138139
exit.Error(err)
139140
}
140141

141-
err = createBucketIfNotFound(awsClient, clusterConfig.Bucket)
142-
if err != nil {
143-
exit.Error(err)
144-
}
145-
err = awsClient.TagBucket(clusterConfig.Bucket, clusterConfig.Tags)
142+
err = createBucketIfNotFound(awsClient, clusterConfig.Bucket, clusterConfig.Tags)
146143
if err != nil {
147144
exit.Error(err)
148145
}
149146

150-
err = createLogGroupIfNotFound(awsClient, clusterConfig.LogGroup)
151-
if err != nil {
152-
exit.Error(err)
153-
}
154-
err = awsClient.TagLogGroup(clusterConfig.LogGroup, clusterConfig.Tags)
147+
err = createLogGroupIfNotFound(awsClient, clusterConfig.LogGroup, clusterConfig.Tags)
155148
if err != nil {
156149
exit.Error(err)
157150
}
@@ -725,7 +718,7 @@ func getCloudFormationURLWithAccessConfig(accessConfig *clusterconfig.AccessConf
725718
return getCloudFormationURL(*accessConfig.ClusterName, *accessConfig.Region)
726719
}
727720

728-
func createBucketIfNotFound(awsClient *aws.Client, bucket string) error {
721+
func createBucketIfNotFound(awsClient *aws.Client, bucket string, tags map[string]string) error {
729722
bucketFound, err := awsClient.DoesBucketExist(bucket)
730723
if err != nil {
731724
return err
@@ -737,14 +730,28 @@ func createBucketIfNotFound(awsClient *aws.Client, bucket string) error {
737730
fmt.Print("\n\n")
738731
return err
739732
}
740-
fmt.Println(" ✓")
741733
} else {
742-
fmt.Println("○ using existing s3 bucket:", bucket, "✓")
734+
fmt.Print("○ using existing s3 bucket:", bucket)
743735
}
744-
return nil
736+
737+
// retry since it's possible that it takes some time for the new bucket to be registered by AWS
738+
for i := 0; i < 10; i++ {
739+
err = awsClient.TagBucket(bucket, tags)
740+
if err == nil {
741+
fmt.Println(" ✓")
742+
return nil
743+
}
744+
if !aws.IsNoSuchBucketErr(err) {
745+
break
746+
}
747+
time.Sleep(1 * time.Second)
748+
}
749+
750+
fmt.Print("\n\n")
751+
return err
745752
}
746753

747-
func createLogGroupIfNotFound(awsClient *aws.Client, logGroup string) error {
754+
func createLogGroupIfNotFound(awsClient *aws.Client, logGroup string, tags map[string]string) error {
748755
logGroupFound, err := awsClient.DoesLogGroupExist(logGroup)
749756
if err != nil {
750757
return err
@@ -756,12 +763,25 @@ func createLogGroupIfNotFound(awsClient *aws.Client, logGroup string) error {
756763
fmt.Print("\n\n")
757764
return err
758765
}
759-
fmt.Println(" ✓")
760766
} else {
761-
fmt.Println("○ using existing cloudwatch log group:", logGroup, "✓")
767+
fmt.Print("○ using existing cloudwatch log group:", logGroup)
762768
}
763769

764-
return nil
770+
// retry since it's possible that it takes some time for the new log group to be registered by AWS
771+
for i := 0; i < 10; i++ {
772+
err = awsClient.TagLogGroup(logGroup, tags)
773+
if err == nil {
774+
fmt.Println(" ✓")
775+
return nil
776+
}
777+
if !aws.IsErrCode(err, cloudwatchlogs.ErrCodeResourceNotFoundException) {
778+
break
779+
}
780+
time.Sleep(1 * time.Second)
781+
}
782+
783+
fmt.Print("\n\n")
784+
return err
765785
}
766786

767787
// createOrClearDashboard creates a new dashboard (or clears an existing one if it already exists)

pkg/lib/aws/errors.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/aws/aws-sdk-go/aws/awserr"
23+
"github.com/aws/aws-sdk-go/service/s3"
2324
"github.com/cortexlabs/cortex/pkg/lib/errors"
2425
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2526
)
@@ -43,15 +44,11 @@ func IsNotFoundErr(err error) bool {
4344
}
4445

4546
func IsNoSuchKeyErr(err error) bool {
46-
return IsErrCode(err, "NoSuchKey")
47+
return IsErrCode(err, s3.ErrCodeNoSuchKey)
4748
}
4849

4950
func IsNoSuchBucketErr(err error) bool {
50-
return IsErrCode(err, "NoSuchBucket")
51-
}
52-
53-
func IsForbiddenErr(err error) bool {
54-
return IsErrCode(err, "Forbidden")
51+
return IsErrCode(err, s3.ErrCodeNoSuchBucket)
5552
}
5653

5754
func IsGenericNotFoundErr(err error) bool {

0 commit comments

Comments
 (0)