Skip to content

Commit

Permalink
Fix Custom Resource pagination hang (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Jan 7, 2025
1 parent ce3e0ac commit 8b2466c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions kr8s/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ async def _async_get_single(
if (
"metadata" in resourcelist
and "continue" in resourcelist["metadata"]
and resourcelist["metadata"]["continue"]
):
continue_paging = True
params["continue"] = resourcelist["metadata"]["continue"]
Expand Down
49 changes: 47 additions & 2 deletions kr8s/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,50 @@ async def example_deployment_spec(ns):
}


@pytest.fixture
async def example_crd_spec():
spec = {
"apiVersion": "apiextensions.k8s.io/v1",
"kind": "CustomResourceDefinition",
"metadata": {"name": "shirts.stable.example.com"},
"spec": {
"group": "stable.example.com",
"scope": "Namespaced",
"names": {"plural": "shirts", "singular": "shirt", "kind": "Shirt"},
"versions": [
{
"name": "v1",
"served": True,
"storage": True,
"schema": {
"openAPIV3Schema": {
"type": "object",
"properties": {
"spec": {
"type": "object",
"properties": {
"color": {"type": "string"},
"size": {"type": "string"},
},
}
},
}
},
"selectableFields": [
{"jsonPath": ".spec.color"},
{"jsonPath": ".spec.size"},
],
"additionalPrinterColumns": [
{"jsonPath": ".spec.color", "name": "Color", "type": "string"},
{"jsonPath": ".spec.size", "name": "Size", "type": "string"},
],
}
],
},
}
yield spec


def check_socket(host, port):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
return sock.connect_ex((host, port)) == 0
Expand Down Expand Up @@ -163,8 +207,9 @@ def serviceaccount(k8s_cluster, k8s_token):
host, port = _hostport.split(":")

# Create a temporary directory and populate it with the serviceaccount files
with tempfile.TemporaryDirectory() as tempdir, set_env(
KUBERNETES_SERVICE_HOST=host, KUBERNETES_SERVICE_PORT=port
with (
tempfile.TemporaryDirectory() as tempdir,
set_env(KUBERNETES_SERVICE_HOST=host, KUBERNETES_SERVICE_PORT=port),
):
tempdir = Path(tempdir)
# Create ca.crt in tempdir from the certificate-authority-data in kubeconfig
Expand Down
17 changes: 17 additions & 0 deletions kr8s/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
from kr8s.asyncio.objects import Pod, Table


@pytest.fixture
async def example_crd(example_crd_spec):
example = await kr8s.asyncio.objects.CustomResourceDefinition(example_crd_spec)
if not await example.exists():
await example.create()
assert example in [
crd async for crd in kr8s.asyncio.get("customresourcedefinitions")
]
yield example
await example.delete()


async def test_factory_bypass() -> None:
with pytest.raises(ValueError, match="kr8s.api()"):
_ = kr8s.Api()
Expand Down Expand Up @@ -160,6 +172,11 @@ async def test_get_pods(namespace) -> None:
assert isinstance(pods[0], Pod)


async def test_get_custom_resouces(example_crd) -> None:
async for shirt in kr8s.asyncio.get(example_crd.name):
assert shirt


async def test_get_pods_as_table() -> None:
api = await kr8s.asyncio.api()
async for pods in api.get("pods", namespace="kube-system", as_object=Table):
Expand Down

0 comments on commit 8b2466c

Please sign in to comment.