Copilot commented on code in PR #102: URL: https://github.com/apache/cloudstack-kubernetes-provider/pull/102#discussion_r4036448384
########## pagination.go: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package cloudstack + +import "k8s.io/klog/v2" + +// maxListPages bounds a single walk. Because the total is re-read on every page, +// a result set that grows as fast as it is consumed would otherwise keep the +// walk going indefinitely. It is a backstop, not a limit expected to be reached: +// at CloudStack's default page size it allows half a million records. +const maxListPages = 1000 + +// pageableParams is the paging surface that every cloudstack-go List*Params +// type exposes. +type pageableParams interface { + SetPage(int) + SetPagesize(int) +} + +// listAll makes further requests to fetch the remaining items if the count is higher +// than the number of items returned. +func listAll[T any](p pageableParams, list func() (count int, items []T, err error)) ([]T, error) { + count, items, err := list() + if err != nil { + return nil, err + } + + // Nothing was truncated, or there is nothing to page through. + if len(items) >= count || len(items) == 0 { + return items, nil + } + + // The server just demonstrated how many records it will return at a time, + // which is the one page size it is guaranteed to accept. + pageSize := len(items) + collected := items + + for page := 2; len(collected) < count; page++ { + // The total is re-read on every page, so a result set that keeps growing + // keeps the walk going. Bound it rather than risk spinning forever. + if page > maxListPages { + klog.Warningf("stopped paging after %d pages holding %d of %d records; results may be incomplete", + maxListPages, len(collected), count) + break + } + + p.SetPage(page) + p.SetPagesize(pageSize) Review Comment: These requests use offset-based `page`/`pagesize`, so inserting or deleting a VM before the current offset shifts all later records. The deduplication added at the call sites only removes repeats; it cannot recover skipped records—for example, deleting one VM from page 1 can make a still-running node at the old page-2 boundary disappear from `verifyHosts`, causing reconciliation to fail. Please use a stable cursor/order supported by CloudStack or retry/abort when the result set changes instead of treating this walk as complete. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
