Skip to content

Commit 4358626

Browse files
author
Phillip Wittrock
authored
Merge pull request #18 from pwittrock/client
Add code coverage
2 parents 20db4b7 + 6e54443 commit 4358626

File tree

6 files changed

+199
-131
lines changed

6 files changed

+199
-131
lines changed

pkg/controller/controller.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ import (
2424
"github.com/kubernetes-sigs/controller-runtime/pkg/manager"
2525
"github.com/kubernetes-sigs/controller-runtime/pkg/predicate"
2626
"github.com/kubernetes-sigs/controller-runtime/pkg/reconcile"
27-
logf "github.com/kubernetes-sigs/controller-runtime/pkg/runtime/log"
2827
"github.com/kubernetes-sigs/controller-runtime/pkg/source"
2928
"k8s.io/client-go/util/workqueue"
3029
)
3130

32-
var log = logf.KBLog.WithName("controller")
33-
3431
// Options are the arguments for creating a new Controller
3532
type Options struct {
3633
// MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1.
@@ -43,6 +40,9 @@ type Options struct {
4340
// Controller is a work queue that watches for changes to objects (i.e. Create / Update / Delete events) and
4441
// then reconciles an object (i.e. make changes to ensure the system state matches what is specified in the object).
4542
type Controller interface {
43+
// Reconcile is called to Reconcile an object by Namespace/Name
44+
reconcile.Reconcile
45+
4646
// Watch takes events provided by a Source and uses the EventHandler to enqueue reconcile.Requests in
4747
// response to the events.
4848
//
@@ -68,27 +68,19 @@ func New(name string, mrg manager.Manager, options Options) (Controller, error)
6868
options.MaxConcurrentReconciles = 1
6969
}
7070

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-
7971
// Inject dependencies into Reconcile
8072
if err := mrg.SetFields(options.Reconcile); err != nil {
8173
return nil, err
8274
}
8375

8476
// Create controller with dependencies set
8577
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),
78+
Do: options.Reconcile,
79+
Cache: mrg.GetCache(),
80+
Config: mrg.GetConfig(),
81+
Scheme: mrg.GetScheme(),
82+
Client: mrg.GetClient(),
83+
Queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
9284
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
9385
Name: name,
9486
}

pkg/controller/controller_test.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@ package controller_test
1919
import (
2020
"fmt"
2121

22-
"github.com/kubernetes-sigs/controller-runtime/pkg/cache"
2322
"github.com/kubernetes-sigs/controller-runtime/pkg/client"
2423
"github.com/kubernetes-sigs/controller-runtime/pkg/controller"
2524
"github.com/kubernetes-sigs/controller-runtime/pkg/manager"
2625
"github.com/kubernetes-sigs/controller-runtime/pkg/reconcile"
2726
"github.com/kubernetes-sigs/controller-runtime/pkg/runtime/inject"
2827
. "github.com/onsi/ginkgo"
2928
. "github.com/onsi/gomega"
30-
"k8s.io/apimachinery/pkg/runtime"
31-
"k8s.io/client-go/rest"
3229
)
3330

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

3734
rec := reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {
@@ -93,38 +90,3 @@ func (*failRec) Reconcile(reconcile.Request) (reconcile.Result, error) {
9390
func (*failRec) InjectClient(client.Client) error {
9491
return fmt.Errorf("expected error")
9592
}
96-
97-
type injectable struct {
98-
scheme func(scheme *runtime.Scheme) error
99-
client func(client.Client) error
100-
config func(config *rest.Config) error
101-
cache func(cache.Cache) error
102-
}
103-
104-
func (i *injectable) InjectCache(c cache.Cache) error {
105-
if i.cache == nil {
106-
return nil
107-
}
108-
return i.cache(c)
109-
}
110-
111-
func (i *injectable) InjectConfig(config *rest.Config) error {
112-
if i.config == nil {
113-
return nil
114-
}
115-
return i.config(config)
116-
}
117-
118-
func (i *injectable) InjectClient(c client.Client) error {
119-
if i.client == nil {
120-
return nil
121-
}
122-
return i.client(c)
123-
}
124-
125-
func (i *injectable) InjectScheme(scheme *runtime.Scheme) error {
126-
if i.scheme == nil {
127-
return nil
128-
}
129-
return i.scheme(scheme)
130-
}

pkg/internal/controller/controller.go

Lines changed: 7 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,11 @@ type Controller struct {
9090
// TODO(community): Consider initializing a logger with the Controller Name as the tag
9191
}
9292

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

204209
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
205210
// resource to be synced.
206-
if result, err := c.Reconcile.Reconcile(req); err != nil {
211+
if result, err := c.Do.Reconcile(req); err != nil {
207212
c.Queue.AddRateLimited(req)
208213
log.Error(nil, "Reconcile error", "Controller", c.Name, "Request", req)
209214

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)