watcher: Clean up properly on exiting event loop
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Rob Watson 2022-06-14 09:56:59 +02:00
parent 969b0db9fc
commit a0949b2bde
2 changed files with 14 additions and 1 deletions

View File

@ -100,7 +100,7 @@ const tickerInterval = time.Millisecond * 250
// Close terminates the PodWatcher.
func (pw *PodWatcher) Close() {
pw.closeChan <- struct{}{}
close(pw.closeChan)
}
func (pw *PodWatcher) watchPods(ctx context.Context, wg *sync.WaitGroup) error {

View File

@ -54,6 +54,7 @@ type Watcher struct {
podSelector labels.Selector
podWatcher PodWatcherInterface
podWatcherFunc PodWatcherFunc
closeChan chan struct{}
errChan chan error
dst io.Writer
logger *log.Logger
@ -65,14 +66,24 @@ func NewWatcher(params WatcherParams, client KubernetesClient, podWatcherFunc Po
params: params,
client: client,
podWatcherFunc: podWatcherFunc,
closeChan: make(chan struct{}),
errChan: make(chan error),
dst: dst,
logger: logger,
}
}
// Close blocks until any actively monitored resource has been released, and
// exits.
func (w *Watcher) Close() {
close(w.closeChan)
}
// Watch watches a deployment.
func (w *Watcher) Watch(ctx context.Context) error {
// ensure any actively monitored resource is cleaned up on exit.
defer w.removeDeployment()
ns := w.params.Namespace
if ns == "" {
ns = corev1.NamespaceDefault
@ -127,6 +138,8 @@ func (w *Watcher) Watch(ctx context.Context) error {
// errChan is never closed.
case err := <-w.errChan:
return err
case <-w.closeChan:
return nil
case <-ctx.Done():
return ctx.Err()
}