Skip to content

Commit

Permalink
Update controller libraries
Browse files Browse the repository at this point in the history
- Lookup controller and compare uid when watchingcontrollerof
- Rename packages and functions
- Better test coverage
  • Loading branch information
pwittrock committed Mar 19, 2018
1 parent ca1b152 commit dad9a7c
Show file tree
Hide file tree
Showing 18 changed files with 1,073 additions and 465 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/common_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
)

func Testcontroller(t *testing.T) {
func TestController(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "controller Suite")
}
45 changes: 25 additions & 20 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
"time"

"github.com/golang/glog"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/handlefunctions"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/informers"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/metrics"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/predicates"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
"github.com/kubernetes-sigs/kubebuilder/pkg/inject/run"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -41,7 +42,7 @@ import (
var (
// DefaultReconcileFn is used by GenericController if Reconcile is not set
DefaultReconcileFn = func(k types.ReconcileKey) error {
log.Printf("No ReconcileFn definded - skipping %+v", k)
log.Printf("No ReconcileFn defined - skipping %+v", k)
return nil
}

Expand Down Expand Up @@ -87,34 +88,38 @@ func (gc *GenericController) GetMetrics() metrics.Metrics {
}
}

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

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

func (gc *GenericController) WatchAndMapToControllerIf(obj metav1.Object,
p handlefunctions.Predicate, gvks ...metav1.GroupVersionKind) error {
// WatchTransformationOf watches objects matching obj's type and enqueues the key returned by mapFn.
func (gc *GenericController) WatchTransformationOf(obj metav1.Object, mapFn eventhandlers.ObjToKey,
p ...predicates.Predicate) error {
gc.once.Do(gc.init)
return gc.queue.watchForAndMapToControllerIf(obj, p, gvks...)
return gc.queue.addEventHandler(obj,
eventhandlers.MapAndEnqueue{Map: mapFn, Predicates: p})
}

// WatchAndMap watches objects matching obj's type and enqueues the key returned by mapFn.
func (gc *GenericController) WatchAndMap(obj metav1.Object, mapFn handlefunctions.ObjToKey) error {
// WatchEvents watches objects matching obj's type and uses the functions from provider to handle events.
func (gc *GenericController) WatchEvents(obj metav1.Object, provider types.HandleFnProvider) error {
gc.once.Do(gc.init)
return gc.queue.watchForAndMapToNewObjectKey(obj, mapFn)
}

// WatchAndHandleEvents watches objects matching obj's type and uses the functions from provider to handle events.
func (gc *GenericController) WatchAndHandleEvents(obj metav1.Object, provider types.HandleFnProvider) error {
gc.once.Do(gc.init)
return gc.queue.watchForAndHandleEvent(obj, fnToInterfaceAdapter{provider})
return gc.queue.addEventHandler(obj, fnToInterfaceAdapter{provider})
}

// WatchChannel enqueues object keys read from the channel.
Expand Down
Loading

0 comments on commit dad9a7c

Please sign in to comment.