Skip to content

Commit 0ccb910

Browse files
committed
Protect observer.ctrls againts concurrent access
Can happen if Stop() is called during refresh().
1 parent 5b25228 commit 0ccb910

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

pkg/observer/observer.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package observer
77
import (
88
"fmt"
99
"strings"
10+
"sync"
1011
"time"
1112

1213
"github.com/bpineau/katafygio/config"
@@ -32,14 +33,15 @@ type controllerCollection map[string]controller.Interface
3233

3334
// Observer watch api-server and manage kubernetes controllers lifecyles
3435
type Observer struct {
35-
config *config.KfConfig
36-
stopCh chan struct{}
37-
doneCh chan struct{}
38-
notifier event.Notifier
39-
discovery discovery.DiscoveryInterface
40-
cpool dynamic.ClientPool
41-
ctrls controllerCollection
42-
factory ControllerFactory
36+
sync.RWMutex // protect ctrls
37+
config *config.KfConfig
38+
stopCh chan struct{}
39+
doneCh chan struct{}
40+
notifier event.Notifier
41+
discovery discovery.DiscoveryInterface
42+
cpool dynamic.ClientPool
43+
ctrls controllerCollection
44+
factory ControllerFactory
4345
}
4446

4547
type gvk struct {
@@ -94,16 +96,21 @@ func (c *Observer) Start() *Observer {
9496
func (c *Observer) Stop() {
9597
c.config.Logger.Info("Stopping all kubernetes controllers")
9698

97-
close(c.stopCh)
99+
c.stopCh <- struct{}{}
98100

101+
c.RLock()
99102
for _, ct := range c.ctrls {
100103
ct.Stop()
101104
}
105+
c.RUnlock()
102106

103107
<-c.doneCh
104108
}
105109

106110
func (c *Observer) refresh() error {
111+
c.Lock()
112+
defer c.Unlock()
113+
107114
groups, err := c.discovery.ServerResources()
108115
if err != nil {
109116
return fmt.Errorf("failed to collect server resources: %v", err)

0 commit comments

Comments
 (0)