Skip to content

Commit

Permalink
Allow passing api kwarg to APIObject.list()
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson committed Jan 22, 2025
1 parent 0036e77 commit df8b3ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit df8b3ca

Please sign in to comment.