Copilot commented on code in PR #102: URL: https://github.com/apache/cloudstack-kubernetes-provider/pull/102#discussion_r3913363387
########## pagination.go: ########## @@ -0,0 +1,95 @@ +/* + * 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 + } Review Comment: When the page cap is hit, listAll currently logs a warning and returns partial results with a nil error. Several call sites treat these lists as authoritative for create/delete decisions, so silently returning an incomplete set can lead to incorrect reconciliation. Consider returning a sentinel error (and/or a completeness flag) so callers can abort/retry instead of acting on partial data. -- 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]
