Skip to content

Add has_synced() and wait_for_sync() to SharedInformer - #2817

Open
ilcm96 wants to merge 1 commit into
kubernetes-client:masterfrom
ilcm96:informer-has-synced
Open

ilcm96 wants to merge 1 commit into
kubernetes-client:masterfrom
ilcm96:informer-has-synced

Conversation

@ilcm96

@ilcm96 ilcm96 commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

SharedInformer.start() returns before the initial list is applied, and there is no way to tell when the cache is ready. A controller that starts its workers right away can read an empty cache and act as if objects do not exist; today callers set a flag in a handler or sleep.

This adds two methods:

  • has_synced() returns True once a full list is in the cache and its ADDED/MODIFIED/DELETED handlers have returned. It stays True through re-lists, watch failures and stop()/start() (the cache is kept across a restart).
  • wait_for_sync(timeout=None) blocks until synced (True), until the timeout passes, or until the informer is stopped (False). It waits without polling and must not be called from an event handler.
informer.start()
if not informer.wait_for_sync(timeout=30):
    raise RuntimeError("informer cache did not sync")
run_workers(informer.cache)

The e2e helper that polled the private _resource_version now uses wait_for_sync(), and the informer example waits for the initial list.

Which issue(s) this PR fixes:

None.

Special notes for your reviewer:

Tested with new unit tests (state before/after the initial list, a list that keeps failing, stop() releasing a wait, 410 with a failed re-list, restart) and on kind: with 50 ConfigMaps, wait_for_sync() returned True with all 50 in the cache and their ADDED handlers run; on a list that kept failing (404), wait_for_sync(timeout=3) returned False; stop() released a waiting call with False. Informer and watch e2e tests: 7 passed.

Does this PR introduce a user-facing change?

Add `SharedInformer.has_synced()` and `SharedInformer.wait_for_sync(timeout=None)` to tell when the initial list has been applied to the informer cache.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


SharedInformer gives no way to tell whether the initial list has been
applied to the cache. A controller that starts its workers right after
start() can therefore read an empty cache and conclude that objects do
not exist; callers have to set a flag in a handler or sleep.

Add two methods:

* has_synced() returns True once the objects of a full list are in the
  cache and the ADDED/MODIFIED/DELETED handlers for that list have
  returned. It stays True afterwards, through re-lists, watch failures
  and stop()/start(), since the cache is kept across a restart.
* wait_for_sync(timeout=None) blocks on a Condition until the informer
  is synced (True), the timeout passes (False) or the informer is
  stopped (False). On a stopped informer it returns has_synced() at
  once. It must not be called from an event handler, which runs on the
  informer thread.

The e2e helper that polled the private _resource_version now uses
wait_for_sync(), and the informer example waits for the initial list.

Unit tests cover the state before and after the initial list, a list
that keeps failing, stop() releasing a wait, and the state surviving a
410 with a failed re-list and a restart.
@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. labels Nov 28, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 28, 2026 07:01
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ilcm96
Once this PR has been reviewed and has the lgtm label, please assign yliaog for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant