Skip to content

Commit 99acd5c

Browse files
committed
Create stop channel on new manager
1 parent 5373e8e commit 99acd5c

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

pkg/manager/internal.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type controllerManager struct {
7373
mu sync.Mutex
7474
started bool
7575
errChan chan error
76-
stop <-chan struct{}
76+
stop chan struct{}
7777

7878
startCache func(stop <-chan struct{}) error
7979
}
@@ -162,7 +162,8 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
162162
go cm.start(stop)
163163
select {
164164
case <-stop:
165-
// we are done
165+
// We are done, close the internal stop channel
166+
close(cm.stop)
166167
return nil
167168
case err := <-cm.errChan:
168169
// Error starting a controller
@@ -195,7 +196,8 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
195196

196197
select {
197198
case <-stop:
198-
// We are done
199+
// We are done, close the internal stop channel
200+
close(cm.stop)
199201
return nil
200202
case err := <-cm.errChan:
201203
// Error starting a controller
@@ -208,8 +210,6 @@ func (cm *controllerManager) start(stop <-chan struct{}) {
208210
cm.mu.Lock()
209211
defer cm.mu.Unlock()
210212

211-
cm.stop = stop
212-
213213
// Start the Cache. Allow the function to start the cache to be mocked out for testing
214214
if cm.startCache == nil {
215215
cm.startCache = cm.cache.Start

pkg/manager/manager.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ func New(config *rest.Config, options Options) (Manager, error) {
180180
return nil, err
181181
}
182182

183+
stop := make(chan struct{})
184+
183185
return &controllerManager{
184186
config: config,
185187
scheme: options.Scheme,
@@ -191,6 +193,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
191193
recorderProvider: recorderProvider,
192194
resourceLock: resourceLock,
193195
mapper: mapper,
196+
stop: stop,
194197
}, nil
195198
}
196199

pkg/manager/manager_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,37 @@ var _ = Describe("manger.Manager", func() {
164164
close(done)
165165
})
166166

167+
It("should stop the internal when stop is called", func(done Done) {
168+
m, err := New(cfg, options)
169+
Expect(err).NotTo(HaveOccurred())
170+
s := make(chan struct{})
171+
close(s)
172+
Expect(m.Start(s)).NotTo(HaveOccurred())
173+
174+
mgr, ok := m.(*controllerManager)
175+
Expect(ok).To(BeTrue())
176+
Expect(mgr.stop).To(BeClosed())
177+
close(done)
178+
})
179+
180+
It("should not overwrite the internal stop channel", func(done Done) {
181+
m, err := New(cfg, options)
182+
Expect(err).NotTo(HaveOccurred())
183+
s := make(chan struct{})
184+
185+
go func() {
186+
defer GinkgoRecover()
187+
Expect(m.Start(s)).NotTo(HaveOccurred())
188+
close(done)
189+
}()
190+
191+
mgr, ok := m.(*controllerManager)
192+
Expect(ok).To(BeTrue())
193+
Expect(mgr.stop).ToNot(Equal(stop))
194+
195+
close(s)
196+
})
197+
167198
It("should return an error if it can't start the cache", func(done Done) {
168199
m, err := New(cfg, options)
169200
Expect(err).NotTo(HaveOccurred())
@@ -327,8 +358,7 @@ var _ = Describe("manger.Manager", func() {
327358
},
328359
stop: func(stop <-chan struct{}) error {
329360
defer GinkgoRecover()
330-
// Manager stop chan has not been initialized.
331-
Expect(stop).To(BeNil())
361+
Expect(stop).ToNot(BeNil())
332362
return nil
333363
},
334364
f: func(f inject.Func) error {

0 commit comments

Comments
 (0)