Skip to content

Commit

Permalink
fix: add retries
Browse files Browse the repository at this point in the history
  • Loading branch information
jokestax committed Jan 29, 2025
1 parent 027a84e commit 6c2005b
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions cmd/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import (
"github.com/spf13/viper"
)

const (
maxRetries = 20
baseDelay = 2 * time.Second
)

func createAws(cmd *cobra.Command, _ []string) error {
cliFlags, err := utilities.GetFlags(cmd, utilities.CloudProviderAWS)
if err != nil {
Expand Down Expand Up @@ -266,21 +271,13 @@ func convertLocalCredsToSession(ctx context.Context, stsClient stsClienter, iamC
// Create a session name (some unique identifier)
sessionName := fmt.Sprintf("kubefirst-session-%s", *callerIdentity.UserId)

time.Sleep(5 * time.Second)

// Assume the role
output, err := stsClient.AssumeRole(ctx, &sts.AssumeRoleInput{
RoleArn: aws.String(roleArn),
RoleSessionName: aws.String(sessionName),
DurationSeconds: aws.Int32(sessionDuration),
})
creds, err := assumeRoleWithRetry(ctx, stsClient, roleArn, sessionName)
if err != nil {
return nil, fmt.Errorf("failed to assume role %s: %w", roleArn, err)
}

// // Return the credentials
credentials := output.Credentials
return credentials, nil
return creds, nil
}

// AdditionalRolePolicies is a slice of policy ARNs you want to attach
Expand Down Expand Up @@ -523,3 +520,24 @@ func getSupportedInstanceTypes(ctx context.Context, p paginator, architecture st
}
return instanceTypes, nil
}

func assumeRoleWithRetry(ctx context.Context, stsClient stsClienter, roleArn, sessionName string) (*types.Credentials, error) {
var lastErr error
for attempt := 1; attempt <= maxRetries; attempt++ {
output, err := stsClient.AssumeRole(ctx, &sts.AssumeRoleInput{
RoleArn: aws.String(roleArn),
RoleSessionName: aws.String(sessionName),
})
if err == nil {
return output.Credentials, nil
}

lastErr = err

// Exponential backoff
delay := time.Duration(attempt*attempt) * baseDelay
time.Sleep(delay)
}

return nil, fmt.Errorf("failed to assume role after %d attempts: %w", maxRetries, lastErr)
}

0 comments on commit 6c2005b

Please sign in to comment.