Skip to content

Commit 977e1fa

Browse files
committed
Stop channel commenting/renaming for clarity
This cleans up, adds comments, and renames things in the stop channel fix commits for clarity.
1 parent 576c309 commit 977e1fa

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

pkg/controller/controller.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func New(name string, mgr manager.Manager, options Options) (Controller, error)
7878

7979
// Create controller with dependencies set
8080
c := &controller.Controller{
81-
Do: options.Reconciler,
82-
Cache: mgr.GetCache(),
83-
Config: mgr.GetConfig(),
84-
Scheme: mgr.GetScheme(),
85-
Client: mgr.GetClient(),
86-
Recorder: mgr.GetRecorder(name),
87-
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
81+
Do: options.Reconciler,
82+
Cache: mgr.GetCache(),
83+
Config: mgr.GetConfig(),
84+
Scheme: mgr.GetScheme(),
85+
Client: mgr.GetClient(),
86+
Recorder: mgr.GetRecorder(name),
87+
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
8888
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
89-
Name: name,
89+
Name: name,
9090
}
9191

9292
// Add the controller as a Manager components

pkg/internal/controller/controller_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ var _ = Describe("controller", func() {
6060
informers = &informertest.FakeInformers{}
6161
ctrl = &Controller{
6262
MaxConcurrentReconciles: 1,
63-
Do: fakeReconcile,
64-
Queue: queue,
65-
Cache: informers,
63+
Do: fakeReconcile,
64+
Queue: queue,
65+
Cache: informers,
6666
}
6767
ctrl.InjectFunc(func(interface{}) error { return nil })
6868
})

pkg/manager/internal.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type controllerManager struct {
6464
// (and EventHandlers, Sources and Predicates).
6565
recorderProvider recorder.Provider
6666

67-
// resourceLock
67+
// resourceLock forms the basis for leader election
6868
resourceLock resourcelock.Interface
6969

7070
// mapper is used to map resources to kind, and map kind and version.
@@ -73,10 +73,16 @@ type controllerManager struct {
7373
mu sync.Mutex
7474
started bool
7575
errChan chan error
76-
stop <-chan struct{}
7776

78-
// stopper is the write side of the stop channel. They should have the same value.
79-
stopper chan<- struct{}
77+
// internalStop is the stop channel *actually* used by everything involved
78+
// with the manager as a stop channel, so that we can pass a stop channel
79+
// to things that need it off the bat (like the Channel source). It can
80+
// be closed via `internalStopper` (by being the same underlying channel).
81+
internalStop <-chan struct{}
82+
83+
// internalStopper is the write side of the internal stop channel, allowing us to close it.
84+
// It and `internalStop` should point to the same channel.
85+
internalStopper chan<- struct{}
8086

8187
startCache func(stop <-chan struct{}) error
8288
}
@@ -96,7 +102,7 @@ func (cm *controllerManager) Add(r Runnable) error {
96102
if cm.started {
97103
// If already started, start the controller
98104
go func() {
99-
cm.errChan <- r.Start(cm.stop)
105+
cm.errChan <- r.Start(cm.internalStop)
100106
}()
101107
}
102108

@@ -119,7 +125,7 @@ func (cm *controllerManager) SetFields(i interface{}) error {
119125
if _, err := inject.InjectorInto(cm.SetFields, i); err != nil {
120126
return err
121127
}
122-
if _, err := inject.StopChannelInto(cm.stop, i); err != nil {
128+
if _, err := inject.StopChannelInto(cm.internalStop, i); err != nil {
123129
return err
124130
}
125131
if _, err := inject.DecoderInto(cm.admissionDecoder, i); err != nil {
@@ -161,13 +167,15 @@ func (cm *controllerManager) GetRESTMapper() meta.RESTMapper {
161167
}
162168

163169
func (cm *controllerManager) Start(stop <-chan struct{}) error {
164-
defer close(cm.stopper)
170+
// stop everything we start when we exit this method for any reason
171+
defer close(cm.internalStopper)
165172

173+
// if we're not using resource election, start directly
166174
if cm.resourceLock == nil {
167-
go cm.start()
175+
cm.start()
168176
select {
169177
// Only this function should receive from stop, and everything else
170-
// should receive from cm.stop.
178+
// should receive from cm.internalStop.
171179
case <-stop:
172180
// we are done
173181
return nil
@@ -177,7 +185,8 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
177185
}
178186
}
179187

180-
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
188+
// otherwise, start when we acquire a resource election lock
189+
leaderElector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
181190
Lock: cm.resourceLock,
182191
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
183192
// TODO(joelspeed): These timings should be configurable
@@ -204,7 +213,7 @@ func (cm *controllerManager) Start(stop <-chan struct{}) error {
204213
return err
205214
}
206215

207-
go l.Run()
216+
go leaderElector.Run()
208217

209218
select {
210219
case <-stop:
@@ -225,22 +234,22 @@ func (cm *controllerManager) start() {
225234
cm.startCache = cm.cache.Start
226235
}
227236
go func() {
228-
if err := cm.startCache(cm.stop); err != nil {
237+
if err := cm.startCache(cm.internalStop); err != nil {
229238
cm.errChan <- err
230239
}
231240
}()
232241

233242
// Wait for the caches to sync.
234243
// TODO(community): Check the return value and write a test
235-
cm.cache.WaitForCacheSync(cm.stop)
244+
cm.cache.WaitForCacheSync(cm.internalStop)
236245

237246
// Start the runnables after the cache has synced
238247
for _, c := range cm.runnables {
239248
// Controllers block, but we want to return an error if any have an error starting.
240249
// Write any Start errors to a channel so we can return them
241250
ctrl := c
242251
go func() {
243-
cm.errChan <- ctrl.Start(cm.stop)
252+
cm.errChan <- ctrl.Start(cm.internalStop)
244253
}()
245254
}
246255

pkg/manager/manager.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ func New(config *rest.Config, options Options) (Manager, error) {
193193
recorderProvider: recorderProvider,
194194
resourceLock: resourceLock,
195195
mapper: mapper,
196-
stop: stop,
197-
stopper: stop,
196+
internalStop: stop,
197+
internalStopper: stop,
198198
}, nil
199199
}
200200

0 commit comments

Comments
 (0)