Skip to content

Commit d51fe83

Browse files
committed
re-add code coverage
1 parent 96119e3 commit d51fe83

File tree

6 files changed

+198
-90
lines changed

6 files changed

+198
-90
lines changed

pkg/controller/controller.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type Options struct {
4343
// Controller is a work queue that watches for changes to objects (i.e. Create / Update / Delete events) and
4444
// then reconciles an object (i.e. make changes to ensure the system state matches what is specified in the object).
4545
type Controller interface {
46+
// Reconcile is called to Reconcile an object by Namespace/Name
47+
reconcile.Reconcile
48+
4649
// Watch takes events provided by a Source and uses the EventHandler to enqueue reconcile.Requests in
4750
// response to the events.
4851
//
@@ -68,27 +71,19 @@ func New(name string, mrg manager.Manager, options Options) (Controller, error)
6871
options.MaxConcurrentReconciles = 1
6972
}
7073

71-
if options.Reconcile == nil {
72-
options.Reconcile = reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
73-
log.Error(nil, "Reconcile function not implemented", "Controller", name)
74-
fmt.Printf("Received Reconcile request on Controller %s for %s/%s", name, o.Namespace, o.Name)
75-
return reconcile.Result{}, nil
76-
})
77-
}
78-
7974
// Inject dependencies into Reconcile
8075
if err := mrg.SetFields(options.Reconcile); err != nil {
8176
return nil, err
8277
}
8378

8479
// Create controller with dependencies set
8580
c := &controller.Controller{
86-
Reconcile: options.Reconcile,
87-
Cache: mrg.GetCache(),
88-
Config: mrg.GetConfig(),
89-
Scheme: mrg.GetScheme(),
90-
Client: mrg.GetClient(),
91-
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
81+
Do: options.Reconcile,
82+
Cache: mrg.GetCache(),
83+
Config: mrg.GetConfig(),
84+
Scheme: mrg.GetScheme(),
85+
Client: mrg.GetClient(),
86+
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
9287
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
9388
Name: name,
9489
}

pkg/controller/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
. "github.com/onsi/gomega"
2929
)
3030

31-
var _ = Describe("controller", func() {
31+
var _ = Describe("controller.Controller", func() {
3232
var stop chan struct{}
3333

3434
rec := reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {

pkg/internal/controller/controller.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Controller struct {
5252
// Reconcile is a function that can be called at any time with the Name / Namespace of an object and
5353
// ensures that the state of the system matches the state specified in the object.
5454
// Defaults to the DefaultReconcileFunc.
55-
Reconcile reconcile.Reconcile
55+
Do reconcile.Reconcile
5656

5757
// Client is a lazily initialized Client. The controllerManager will initialize this when Start is called.
5858
Client client.Client
@@ -90,6 +90,10 @@ type Controller struct {
9090
// TODO(community): Consider initializing a logger with the Controller Name as the tag
9191
}
9292

93+
func (c *Controller) Reconcile(r reconcile.Request) (reconcile.Result, error) {
94+
return c.Do.Reconcile(r)
95+
}
96+
9397
// Watch implements controller.Controller
9498
func (c *Controller) Watch(src source.Source, evthdler handler.EventHandler, prct ...predicate.Predicate) error {
9599
c.mu.Lock()
@@ -203,7 +207,7 @@ func (c *Controller) processNextWorkItem() bool {
203207

204208
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
205209
// resource to be synced.
206-
if result, err := c.Reconcile.Reconcile(req); err != nil {
210+
if result, err := c.Do.Reconcile(req); err != nil {
207211
c.Queue.AddRateLimited(req)
208212
log.Error(nil, "Reconcile error", "Controller", c.Name, "Request", req)
209213

pkg/internal/controller/controller_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ var _ = Describe("controller", func() {
6161
informers = &informertest.FakeInformers{}
6262
ctrl = &Controller{
6363
MaxConcurrentReconciles: 1,
64-
Reconcile: fakeReconcile,
65-
Queue: queue,
66-
Cache: informers,
64+
Do: fakeReconcile,
65+
Queue: queue,
66+
Cache: informers,
6767
}
6868
ctrl.InjectFunc(func(interface{}) error { return nil })
6969
})
@@ -72,7 +72,19 @@ var _ = Describe("controller", func() {
7272
close(stop)
7373
})
7474

75-
Describe("Starting a Controller", func() {
75+
Describe("Reconcile", func() {
76+
It("should call the Reconcile function", func() {
77+
ctrl.Do = reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {
78+
return reconcile.Result{Requeue: true}, nil
79+
})
80+
result, err := ctrl.Reconcile(
81+
reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"}})
82+
Expect(err).NotTo(HaveOccurred())
83+
Expect(result).To(Equal(reconcile.Result{Requeue: true}))
84+
})
85+
})
86+
87+
Describe("Start", func() {
7688
It("should return an error if there is an error waiting for the informers", func(done Done) {
7789
ctrl.WaitForCache = func(<-chan struct{}, ...toolscache.InformerSynced) bool { return false }
7890
ctrl.Name = "foo"
@@ -105,7 +117,7 @@ var _ = Describe("controller", func() {
105117
})
106118
})
107119

108-
Describe("Calling Watch on a Controller", func() {
120+
Describe("Watch", func() {
109121
It("should inject dependencies into the Source", func() {
110122
src := &source.Kind{Type: &corev1.Pod{}}
111123
src.InjectCache(ctrl.Cache)
@@ -270,7 +282,7 @@ var _ = Describe("controller", func() {
270282
})
271283

272284
It("should continue to process additional queue items after the first", func(done Done) {
273-
ctrl.Reconcile = reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {
285+
ctrl.Do = reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {
274286
defer GinkgoRecover()
275287
Fail("Reconcile should not have been called")
276288
return reconcile.Result{}, nil

pkg/manager/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ type Runnable interface {
7878
Start(<-chan struct{}) error
7979
}
8080

81+
// RunnableFunc implements Runnable
82+
type RunnableFunc func(<-chan struct{}) error
83+
84+
// Start implements Runnable
85+
func (r RunnableFunc) Start(s <-chan struct{}) error {
86+
return r(s)
87+
}
88+
8189
// New returns a new Manager
8290
func New(config *rest.Config, options Options) (Manager, error) {
8391
cm := &controllerManager{config: config, scheme: options.Scheme, errChan: make(chan error)}

0 commit comments

Comments
 (0)