Skip to content

Commit

Permalink
fix: handle konnect runtime groups pagination properly
Browse files Browse the repository at this point in the history
  • Loading branch information
aboudreault committed Feb 8, 2023
1 parent 629712b commit 0ae9347
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
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

0 comments on commit 0ae9347

Please sign in to comment.