Refactor AppK8sClient.Watch to use controller-runtime informer cache
What
AppK8sClient.Watch in app/internal/k8s/app_client.go currently implements its own watch loop with manual reconnect, exponential backoff, and resourceVersion tracking (see drainWatcher / reconnect loop around lines 269-290, added in #7188).
We should replace this hand-rolled logic with the controller-runtime informer cache (sigs.k8s.io/controller-runtime/pkg/cache), which is already imported in this file.
Why
Informers (reflectors) already handle all of the concerns the current code is reinventing:
- Reconnect / backoff — the reflector does exponential backoff on watch errors automatically.
- List + Watch atomicity — the reflector performs the initial
Listand thenWatchusing the returnedresourceVersion, so no events are lost between the two calls. This also resolves #7250 (events being dropped betweenWatchsubscription and snapshot replay inInternalAppService.Watch). - Snapshot replay — listing from the informer's in-memory cache is synchronous and cheap, instead of hitting the API server on every client connect.
- 410 Gone / resourceVersion expiry — the reflector relists automatically.
- Shared across consumers — one informer per GVK+namespace scope, shared by many
Watchclients, instead of one API-server watch per client.
Maintaining our own reconnect/backoff/resourceVersion state machine is duplicate work and a correctness risk; the upstream implementation is battle-tested.
Proposed change
- Create (or reuse) a controller-runtime
cache.Cacheforservingv1.Servicescoped to the namespaces we care about. - Replace the raw
client.Watch+drainWatcher+ reconnect loop with:cache.Listfor the initial snapshot.- An
EventHandler(AddFunc/UpdateFunc/DeleteFunc) that fans out events to the per-client channel returned fromWatch.
- Remove
drainWatcher, thewatchState/ backoff helpers, and the manualresourceVersiontracking once the informer path is in place. - Update
InternalAppService.Watchto rely on the informer-backed snapshot (which eliminates the List/Watch ordering concern from #7250).
References
app/internal/k8s/app_client.go:269-290— current reconnect loopapp/internal/service/internal_app_service.go:197-252— consumer that would benefit- #7188 — PR that introduced the hand-rolled resilient watch
- #7250 — List/Watch race this refactor also fixes
My understanding is that the data sources suggest using the controller-runtime informer cache (sigs.k8s.io/controller-runtime/pkg/cache) is a best practice for Kubernetes controllers, as it handles reconnects, backoff, resourceVersion, and event delivery efficiently. Flyte's codebase already uses this pattern for K8s resource monitoring, leveraging the cache for listing and watching resources, and sharing informers across consumers, which aligns with your proposal to refactor away from manual watch loops and state machines. See pluginmachinery/k8s/client.go and core/kube_client.go for examples of constructing and using such a cache.
It seems likely that you can create or reuse a cache.Cache, use its List method for snapshots, and register event handlers for add/update/delete events, removing the need for custom reconnect/backoff/resourceVersion logic. This approach is already used in Flyte's plugin manager and resource monitoring code, as shown in plugin_manager.go and plugin_collector_test.go. Would you like a step-by-step breakdown or a code example for this refactor?
Sources:
This is an AI-generated response and your feedback is appreciated! Please leave a 👍 if this is helpful and 👎 if it is not.
Mention @runllm to ask follow-up questions. See runllm.com for more!
#take