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

Handle 410 Gone response when watching resources #478

Merged
merged 5 commits into from
Aug 29, 2024
Merged
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
46 changes: 33 additions & 13 deletions kr8s/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import contextlib
import copy
import json
import logging
import ssl
import threading
import warnings
Expand All @@ -29,6 +30,7 @@
from ._objects import APIObject

ALL = "all"
logger = logging.getLogger(__name__)


class Api:
Expand Down Expand Up @@ -508,19 +510,37 @@ async def async_watch(
allow_unknown_type: bool = True,
) -> AsyncGenerator[tuple[str, APIObject]]:
"""Watch a Kubernetes resource."""
async with self.async_get_kind(
kind,
namespace=namespace,
label_selector=label_selector,
field_selector=field_selector,
params={"resourceVersion": since} if since else None,
watch=True,
timeout=None,
allow_unknown_type=allow_unknown_type,
) as (obj_cls, response):
async for line in response.aiter_lines():
event = json.loads(line)
yield event["type"], obj_cls(event["object"], api=self)
while True:
restart_watch = False
async with self.async_get_kind(
kind,
namespace=namespace,
label_selector=label_selector,
field_selector=field_selector,
params={"resourceVersion": since} if since else None,
watch=True,
timeout=None,
allow_unknown_type=allow_unknown_type,
) as (obj_cls, response):
logger.debug(
f"Starting watch of {kind}{' at resourceVersion ' + since if since else ''}"
)
async for line in response.aiter_lines():
event = json.loads(line)
if (
event["object"]["kind"] == "Status"
and event["object"].get("code") == 410
):
restart_watch = True
logger.debug(
f"Got 410 Gone: Restarting watch of {kind} at resourceVersion {since}"
)
break
obj = obj_cls(event["object"], api=self)
since = obj.metadata.resourceVersion
yield event["type"], obj
if not restart_watch:
return

async def api_resources(self) -> list[dict]:
"""Get the Kubernetes API resources."""
Expand Down
Loading