Skip to content

Commit

Permalink
k8s: handle resource version exception in stream
Browse files Browse the repository at this point in the history
Problem: as Blake Devcich noted in PR #247, the k8s watch stream
can raise an exception while iterating through it, rather than when
initializing the stream. Perhaps it is due to version differences in
the k8s python library.

Add exception handling for the iteration.
  • Loading branch information
jameshcorbett committed Jan 15, 2025
1 parent 1268cfa commit 77d516d
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/python/flux_k8s/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ def watch(self):
stream = k8s.watch.Watch().stream(
self.api.list_namespaced_custom_object, *self.crd, **kwargs
)
for event in stream:
if event["type"] == "ERROR" and event["object"]["code"] == 410:
LOGGER.debug(
"Resource version too old in watch, restarting "
"from resourceVersion = 0: %s",
event["object"]["message"],
)
self.resource_version = 0
return
self.resource_version = event["object"]["metadata"]["resourceVersion"]
self.cb(event, *self.cb_args, **self.cb_kwargs)
try:
for event in stream:
if event["type"] == "ERROR" and event["object"]["code"] == 410:
LOGGER.debug(
"Resource version too old in watch, restarting "
"from resourceVersion = 0: %s",
event["object"]["message"],
)
self.resource_version = 0
return self.watch()
self.resource_version = event["object"]["metadata"]["resourceVersion"]
self.cb(event, *self.cb_args, **self.cb_kwargs)
except ApiException as apiexc:
if apiexc.status != 410:
raise
self.resource_version = 0
self.watch()


def watch_cb(reactor, watcher, _r, watchers):
Expand Down

0 comments on commit 77d516d

Please sign in to comment.