Skip to content

Fix parsing of bookmark objects #364

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ndhansen
Copy link
Contributor

@ndhansen ndhansen commented Jun 18, 2025

What is this MR?

This MR fixes the bug described in #363, and includes a test to show it's working. It's arguably a breaking change, because bookmark objects will no longer contain a parsed object fields, but I would argue that this is fine because the parsing didn't work anyway, so it's unlikely anyone was relying on this.

There are other ways of fixing this, such as handling bookmarks early and short-circuiting, but I decided to go for more minimal changes to the unmarshal logic.

How is this tested?

I added a test which includes a real payload from a bookmark event obtained when watching a kubernetes namespace, which previously would cause the client to crash.

I tested this manually by running the bookmark_bug.py script described in the issue, and got the expected output:

$ python streaming_test.py
ADDED
BOOKMARK
BOOKMARK
...

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes a bug in the parsing of bookmark events so that bookmark resource versions are correctly obtained from the original (raw) JSON object. Key changes include updates to the test assertions in watch_test.py and modifications in watch.py to extract resourceVersion from raw_object for bookmark events.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
kubernetes_asyncio/watch/watch_test.py Updated assertions to use raw_object for resourceVersion verification
kubernetes_asyncio/watch/watch.py Adjusted unmarshal_event to correctly access resourceVersion for bookmark events
Comments suppressed due to low confidence (1)

kubernetes_asyncio/watch/watch_test.py:392

  • [nitpick] The test now checks 'raw_object' instead of 'object'; ensure that additional test cases are in place to cover scenarios where the deserialized object may be used, ensuring full coverage of both bookmark and non-bookmark events.
        self.assertTrue(isinstance(event['raw_object'], dict))

Comment on lines +126 to 127
self.resource_version = js['raw_object']['metadata']['resourceVersion']

Copy link
Preview

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

codecov bot commented Jun 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 27.06%. Comparing base (abd4624) to head (e1e77a9).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #364   +/-   ##
=======================================
  Coverage   27.06%   27.06%           
=======================================
  Files         805      805           
  Lines       98720    98727    +7     
=======================================
+ Hits        26717    26724    +7     
  Misses      72003    72003           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +126 to 127
self.resource_version = js['raw_object']['metadata']['resourceVersion']

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants