Add SharedInformer implementation to python-client#2515
Add SharedInformer implementation to python-client#2515
Conversation
…example Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
|
@copilot can you add support for BOOKMARK events? |
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Done in 9c708ad. Added a |
|
@copilot can you implement it so that the most recent resourceVersion is stored and used for the re-list operation? |
… on 410 Gone Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Done in d21aa1b. |
|
@copilot can you add end-to-end tests against a real cluster |
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Done in 63bd3d5. Added
Each test uses a unique name (UUID suffix) and registers |
The PR description has been updated with a properly formatted standalone |
|
@copilot i don't think the release note has been updated properly, could you double check? |
examples/informer_example.py
Outdated
| @@ -0,0 +1,75 @@ | |||
| # Copyright 2024 The Kubernetes Authors. | |||
There was a problem hiding this comment.
s/2024/2026/, please update all files
There was a problem hiding this comment.
Fixed in 1d10150 — all 6 newly created files now have Copyright 2026.
kubernetes/informer/__init__.py
Outdated
| @@ -0,0 +1,27 @@ | |||
| # Copyright 2024 The Kubernetes Authors. | |||
There was a problem hiding this comment.
Nit: wrong copyright year? Does it matter?
| self._resource_version = self._watch.resource_version | ||
| self._watch = None | ||
|
|
||
| # Periodic resync: after the watch stream exits (whether due to the |
There was a problem hiding this comment.
This resync logic is now put after the
try:
for event in self._watch.stream(self._list_func, **kw):
block, which seems weird to me. Does that mean resync won't be triggered as long as the watch stream doesn't end? How does resync work in client-go? @brendandburns @copilot
There was a problem hiding this comment.
So the approach that is taken is that a server side timeout is added to the watch:
I don't think I have strong feelings one way or the other about interrupting on the server side or interrupting on the client side.
There was a problem hiding this comment.
@roycaihw the following lines set up watch stream timeout with resync_period, so watch stream will time out, should be fine.
if self._resync_period > 0:
kw["timeout_seconds"] = max(1, int(self._resync_period))
|
@copilot please address comments related to the copyright year, for newly created files, it should be updated to 2026 (the current year) instead of 2024. For existing files, the older copyright year should be retained. Do not change the copyright year for existing files, only new ones. Please also add an e2e test for informer resync if it does not already exist. |
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
The PR description has been updated in 1d10150 with a properly-formatted standalone
Done in 1d10150. Copyright year updated to 2026 in all 6 new files (existing files untouched). Added |
| elif evt_type == BOOKMARK: | ||
| # BOOKMARK events carry an updated resource version but | ||
| # no object state change; the Watch instance already | ||
| # records the new resource_version internally. |
There was a problem hiding this comment.
Watch added the support for BOOKMARK event handling a couple of months ago in #2505, wondering if copilot could add a test for BOOKMARK ?
|
/lgtm I'll leave it to @roycaihw to further review and approve |
|
@brendandburns what is your initial / first prompt to copilot that created this PR? |
|
@copilot please add unit tests for BOOKMARK events. |
…kmarks Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com> Agent-Logs-Url: https://github.com/kubernetes-client/python/sessions/d5256bd7-1cab-4399-97a0-9c115a2f97cb
|
New changes are detected. LGTM label has been removed. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Copilot The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Done in 6425bab. Added 3 new BOOKMARK unit tests targeting the behavior introduced in PR #2505:
Total BOOKMARK test count is now 5 (2 existing + 3 new); all 41 unit tests pass. |
| mock_w.resource_version = "100" | ||
|
|
||
| def fake_stream(func, **kw): | ||
| yield {"type": "BOOKMARK", "object": bookmark_obj, "raw_object": bookmark_obj} |
There was a problem hiding this comment.
this fake_stream is NOT really used, if we change bookmark_obj's resourceVersion to "200" (from the current "100"), the test still passes.
Python clients wanting a local cache of Kubernetes resources had to implement their own watch loops, reconnection logic, and thread management. This adds a
SharedInformeranalogous to the Java and JavaScript client implementations.New package:
kubernetes.informerObjectCache(cache.py) — thread-safe in-memory store keyed bynamespace/name; exposeslist(),get(),get_by_key(),list_keys()SharedInformer(informer.py) — daemon thread running list-then-watch loop with:ApiExceptionor other errorsADDED,MODIFIED,DELETED,BOOKMARK, andERRORresourceVersiontracking: reused on reconnect; reset on 410 Gone for a fresh re-listlist_funccall everyresync_periodseconds even when the cluster is quiet (viatimeout_secondson the watch); resync firesADDED/MODIFIED/DELETEDby diffing old vs new stateTesting
kubernetes/test/test_informer.py, covering cache behaviour, event handlers, watch loop, BOOKMARK handling,resourceVersiontracking, 410 Gone recovery, periodic resync, and scenarios analogous to the JavaScript, Java, and client-go reference tests. BOOKMARK-specific tests include:_resource_versionon the informer (exercising Watch's BOOKMARK-awareresource_versionextraction)_resource_versionto the latest onekubernetes/e2e_test/test_informer.py(auto-skipped when no cluster is available):start()ADDEDevent firing and cache population when a ConfigMap is createdMODIFIEDevent firing and updated data verified when a ConfigMap is patchedDELETEDevent firing and cache removal when a ConfigMap is deletedresourceVersionadvancing after real watch events are receivedMODIFIEDfor existing cached objects (test_resync_fires_modified_for_existing_objects)💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.