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

Allow passing api kwarg to APIObject.list() #562

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,12 @@ def gen(cls, *args, **kwargs):
raise NotImplementedError("gen is not implemented for this object")

@classmethod
async def async_list(cls, **kwargs) -> AsyncGenerator[Self]:
api = await kr8s.asyncio.api()
async def async_list(cls, api: Api | None = None, **kwargs) -> AsyncGenerator[Self]:
if api is None:
if cls._asyncio:
api = await kr8s.asyncio.api()
else:
api = await kr8s.asyncio.api(_asyncio=False)
async for resource in api.async_get(kind=cls, **kwargs):
if isinstance(resource, cls):
yield resource
Expand All @@ -758,6 +762,7 @@ async def list(cls, **kwargs) -> AsyncGenerator[Self]:
"""List objects in Kubernetes.

Args:
api: An optional API object to use.
**kwargs: Keyword arguments to pass to :func:`kr8s.get`.

Returns:
Expand Down
20 changes: 19 additions & 1 deletion kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ async def test_pod_list():
assert {p.name for p in pods1} == {p.name for p in pods2}


async def test_pod_list_sync():
def test_pod_list_sync():
pods1 = [pod for pod in kr8s.get("pods", namespace=kr8s.ALL)]
pods2 = [pod for pod in SyncPod.list(namespace=kr8s.ALL)]
assert pods1 and pods2
Expand All @@ -1149,6 +1149,24 @@ async def test_pod_list_sync():
assert {p.name for p in pods1} == {p.name for p in pods2}


async def test_pod_list_api():
api = await kr8s.asyncio.api()
pods = [pod async for pod in Pod.list(namespace=kr8s.ALL, api=api)]
assert pods
assert pods[0].api
assert pods[0].api == api
assert pods[0].api._asyncio


async def test_pod_list_api_sync():
api = kr8s.api()
pods = [pod for pod in SyncPod.list(namespace=kr8s.ALL, api=api)]
assert pods
assert pods[0].api
assert pods[0].api == api
assert not pods[0].api._asyncio


@pytest.mark.parametrize(
"ports",
[
Expand Down
Loading