Skip to content

Commit 2454fdd

Browse files
author
Phillip Wittrock
authored
Merge pull request #8 from pwittrock/iterations
Update controller libraries
2 parents 4485f2f + dad9a7c commit 2454fdd

18 files changed

+1073
-465
lines changed

pkg/controller/common_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"testing"
2424
)
2525

26-
func Testcontroller(t *testing.T) {
26+
func TestController(t *testing.T) {
2727
RegisterFailHandler(Fail)
2828
RunSpecs(t, "controller Suite")
2929
}

pkg/controller/controller.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
"time"
2626

2727
"github.com/golang/glog"
28-
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/handlefunctions"
28+
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
2929
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/informers"
3030
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/metrics"
31+
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/predicates"
3132
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
3233
"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,7 +42,7 @@ import (
4142
var (
4243
// DefaultReconcileFn is used by GenericController if Reconcile is not set
4344
DefaultReconcileFn = func(k types.ReconcileKey) error {
44-
log.Printf("No ReconcileFn definded - skipping %+v", k)
45+
log.Printf("No ReconcileFn defined - skipping %+v", k)
4546
return nil
4647
}
4748

@@ -87,34 +88,38 @@ func (gc *GenericController) GetMetrics() metrics.Metrics {
8788
}
8889
}
8990

90-
// Watch watches objects matching obj's type and enqueues their keys.
91-
func (gc *GenericController) Watch(obj metav1.Object) error {
91+
// Watch watches objects matching obj's type and enqueues their keys to be reconcild.
92+
func (gc *GenericController) Watch(obj metav1.Object, p ...predicates.Predicate) error {
9293
gc.once.Do(gc.init)
93-
return gc.queue.watchFor(obj)
94+
return gc.queue.addEventHandler(obj,
95+
eventhandlers.MapAndEnqueue{Map: eventhandlers.MapToSelf, Predicates: p})
9496
}
9597

96-
// WatchAndMapToController watches objects matching obj's type and enqueues the keys of their controllers.
97-
func (gc *GenericController) WatchAndMapToController(obj metav1.Object, gvks ...metav1.GroupVersionKind) error {
98+
// WatchControllerOf watches for events for objects matching obj's type and enqueues events for the
99+
// controller of the object if the controller UID matches the ownerref UID.
100+
// Will walk the owners references looking up the controller using the path function and comparing the UID of
101+
// the object to the ownersref UID.
102+
// e.g. if obj was a Pod and the path contained lookup functions for ReplicaSet, Deployment, Foo it would walk
103+
// Pod -> (controller) ReplicaSet -> (controller) Deployment -> (controller) Foo and reconcile Foo only.
104+
func (gc *GenericController) WatchControllerOf(obj metav1.Object, path eventhandlers.Path,
105+
p ...predicates.Predicate) error {
98106
gc.once.Do(gc.init)
99-
return gc.queue.watchForAndMapToController(obj, gvks...)
107+
return gc.queue.addEventHandler(obj,
108+
eventhandlers.MapAndEnqueue{Map: eventhandlers.MapToController{Path: path}.Map, Predicates: p})
100109
}
101110

102-
func (gc *GenericController) WatchAndMapToControllerIf(obj metav1.Object,
103-
p handlefunctions.Predicate, gvks ...metav1.GroupVersionKind) error {
111+
// WatchTransformationOf watches objects matching obj's type and enqueues the key returned by mapFn.
112+
func (gc *GenericController) WatchTransformationOf(obj metav1.Object, mapFn eventhandlers.ObjToKey,
113+
p ...predicates.Predicate) error {
104114
gc.once.Do(gc.init)
105-
return gc.queue.watchForAndMapToControllerIf(obj, p, gvks...)
115+
return gc.queue.addEventHandler(obj,
116+
eventhandlers.MapAndEnqueue{Map: mapFn, Predicates: p})
106117
}
107118

108-
// WatchAndMap watches objects matching obj's type and enqueues the key returned by mapFn.
109-
func (gc *GenericController) WatchAndMap(obj metav1.Object, mapFn handlefunctions.ObjToKey) error {
119+
// WatchEvents watches objects matching obj's type and uses the functions from provider to handle events.
120+
func (gc *GenericController) WatchEvents(obj metav1.Object, provider types.HandleFnProvider) error {
110121
gc.once.Do(gc.init)
111-
return gc.queue.watchForAndMapToNewObjectKey(obj, mapFn)
112-
}
113-
114-
// WatchAndHandleEvents watches objects matching obj's type and uses the functions from provider to handle events.
115-
func (gc *GenericController) WatchAndHandleEvents(obj metav1.Object, provider types.HandleFnProvider) error {
116-
gc.once.Do(gc.init)
117-
return gc.queue.watchForAndHandleEvent(obj, fnToInterfaceAdapter{provider})
122+
return gc.queue.addEventHandler(obj, fnToInterfaceAdapter{provider})
118123
}
119124

120125
// WatchChannel enqueues object keys read from the channel.

0 commit comments

Comments
 (0)