Skip to content

Commit

Permalink
Merge pull request #61 from kubescape/deep-equal-specs
Browse files Browse the repository at this point in the history
Fix watcher restart issue
  • Loading branch information
amitschendel authored Feb 5, 2024
2 parents ae7398e + dfdb7dc commit 4430294
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,27 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
}
w.watcher = watcher
w.running = true
currentWatcherContext, cancelFunc := context.WithCancel(context.Background())
go func() {
// Watch for events

for {
event, ok := <-watcher.ResultChan()
if !ok {
if currentWatcherContext != nil && cancelFunc != nil {
cancelFunc()
}

if w.running {
// Need to restart the watcher: wait a bit and restart
time.Sleep(5 * time.Second)
listOptions.ResourceVersion = resourceVersion
w.watcher, err = w.client.Resource(gvr).Namespace("").Watch(context.Background(), listOptions)
currentWatcherContext, cancelFunc = context.WithCancel(context.Background())
w.watcher, err = w.client.Resource(gvr).Namespace("").Watch(currentWatcherContext, listOptions)
if err != nil {
log.Printf("watcher restart error: %v", err)
}
watcher = w.watcher
// Restart the loop
continue
} else {
Expand Down

0 comments on commit 4430294

Please sign in to comment.