Skip to content

Commit 34362e0

Browse files
author
Phillip Wittrock
authored
Merge pull request #5 from pwittrock/master
Support Predicates
2 parents 17ac9e2 + 83570bb commit 34362e0

File tree

14 files changed

+98
-69
lines changed

14 files changed

+98
-69
lines changed

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type reconcileReplicaSet struct {
7878
client client.Interface
7979
}
8080

81-
// Implement reconcile.reconcile so the controller can reconcile objects
81+
// Implement reconcile.Reconcile so the controller can reconcile objects
8282
var _ reconcile.Reconcile = &reconcileReplicaSet{}
8383

8484
func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {

pkg/controller/controller.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Args struct {
5151
// Controller is a work queue that watches for changes to objects (i.e. Create / Update / Delete events) and
5252
// then reconciles an object (i.e. make changes to ensure the system state matches what is specified in the object).
5353
type Controller interface {
54-
// Watch takes events provided by a Source and uses the EventHandler to enqueue ReconcileRequests in
54+
// Watch takes events provided by a Source and uses the EventHandler to enqueue reconcile.Requests in
5555
// response to the events.
5656
//
5757
// Watch may be provided one or more Predicates to filter events before they are given to the EventHandler.
@@ -125,7 +125,7 @@ func (c *controller) Watch(src source.Source, evthdler eventhandler.EventHandler
125125
// TODO(pwittrock): wire in predicates
126126

127127
log.Info("Starting EventSource", "controller", c.name, "Source", src)
128-
return src.Start(evthdler, c.queue)
128+
return src.Start(evthdler, c.queue, prct...)
129129
}
130130

131131
func (c *controller) Start(stop <-chan struct{}) error {
@@ -179,7 +179,8 @@ func (c *controller) processNextWorkItem() bool {
179179
}
180180

181181
if shutdown {
182-
// Return false, take a break before starting again. But Y tho?
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.
183184
return false
184185
}
185186

@@ -209,13 +210,11 @@ func (c *controller) processNextWorkItem() bool {
209210
c.queue.AddRateLimited(req)
210211
log.Error(nil, "reconcile error", "controller", c.name, "Request", req)
211212

212-
// TODO(pwittrock): FTO Returning an error here seems to back things off for a second before restarting
213-
// the loop through wait.Util.
214-
// Return false, take a break. But y tho?
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.
215215
return false
216216
} else if result.Requeue {
217217
c.queue.AddRateLimited(req)
218-
// Return true, don't take a break
219218
return true
220219
}
221220

pkg/controller/doc.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ and are central to building Operators, Workload APIs, Configuration APIs, Autosc
2121
Controllers
2222
2323
Controllers are work queues that enqueue work in response to source.Source events (e.g. Pod Create, Update, Delete)
24-
and trigger reconcile.reconcile functions when the work is dequeued.
24+
and trigger reconcile.Reconcile functions when the work is dequeued.
2525
2626
Unlike http handlers, Controllers DO NOT perform work directly in response to events, but instead enqueue
27-
ReconcileRequests so the work is performed eventually.
27+
reconcile.Requests so the work is performed eventually.
2828
29-
* Controllers run reconcile.reconcile functions against objects (provided as name / Namespace).
29+
* Controllers run reconcile.Reconcile functions against objects (provided as name / Namespace).
3030
31-
* Controllers enqueue reconcile.ReconcileRequests in response events provided by source.Sources.
31+
* Controllers enqueue reconcile.Requests in response events provided by source.Sources.
3232
3333
reconcile
3434
35-
reconcile.reconcile is a function that may be called at anytime with the name / Namespace of an
35+
reconcile.Reconcile is a function that may be called at anytime with the name / Namespace of an
3636
object. When called, it will ensure that the state of the system matches what is specified in the object at the
3737
time reconcile is called.
3838
@@ -42,9 +42,9 @@ ReplicationController.
4242
4343
* reconcile works on a single object type. - e.g. it will only reconcile ReplicaSets.
4444
45-
* reconcile is triggered by a ReconcileRequest containing the name / Namespace of an object to reconcile.
45+
* reconcile is triggered by a reconcile.Request containing the name / Namespace of an object to reconcile.
4646
47-
* reconcile does not care about the event contents or event type triggering the ReconcileRequest.
47+
* reconcile does not care about the event contents or event type triggering the reconcile.Request.
4848
- e.g. it doesn't matter whether a ReplicaSet was created or updated, reconcile will check that the correct
4949
Pods exist either way.
5050
@@ -70,16 +70,16 @@ from another source (e.g. WebHook callback).
7070
7171
EventHandler
7272
73-
eventhandler.EventHandler transforms and enqueues events from a source.Source into reconcile.ReconcileRequests.
73+
eventhandler.EventHandler transforms and enqueues events from a source.Source into reconcile.Requests.
7474
7575
Example: a Pod Create event from a Source is provided to the eventhandler.EnqueueHandler, which enqueues a
76-
ReconcileRequest containing the name / Namespace of the Pod.
76+
reconcile.Request containing the name / Namespace of the Pod.
7777
78-
* EventHandler takes an event.Event and enqueues ReconcileRequests
78+
* EventHandler takes an event.Event and enqueues reconcile.Requests
7979
80-
* EventHandlers MAY map an event for an object of one type to a ReconcileRequest for an object of another type.
80+
* EventHandlers MAY map an event for an object of one type to a reconcile.Request for an object of another type.
8181
82-
* EventHandlers MAY map an event for an object to multiple ReconcileRequests for different objects.
82+
* EventHandlers MAY map an event for an object to multiple reconcile.Requests for different objects.
8383
8484
* Users SHOULD use the provided EventHandler implementations instead of implementing their own for almost all cases.
8585
@@ -100,13 +100,13 @@ Source provides event:
100100
101101
* &source.KindSource{"core", "v1", "Pod"} -> (Pod foo/bar Create Event)
102102
103-
EventHandler enqueues ReconcileRequest:
103+
EventHandler enqueues Request:
104104
105-
* &eventhandler.Enqueue{} -> (ReconcileRequest{"foo", "bar"})
105+
* &eventhandler.Enqueue{} -> (reconcile.Request{"foo", "bar"})
106106
107-
Reconcile is called with the ReconcileRequest:
107+
Reconcile is called with the Request:
108108
109-
* Reconcile(ReconcileRequest{"foo", "bar"})
109+
* Reconcile(Request{"foo", "bar"})
110110
111111
112112
controllerManager
@@ -162,22 +162,22 @@ to Pod or ReplicaSet events. The Reconcile function simply adds a label to the
162162
client client.Interface
163163
}
164164
165-
// Implement reconcile.reconcile so the controller can reconcile objects
165+
// Implement reconcile.Reconcile so the controller can reconcile objects
166166
var _ reconcile.Reconcile = &ReconcileReplicaSet{}
167167
168168
// Reconcile writes a "hello": "world" annotation to ReplicaSets that don't have one.
169-
func (r *ReconcileReplicaSet) Reconcile(request reconcile.ReconcileRequest) (reconcile.ReconcileResult, error) {
169+
func (r *ReconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
170170
// Fetch the ReplicaSet from the cache
171171
rs := &appsv1.ReplicaSet{}
172172
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
173173
if errors.IsNotFound(err) {
174174
log.Printf("Could not find ReplicaSet %v.\n", request)
175-
return reconcile.ReconcileResult{}, nil
175+
return reconcile.Result{}, nil
176176
}
177177
178178
if err != nil {
179179
log.Printf("Could not fetch ReplicaSet %v for %+v\n", err, request)
180-
return reconcile.ReconcileResult{}, err
180+
return reconcile.Result{}, err
181181
}
182182
183183
// Print the ReplicaSet
@@ -189,18 +189,18 @@ to Pod or ReplicaSet events. The Reconcile function simply adds a label to the
189189
rs.Labels = map[string]string{}
190190
}
191191
if rs.Labels["hello"] == "world" {
192-
return reconcile.ReconcileResult{}, nil
192+
return reconcile.Result{}, nil
193193
}
194194
195195
// Update the ReplicaSet
196196
rs.Labels["hello"] = "world"
197197
err = r.client.Update(context.TODO(), rs)
198198
if err != nil {
199199
log.Printf("Could not write ReplicaSet %v\n", err)
200-
return reconcile.ReconcileResult{}, err
200+
return reconcile.Result{}, err
201201
}
202202
203-
return reconcile.ReconcileResult{}, nil
203+
return reconcile.Result{}, nil
204204
}
205205
206206
Controller Example
@@ -233,13 +233,13 @@ For example, a Deployment controller might use an EnqueueHandler and EnqueueOwne
233233
234234
* Watch for ReplicaSet Events - enqueue the key of the Deployment that created the ReplicaSet (e.g the Owner)
235235
236-
Note: ReconcileRequests are deduplicated when they are enqueued. Many Pod Events for the same ReplicaSet
236+
Note: reconcile.Requests are deduplicated when they are enqueued. Many Pod Events for the same ReplicaSet
237237
may trigger only 1 reconcile invocation as each Event results in the Handler trying to enqueue
238-
the same ReconcileRequest for the ReplicaSet.
238+
the same reconcile.Request for the ReplicaSet.
239239
240-
controller Writing Tips
240+
Controller Writing Tips
241241
242-
reconcile Runtime Complexity:
242+
Reconcile Runtime Complexity:
243243
244244
* It is better to write Controllers to perform an O(1) reconcile N times (e.g. on N different objects) instead of
245245
performing an O(N) reconcile 1 time (e.g. on a single object which manages N other objects).
@@ -250,7 +250,7 @@ Services from a single reconcile.
250250
251251
Event Multiplexing:
252252
253-
* ReconcileRequests for the same name / Namespace are deduplicated when they are enqueued. This allows
253+
* reconcile.Requests for the same name / Namespace are deduplicated when they are enqueued. This allows
254254
for Controllers to gracefully handle event storms for a single object. Multiplexing multiple event Sources to
255255
a single object type takes advantage of this.
256256

pkg/controller/eventhandler/doc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ limitations under the License.
1515
*/
1616

1717
/*
18-
Package eventhandler defines EventHandlers that enqueue ReconcileRequests in response to Create, Update, Deletion Events
18+
Package eventhandler defines EventHandlers that enqueue reconcile.Requests in response to Create, Update, Deletion Events
1919
observed from Watching Kubernetes APIs.
2020
2121
EventHandlers
2222
23-
EnqueueHandler - Enqueues a ReconcileRequest containing the Name and Namespace of the object in the Event.
23+
EnqueueHandler - Enqueues a reconcile.equest containing the Name and Namespace of the object in the Event.
2424
25-
EnqueueOwnerHandler - Enqueues a ReconcileRequest containing the Name and Namespace of the Owner of the object in the Event.
25+
EnqueueOwnerHandler - Enqueues a reconcile.Request containing the Name and Namespace of the Owner of the object in the Event.
2626
27-
EnqueueMappedHander - Enqueues ReconcileRequests resulting from a user provided transformation function run against the
27+
EnqueueMappedHander - Enqueues Rreconcile.Requests resulting from a user provided transformation function run against the
2828
object in the Event.
2929
*/
3030
package eventhandler

pkg/controller/eventhandler/enqueue_mapped.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626

2727
var _ EventHandler = &EnqueueMappedHandler{}
2828

29-
// EnqueueMappedHandler enqueues ReconcileRequests by running a transformation function on each Event.
29+
// EnqueueMappedHandler enqueues Requests by running a transformation function on each Event.
3030
//
3131
// For UpdateEvents which contain both a new and old object, the transformation function is run on both
32-
// objects and both sets of ReconcileRequests are enqueue.
32+
// objects and both sets of Requests are enqueue.
3333
type EnqueueMappedHandler struct {
3434
// Mapper transforms the argument into a slice of keys to be reconciled
3535
ToRequests Mapper

pkg/controller/eventhandler/enqueue_owner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var _ EventHandler = &EnqueueOwnerHandler{}
3434

3535
var log = logf.KBLog.WithName("eventhandler").WithName("EnqueueOwnerHandler")
3636

37-
// EnqueueOwnerHandler enqueues ReconcileRequests for the Owners of an object. E.g. an object that created
37+
// EnqueueOwnerHandler enqueues Requests for the Owners of an object. E.g. an object that created
3838
// another object.
3939
//
4040
// If a ReplicaSet creates Pods, reconcile the ReplicaSet in response to events on Pods that it created using:

pkg/controller/eventhandler/eventhandler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"k8s.io/client-go/util/workqueue"
2222
)
2323

24-
// EventHandler enqueues ReconcileRequests in response to events (e.g. Pod Create). EventHandlers map an Event
24+
// EventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create). EventHandlers map an Event
2525
// for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
2626
// Event for object with type Foo (using source.KindSource) then reconcile one or more object(s) with type Bar.
2727
//
28-
// Identical ReconcileRequests will be batched together through the queuing mechanism before reconcile is called.
28+
// Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
2929
//
3030
// * Use EnqueueHandler to reconcile the object the event is for
3131
// - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
@@ -59,19 +59,19 @@ var _ EventHandler = Funcs{}
5959
// Funcs allows specifying a subset of EventHandler functions are fields.
6060
type Funcs struct {
6161
// Create is called in response to an add event. Defaults to no-op.
62-
// RateLimitingInterface is used to enqueue reconcile.ReconcileRequests.
62+
// RateLimitingInterface is used to enqueue reconcile.Requests.
6363
CreateFunc func(workqueue.RateLimitingInterface, event.CreateEvent)
6464

6565
// Update is called in response to an update event. Defaults to no-op.
66-
// RateLimitingInterface is used to enqueue reconcile.ReconcileRequests.
66+
// RateLimitingInterface is used to enqueue reconcile.Requests.
6767
UpdateFunc func(workqueue.RateLimitingInterface, event.UpdateEvent)
6868

6969
// Delete is called in response to a delete event. Defaults to no-op.
70-
// RateLimitingInterface is used to enqueue reconcile.ReconcileRequests.
70+
// RateLimitingInterface is used to enqueue reconcile.Requests.
7171
DeleteFunc func(workqueue.RateLimitingInterface, event.DeleteEvent)
7272

7373
// GenericFunc is called in response to a generic event. Defaults to no-op.
74-
// RateLimitingInterface is used to enqueue reconcile.ReconcileRequests.
74+
// RateLimitingInterface is used to enqueue reconcile.Requests.
7575
GenericFunc func(workqueue.RateLimitingInterface, event.GenericEvent)
7676
}
7777

pkg/controller/eventhandler/eventhandler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ var _ = Describe("Eventhandler", func() {
524524
})
525525

526526
Context("with the Controller field set to true", func() {
527-
It("should enqueue ReconcileRequests for only the first the Controller if there are "+
527+
It("should enqueue reconcile.Requests for only the first the Controller if there are "+
528528
"multiple Controller owners.", func() {
529529
instance := eventhandler.EnqueueOwnerHandler{
530530
OwnerType: &appsv1.ReplicaSet{},
@@ -571,7 +571,7 @@ var _ = Describe("Eventhandler", func() {
571571
NamespacedName: types.NamespacedName{Namespace: pod.GetNamespace(), Name: "foo2-parent"}}))
572572
})
573573

574-
It("should not enqueue ReconcileRequests if there are no Controller owners.", func() {
574+
It("should not enqueue reconcile.Requests if there are no Controller owners.", func() {
575575
instance := eventhandler.EnqueueOwnerHandler{
576576
OwnerType: &appsv1.ReplicaSet{},
577577
IsController: t,
@@ -602,7 +602,7 @@ var _ = Describe("Eventhandler", func() {
602602
Expect(q.Len()).To(Equal(0))
603603
})
604604

605-
It("should not enqueue ReconcileRequests if there are no owners.", func() {
605+
It("should not enqueue reconcile.Requests if there are no owners.", func() {
606606
instance := eventhandler.EnqueueOwnerHandler{
607607
OwnerType: &appsv1.ReplicaSet{},
608608
IsController: t,
@@ -618,7 +618,7 @@ var _ = Describe("Eventhandler", func() {
618618
})
619619

620620
Context("with the Controller field set to false", func() {
621-
It("should enqueue a ReconcileRequests for all owners.", func() {
621+
It("should enqueue a reconcile.Requests for all owners.", func() {
622622
instance := eventhandler.EnqueueOwnerHandler{
623623
OwnerType: &appsv1.ReplicaSet{},
624624
}

pkg/controller/eventhandler/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
var c controller.Controller
3232

33-
// This example watches Pods and enqueues ReconcileRequests with the Name and Namespace of the Pod from
33+
// This example watches Pods and enqueues Requests with the Name and Namespace of the Pod from
3434
// the Event (i.e. change caused by a Create, Update, Delete).
3535
func ExampleEnqueueHandler() {
3636
// controller is a controller.controller

pkg/controller/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ExampleController() {
4646
}
4747
}
4848

49-
// This example watches Pods and enqueues ReconcileRequests with the changed Pod Name and Namespace.
49+
// This example watches Pods and enqueues reconcile.Requests with the changed Pod Name and Namespace.
5050
func ExampleController_Watch() {
5151
cm, err := controller.NewManager(controller.ManagerArgs{Config: config.GetConfigOrDie()})
5252
if err != nil {

pkg/controller/reconcile/reconcile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"k8s.io/apimachinery/pkg/types"
2121
)
2222

23-
// Result contains the result of a reconcile.
23+
// Result contains the result of a Reconcile invocation.
2424
type Result struct {
2525
// Requeue tells the Controller to requeue the reconcile key. Defaults to false.
2626
Requeue bool

pkg/controller/source/example_test.go

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

2727
var ctrl controller.Controller
2828

29-
// This example Watches for Pod Events (e.g. Create / Update / Delete) and enqueues a ReconcileRequest
29+
// This example Watches for Pod Events (e.g. Create / Update / Delete) and enqueues a reconcile.Request
3030
// with the Name and Namespace of the Pod.
3131
func ExampleKindSource() {
3232
ctrl.Watch(
@@ -35,7 +35,7 @@ func ExampleKindSource() {
3535
)
3636
}
3737

38-
// This example reads GenericEvents from a channel and enqueues a ReconcileRequest containing the Name and Namespace
38+
// This example reads GenericEvents from a channel and enqueues a reconcile.Request containing the Name and Namespace
3939
// provided by the event.
4040
func ExampleChannelSource() {
4141
events := make(chan event.GenericEvent)

0 commit comments

Comments
 (0)