diff --git a/cmd/common_konnect.go b/cmd/common_konnect.go index 16dcde56e..83ab7d283 100644 --- a/cmd/common_konnect.go +++ b/cmd/common_konnect.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/kong/go-database-reconciler/pkg/diff" "github.com/kong/go-database-reconciler/pkg/dump" @@ -16,12 +17,36 @@ import ( "golang.org/x/sync/errgroup" ) -const defaultControlPlaneName = "default" +const ( + defaultControlPlaneName = "default" + maxRetriesForAuth = 5 + apiRateLimitExceededErrorString = "API rate limit exceeded" +) func authenticate( ctx context.Context, client *konnect.Client, konnectConfig utils.KonnectConfig, ) (konnect.AuthResponse, error) { - return client.Auth.LoginV2(ctx, konnectConfig.Email, konnectConfig.Password, konnectConfig.Token) + attempts := 0 + backoff := 200 * time.Millisecond + + for { + authResponse, err := client.Auth.LoginV2(ctx, konnectConfig.Email, konnectConfig.Password, konnectConfig.Token) + if err == nil { + return authResponse, nil + } + + if !strings.Contains(err.Error(), apiRateLimitExceededErrorString) { + return authResponse, err + } + + attempts++ + if attempts > maxRetriesForAuth { + return authResponse, fmt.Errorf("maximum retries (%d) exceeded for authentication", maxRetriesForAuth) + } + + time.Sleep(backoff) + backoff *= 2 + } } // GetKongClientForKonnectMode abstracts the different cloud environments users diff --git a/tests/integration/test_utils.go b/tests/integration/test_utils.go index d20600359..d1d37f676 100644 --- a/tests/integration/test_utils.go +++ b/tests/integration/test_utils.go @@ -43,7 +43,8 @@ func getTestClient() (*kong.Client, error) { return cmd.GetKongClientForKonnectMode(ctx, &konnectConfig) } return utils.GetKongClient(utils.KongClientConfig{ - Address: getKongAddress(), + Address: getKongAddress(), + Retryable: true, }) } @@ -236,7 +237,7 @@ func reset(t *testing.T, opts ...string) { t.Helper() deckCmd := cmd.NewRootCmd() - args := []string{"reset", "--force"} + args := []string{"gateway", "reset", "--force"} if len(opts) > 0 { args = append(args, opts...) }