Skip to content

Commit 576c309

Browse files
committed
Simplifies stop chan management by ignoring leaderelection's stop chan.
1 parent 720d5f9 commit 576c309

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

pkg/manager/internal.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,14 @@ func (cm *controllerManager) GetRESTMapper() meta.RESTMapper {
161161
}
162162

163163
func (cm *controllerManager) Start(stop <-chan struct{}) error {
164-
if cm.resourceLock == nil {
165-
// join the passed-in stop channel as an upstream feeding into cm.stopper
166-
go func() {
167-
<-stop
168-
close(cm.stopper)
169-
}()
164+
defer close(cm.stopper)
170165

166+
if cm.resourceLock == nil {
171167
go cm.start()
172168
select {
173-
case <-cm.stop:
169+
// Only this function should receive from stop, and everything else
170+
// should receive from cm.stop.
171+
case <-stop:
174172
// we are done
175173
return nil
176174
case err := <-cm.errChan:
@@ -188,16 +186,10 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
188186
RetryPeriod: 2 * time.Second,
189187
Callbacks: leaderelection.LeaderCallbacks{
190188
// This type changes in k8s 1.12 to func(context.Context)
191-
OnStartedLeading: func(stopleading <-chan struct{}) {
192-
// join both stop and stopleading so they feed into cm.stopper
193-
go func() {
194-
select {
195-
case <-stop:
196-
close(cm.stopper)
197-
case <-stopleading:
198-
close(cm.stopper)
199-
}
200-
}()
189+
// Ignore the passed-in stop channel from leaderelection. The next
190+
// thing it does anyway after closing its stop channel is call
191+
// OnStoppedLeading.
192+
OnStartedLeading: func(_ <-chan struct{}) {
201193
cm.start()
202194
},
203195
OnStoppedLeading: func() {
@@ -215,7 +207,7 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
215207
go l.Run()
216208

217209
select {
218-
case <-cm.stop:
210+
case <-stop:
219211
// We are done
220212
return nil
221213
case err := <-cm.errChan:

0 commit comments

Comments
 (0)