-
Notifications
You must be signed in to change notification settings - Fork 572
backend: k8scache: add stopwatcher to remove the context from watch r… #4523
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
base: main
Are you sure you want to change the base?
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ChayanDass 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 |
|
@illume, I need a suggestion. Currently, |
How frequently will this be called? Probably we only need it called if it's using a lot of data, or in hours or days. Is it being called on every request? Think about if it's going to use up a lot of resources just checking if stuff should be freed. (This sounds sufficient, but I haven't looked closely yet and it's been quite a while since I last looked at that code) |
There was a problem hiding this 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 PR wires Kubernetes kubeconfig context lifecycle events into the k8s cache watcher lifecycle so that when kube contexts are removed, their corresponding cache watchers are also stopped and cleaned up. It introduces a StopWatcher(contextKey) in the cache layer and a StopContextWatcher callback in the kubeconfig watcher to prevent stale watchers and goroutine leaks.
Changes:
- Added a
StopWatcher(contextKey string)function inbackend/pkg/k8cache/cacheInvalidation.goto cancel watcher contexts and remove their entries fromwatcherRegistryandcontextCancel. - Introduced a package-level
StopContextWatchercallback inbackend/pkg/kubeconfig/watcher.goand invoked it when kubeconfig contexts are removed insyncContexts. - Connected the two by assigning
kubeconfig.StopContextWatcher = k8cache.StopWatcherinCacheMiddleWare, so that kubeconfig watcher cleanup is triggered when contexts disappear.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
backend/pkg/kubeconfig/watcher.go |
Adds the StopContextWatcher global callback and invokes it before removing contexts from the kubeconfig store. |
backend/pkg/k8cache/cacheInvalidation.go |
Adds StopWatcher to stop watcher goroutines and clean up internal watcher tracking maps. |
backend/cmd/server.go |
Hooks kubeconfig.StopContextWatcher to k8cache.StopWatcher inside CacheMiddleWare to integrate kubeconfig context removal with cache watcher shutdown. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // StopWatcher stops and cleans up the watcher for a given contextKey. | ||
| func StopWatcher(contextKey string) { | ||
| if cancelAny, ok := contextCancel.Load(contextKey); ok { | ||
| cancelAny.(context.CancelFunc)() | ||
| contextCancel.Delete(contextKey) | ||
| watcherRegistry.Delete(contextKey) | ||
| } | ||
| } |
Copilot
AI
Feb 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new StopWatcher function lacks unit tests even though cacheInvalidation.go already has tests for related behavior (e.g. RunInformerToWatch in cacheInvalidation_test.go). Given that this function is responsible for cancelling contexts and cleaning up watcherRegistry/contextCancel, adding tests to verify that it correctly removes entries, is safe when called for unknown contextKeys, and is idempotent would help ensure the watcher lifecycle behavior stays correct.
6c071f4 to
09dade5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Description
This PR adds proper lifecycle cleanup for Kubernetes cache watchers when kube contexts are removed or reloaded. Previously, cache watchers could continue running even after their associated kube context was deleted, leading to stale watchers and potential goroutine leaks.
A new
StopWatchermechanism is introduced and wired into kubeconfig context synchronization to ensure watchers are cancelled and cleaned up correctly.What Changed
StopWatcher(contextKey)to cancel and clean up cache watchersRelated Issue
Fixes the review feedback on backend cache invalidation tests from PR
Fixes