Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 9 additions & 9 deletions kubernetes_asyncio/watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ def unmarshal_event(self, data: str, response_type):
reason = "{}: {}".format(obj['reason'], obj['message'])
raise client.exceptions.ApiException(status=obj['code'], reason=reason)

# If possible, compile the JSON response into a Python native response
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...
if response_type:
js['object'] = self._api_client.deserialize(
response=SimpleNamespace(data=json.dumps(js['raw_object'])),
response_type=response_type
)

if js['type'].lower() != 'bookmark':
# If possible, compile the JSON response into a Python native response
# type, eg `V1Namespace` or `V1Pod`,`ExtensionsV1beta1Deployment`, ...
if response_type:
js['object'] = self._api_client.deserialize(
response=SimpleNamespace(data=json.dumps(js['raw_object'])),
response_type=response_type
)

# decode and save resource_version to continue watching
if hasattr(js['object'], 'metadata'):
self.resource_version = js['object'].metadata.resource_version
Expand All @@ -123,7 +123,7 @@ def unmarshal_event(self, data: str, response_type):
self.resource_version = js['object']['metadata']['resourceVersion']

elif js['type'].lower() == 'bookmark':
self.resource_version = js['object']['metadata']['resourceVersion']
self.resource_version = js['raw_object']['metadata']['resourceVersion']

Copy link

Copilot AI Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For bookmark events, ensure that js['raw_object'] always contains a 'metadata' key with 'resourceVersion' to avoid potential KeyErrors. Consider adding a conditional check if there's any chance that this field might be missing.

Suggested change
self.resource_version = js['raw_object']['metadata']['resourceVersion']
if ('metadata' in js['raw_object'] and
'resourceVersion' in js['raw_object']['metadata']):
self.resource_version = js['raw_object']['metadata']['resourceVersion']
else:
raise Exception(("Malformed JSON response for bookmark event, "
"'metadata' or 'resourceVersion' field is missing. "
"JSON: {}").format(js))

Copilot uses AI. Check for mistakes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add some checks for malformed events, similar are for standard events.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomplus Done! I've move or less used the change copilot suggested, plus a new test to check that malformed bookmarks now raise an exception. The logic has become a little duplicated, if you'd like I can extract it in to a new private function, but for now I've left the changes minimal.

return js

Expand Down
21 changes: 19 additions & 2 deletions kubernetes_asyncio/watch/watch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ async def test_unmarshal_bookmark_succeeds_and_preserves_resource_version(self):

# make sure the resource version is preserved,
# and the watcher's resource_version is updated
self.assertTrue(isinstance(event['object'], dict))
self.assertEqual("1", event['object']['metadata']['resourceVersion'])
self.assertTrue(isinstance(event['raw_object'], dict))
self.assertEqual("1", event['raw_object']['metadata']['resourceVersion'])
self.assertEqual("1", w.resource_version)

async def test_unmarshal_job_bookmark_succeeds_and_preserves_resource_version(self):
w = Watch()
event = w.unmarshal_event('{"type": "BOOKMARK", "object": {"apiVersion":'
'"batch/v1","kind":"Job","metadata":'
'{"name": "bar", "resourceVersion": "1"},'
'"spec": {"template": {"metadata": '
'{"creationTimestamp":null}, "spec": '
'{"containers":null}}}}}',
'object')
self.assertEqual("BOOKMARK", event['type'])

# make sure the resource version is preserved,
# and the watcher's resource_version is updated
self.assertTrue(isinstance(event['raw_object'], dict))
self.assertEqual("1", event['raw_object']['metadata']['resourceVersion'])
self.assertEqual("1", w.resource_version)