Paginate the initial list with limit/continue#1309
Open
casassg wants to merge 3 commits into
Open
Conversation
160cd7e to
8bc8527
Compare
The initial listing that precedes every watch fetches the whole collection in a single request and parses it into memory at once. For operators/observers that watch high-cardinality built-in resources (e.g. Pods or Jobs) cluster-wide, this produces a large peak memory spike on cold start, proportional to the total number of objects in the cluster. Add an opt-in `settings.watching.batch_size` that fetches the initial list in chunks using the Kubernetes API's `limit`/`continue` pagination, capping the peak memory footprint of the bootstrap listing. The snapshot resourceVersion (consistent across all chunks) is used to start the watch from the fully listed state. Defaults to `None`, preserving the current single-request behaviour. Signed-off-by: Gerard Casas Saez <gerardc@squareup.com>
Signed-off-by: Gerard Casas Saez <gerardc@squareup.com>
Signed-off-by: Gerard Casas Saez <gerardc@squareup.com>
8bc8527 to
8cf2f6a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add an opt-in
settings.watching.batch_sizeto fetch the initial listing (the list preceding every watch) inlimit/continuepages instead of one large request, capping the cold-start peak-memory spike for high-cardinality cluster-wide watches (e.g. Pods/Jobs).list_objs()paginates vialimit/continuewhenbatch_sizeis set, aggregating items across pages.resourceVersion(consistent across chunks) to start the watch from the fully-listed state.batch_sizedefaults toNone→ single unpaginated request, unchanged URL/params.tests/k8s/test_list_objs.py(kmock-based) covers the default single request, multi-page aggregation + query params, empty-continue stop, andkind/apiVersiondefaulting across pages.Why
list_objs()issues one unpaginatedGETand parses the entire collection into memory at once before the watch begins. For operators/observers watching high-cardinality built-in resources cluster-wide, the cold-start list materializes every object simultaneously — a peak-memory spike scaling with total object count.We hit this with a Prefect Kubernetes worker, which embeds a
kopfobserver watching all Pods/Jobs cluster-wide (prefect_kubernetes/observer.py): after a restart, the cold-start list of thousands of objects pushed the container past its memory limit and it crash-looped (OOMKilled). The Kubernetes API already supports chunked retrieval for exactly this (Retrieving large results in chunks), andclient-go's reflector paginates its list by default —kopfjust wasn't using it.Example usage