Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: creating a retryable kong client for tests and retry logic in konnect authentication for CI rate-limit errors #1381

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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...)
}
Expand Down
Loading