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: handle konnect runtime groups pagination properly #841

Merged
merged 1 commit into from
Feb 8, 2023
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
15 changes: 12 additions & 3 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,18 @@ func fetchKongControlPlaneID(ctx context.Context,
func fetchKongRuntimeGroupID(ctx context.Context,
client *konnect.Client,
) (string, error) {
runtimeGroups, _, err := client.RuntimeGroups.List(ctx, nil)
if err != nil {
return "", fmt.Errorf("fetching runtime groups: %w", err)
var runtimeGroups []*konnect.RuntimeGroup
var listOpt *konnect.ListOpt
for {
currentRuntimeGroups, next, err := client.RuntimeGroups.List(ctx, listOpt)
if err != nil {
return "", fmt.Errorf("fetching runtime groups: %w", err)
}
runtimeGroups = append(runtimeGroups, currentRuntimeGroups...)
if next == nil {
break
}
listOpt = next
}
if konnectRuntimeGroup == "" {
konnectRuntimeGroup = defaultRuntimeGroupName
Expand Down
11 changes: 8 additions & 3 deletions konnect/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ const (
func (c *Client) list(ctx context.Context,
endpoint string, opt *ListOpt,
) ([]json.RawMessage, *ListOpt, error) {
if opt != nil && opt.Size > 100 {
opt.Size = 100
pageSize := 100
if opt != nil {
if opt.Size > 100 {
opt.Size = pageSize
} else {
pageSize = opt.Size
}
}

req, err := c.NewRequest("GET", endpoint, opt, nil)
Expand All @@ -47,7 +52,7 @@ func (c *Client) list(ctx context.Context,
if len(list.Data) > 0 && list.Page != list.PageCount {
next = &ListOpt{
Page: list.Page + 1,
Size: opt.Size,
Size: pageSize,
}
}

Expand Down
2 changes: 1 addition & 1 deletion utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func retryPolicy(ctx context.Context, resp *http.Response, err error) (bool, err
// 429 Too Many Requests is recoverable. Sometimes the server puts
// a Retry-After response header to indicate when the server is
// available to start processing request from client.
if resp.StatusCode == http.StatusTooManyRequests {
if resp != nil && resp.StatusCode == http.StatusTooManyRequests {
return true, nil
}
return false, nil
Expand Down