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

Fix dynamic client watch of named resource #2076

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion kubernetes/base/dynamic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,13 @@ def watch(self, resource, namespace=None, name=None, label_selector=None, field_
"""
if not watcher: watcher = watch.Watch()

# Use field selector to query for named instance so the watch parameter is handled properly.
if name:
field_selector = f"metadata.name={name}"
Copy link
Member

Choose a reason for hiding this comment

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

If in addition to name, the user also specified field_selector when calling watch(), shall we also persist the user-specified value instead of overwriting it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about that, but according to this: https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/#chained-selectors multiple field selectors are and-ed together, and since name is unique including additional field selectors didn't seem to make any sense.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the super late response. That makes sense. Thanks!


for event in watcher.stream(
resource.get,
namespace=namespace,
name=name,
field_selector=field_selector,
label_selector=label_selector,
resource_version=resource_version,
Expand Down
5 changes: 5 additions & 0 deletions kubernetes/base/dynamic/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ def test_configmap_apis(self):
name=name, namespace='default', label_selector="e2e-test=true")
self.assertEqual(name, resp.metadata.name)

count = 0
for _ in client.watch(api, timeout=10, namespace="default", name=name):
count += 1
self.assertTrue(count > 0, msg="no events received for watch")

test_configmap['data']['config.json'] = "{}"
resp = api.patch(
name=name, namespace='default', body=test_configmap)
Expand Down