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

Add support for pagination #523

Merged
merged 3 commits into from
Nov 26, 2024
Merged

Conversation

jacobtomlinson
Copy link
Member

@jacobtomlinson jacobtomlinson commented Nov 26, 2024

This PR makes use of pagination when listing resources. This is expecially useful when working with clusters with large numbers of resources.

Breaking Changes

As a consequence the kr8s.get() and APIObject.list() methods have been changed to generators instead of returning lists. This allows us to iterate over all the resources in a Kubernetes cluster without using large amounts of memory.

This improves the API and makes it more Pythonic, but this is a breaking change for existing users.

Sync

In the sync API the kr8s.get() (and kr8s.api().get()) methods now return a generator instead of a list. This will break some indexing operations and require you to cast to a list to achieve the same functionality.

import kr8s

# Before
second_node = kr8s.get("nodes")[1]

# After
second_node = list(kr8s.get("nodes"))[1]

This also applies to methods that call this such as Nodes.list() or any other APIObject.list() method.

Async

In the async API the same applies and the kr8s.asyncio.get() method returns an async generator. To covert this to a list we need to asynchronously iterate over it.

import kr8s

# Before
nodes = await kr8s.asyncio.get("nodes")

# After
nodes = [node async for node in kr8s.get("nodes")]

This also changes the for functionality when looping over the method directly.

import kr8s

# Before
for node in await kr8s.get("nodes"):
    print(node.name)

# After
async for node in kr8s.get("nodes"):
    print(node.name)

Closes #477

@jacobtomlinson jacobtomlinson added the breaking A breaking change label Nov 26, 2024
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

Attention: Patch coverage is 97.64706% with 2 lines in your changes missing coverage. Please review.

Project coverage is 95.12%. Comparing base (87063fc) to head (f874f10).
Report is 140 commits behind head on main.

Files with missing lines Patch % Lines
kr8s/_api.py 88.88% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #523      +/-   ##
==========================================
+ Coverage   94.61%   95.12%   +0.50%     
==========================================
  Files          29       30       +1     
  Lines        3141     4019     +878     
==========================================
+ Hits         2972     3823     +851     
- Misses        169      196      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 26, 2024
@jacobtomlinson jacobtomlinson marked this pull request as ready for review November 26, 2024 14:46
@jacobtomlinson jacobtomlinson merged commit 91db268 into kr8s-org:main Nov 26, 2024
13 checks passed
@jacobtomlinson jacobtomlinson deleted the pagination branch November 26, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking change documentation Improvements or additions to documentation kr8s tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for pagination
1 participant