Skip to content

Commit 8fb66d9

Browse files
author
Phillip Wittrock
authored
Merge pull request kubernetes-sigs#9 from pwittrock/master
Get controller.go test coverage to 100%
2 parents c55059a + 7d4e9b9 commit 8fb66d9

File tree

12 files changed

+486
-20
lines changed

12 files changed

+486
-20
lines changed

pkg/controller/controller.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ type controller struct {
102102
// mu is used to synchronize controller setup
103103
mu sync.Mutex
104104

105+
// jitterPeriod allows tests to reduce the jitterPeriod so they complete faster
106+
jitterPeriod time.Duration
107+
108+
// waitForCache allows tests to mock out the waitForCache function to return an error
109+
// defaults to cache.WaitForCacheSync
110+
waitForCache func(stopCh <-chan struct{}, cacheSyncs ...cache.InformerSynced) bool
111+
105112
// TODO(pwittrock): Consider initializing a logger with the controller name as the tag
106113
}
107114

@@ -145,21 +152,30 @@ func (c *controller) Start(stop <-chan struct{}) error {
145152
for _, informer := range allInformers {
146153
syncedFuncs = append(syncedFuncs, informer.HasSynced)
147154
}
148-
if ok := cache.WaitForCacheSync(stop, syncedFuncs...); !ok {
155+
156+
if c.waitForCache == nil {
157+
c.waitForCache = cache.WaitForCacheSync
158+
}
159+
if ok := c.waitForCache(stop, syncedFuncs...); !ok {
160+
// This code is unreachable right now since WaitForCacheSync will never return an error
161+
// Leaving it here because that could happen in the future
149162
err := fmt.Errorf("failed to wait for %s caches to sync", c.name)
150163
log.Error(err, "Could not wait for Cache to sync", "controller", c.name)
151164
return err
152165
}
153166

167+
if c.jitterPeriod == 0 {
168+
c.jitterPeriod = time.Second
169+
}
170+
154171
// Launch two workers to process resources
155172
log.Info("Starting workers", "controller", c.name, "WorkerCount", c.maxConcurrentReconciles)
156173
for i := 0; i < c.maxConcurrentReconciles; i++ {
157-
// Continually process work items
174+
// Process work items
158175
go wait.Until(func() {
159-
// TODO(pwittrock): Should we really use wait.Until to continuously restart this if it exits?
160176
for c.processNextWorkItem() {
161177
}
162-
}, time.Second, stop)
178+
}, c.jitterPeriod, stop)
163179
}
164180

165181
<-stop
@@ -174,13 +190,12 @@ func (c *controller) processNextWorkItem() bool {
174190

175191
obj, shutdown := c.queue.Get()
176192
if obj == nil {
177-
log.Error(nil, "Encountered nil Request", "Object", obj)
193+
// Sometimes the queue gives us nil items when it starts up
178194
c.queue.Forget(obj)
179195
}
180196

181197
if shutdown {
182-
// Return false, this will cause the controller to back off for a second before trying again.
183-
// TODO(community): This was copied from the sample-controller. Figure out why / if we need this.
198+
// Stop working
184199
return false
185200
}
186201

@@ -210,8 +225,6 @@ func (c *controller) processNextWorkItem() bool {
210225
c.queue.AddRateLimited(req)
211226
log.Error(nil, "reconcile error", "controller", c.name, "Request", req)
212227

213-
// Return false, this will cause the controller to back off for a second before trying again.
214-
// TODO(community): This was copied from the sample-controller. Figure out why / if we need this.
215228
return false
216229
} else if result.Requeue {
217230
c.queue.AddRateLimited(req)

pkg/controller/controller_suite_test.go renamed to pkg/controller/controller_suite_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
func TestSource(t *testing.T) {
3333
RegisterFailHandler(Fail)
34-
RunSpecsWithDefaultAndCustomReporters(t, "controller Suite", []Reporter{test.NewlineReporter{}})
34+
RunSpecsWithDefaultAndCustomReporters(t, "Controller Integration Suite", []Reporter{test.NewlineReporter{}})
3535
}
3636

3737
var testenv *test.Environment
@@ -40,7 +40,7 @@ var clientset *kubernetes.Clientset
4040
var icache informer.Informers
4141

4242
var _ = BeforeSuite(func() {
43-
logf.SetLogger(logf.ZapLogger(true))
43+
logf.SetLogger(logf.ZapLogger(false))
4444

4545
testenv = &test.Environment{}
4646

0 commit comments

Comments
 (0)