Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change watching events to use interface instead of struct #43

Merged
merged 2 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ func (gc *GenericController) WatchChannel(source <-chan string) error {

// fnToInterfaceAdapter adapts a function to an interface
type fnToInterfaceAdapter struct {
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
val func(workqueue.RateLimitingInterface) cache.ResourceEventHandler
}

func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func (f fnToInterfaceAdapter) Get(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return f.val(q)
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"time"

"github.com/kubernetes-sigs/kubebuilder/pkg/controller/eventhandlers"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/test"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
Expand All @@ -29,7 +31,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"time"
)

var _ = Describe("GenericController", func() {
Expand Down Expand Up @@ -236,7 +237,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling add function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { w.AddRateLimited("key/value") },
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
Expand All @@ -257,7 +258,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling update function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { Fail("Add function called") },
DeleteFunc: func(obj interface{}) { Fail("Delete function called") },
Expand Down Expand Up @@ -288,7 +289,7 @@ var _ = Describe("GenericController", func() {
It("should call the event handling delete function", func() {
// Listen for Pod changes
Expect(instance.WatchEvents(&corev1.Pod{},
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(w workqueue.RateLimitingInterface) cache.ResourceEventHandler {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { Fail("Add function called") },
DeleteFunc: func(obj interface{}) { w.AddRateLimited("key/value") },
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/eventhandlers/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"k8s.io/client-go/util/workqueue"
)

// EventHandler accepts a workqueue and returns ResourceEventHandlerFuncs that enqueue messages to it
// EventHandler accepts a workqueue and returns ResourceEventHandler that enqueue messages to it
// for add / update / delete events
type EventHandler interface {
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler
}

// MapAndEnqueue provides Fns to map objects to name/namespace keys and enqueue them as messages
Expand All @@ -41,8 +41,8 @@ type MapAndEnqueue struct {
Map func(interface{}) string
}

// Get returns ResourceEventHandlerFuncs that Map an object to a Key and enqueue the key if it is non-empty
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
// Get returns ResourceEventHandler that Map an object to a Key and enqueue the key if it is non-empty
func (mp MapAndEnqueue) Get(r workqueue.RateLimitingInterface) cache.ResourceEventHandler {
// Enqueue the mapped key for updates to the object
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
Expand Down
36 changes: 18 additions & 18 deletions pkg/controller/eventhandlers/eventhandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ var _ = Describe("Eventhandlers", func() {
Context("Where there are no Predicates", func() {
It("should set the Add function", func() {
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Eventually(q.Len).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-add"))
})

It("should set the Delete function", func() {
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-delete"))
})

It("should set the Update function", func() {
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-update"))
})
Expand All @@ -76,36 +76,36 @@ var _ = Describe("Eventhandlers", func() {
It("should set the Add function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-add"))

fns.DeleteFunc("delete")
fns.UpdateFunc("old", "update")
fns.OnDelete("delete")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})

It("should set the Delete function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{delete: true}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-delete"))

fns.AddFunc("add")
fns.UpdateFunc("old", "add")
fns.OnAdd("add")
fns.OnUpdate("old", "add")
Consistently(q.Len).Should(Equal(0))
})

It("should set the Update function", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{update: true}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Eventually(q.Len()).Should(Equal(1))
Expect(q.Get()).Should(Equal("p-update"))

fns.AddFunc("add")
fns.DeleteFunc("delete")
fns.OnAdd("add")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})
})
Expand All @@ -115,42 +115,42 @@ var _ = Describe("Eventhandlers", func() {
It("should not Add", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{create: true}, FakePredicates{}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Consistently(q.Len).Should(Equal(0))
})

It("should not Delete", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{delete: true}, FakePredicates{}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})

It("should not Update", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{update: true}, FakePredicates{}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})

It("should not Add", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{create: true}}
fns := mae.Get(q)
fns.AddFunc("add")
fns.OnAdd("add")
Consistently(q.Len).Should(Equal(0))
})

It("should not Delete", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{delete: true}}
fns := mae.Get(q)
fns.DeleteFunc("delete")
fns.OnDelete("delete")
Consistently(q.Len).Should(Equal(0))
})

It("should not Update", func() {
mae.Predicates = []predicates.Predicate{FakePredicates{}, FakePredicates{update: true}}
fns := mae.Get(q)
fns.UpdateFunc("old", "update")
fns.OnUpdate("old", "update")
Consistently(q.Len).Should(Equal(0))
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/example_watchandhandleevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ExampleGenericController_WatchEvents() {
}
err := c.WatchEvents(&corev1.Pod{},
// This function returns the callbacks that will be invoked for events
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
func(q workqueue.RateLimitingInterface) cache.ResourceEventHandler {
// This function implements the same functionality as GenericController.Watch
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { q.AddRateLimited(eventhandlers.MapToSelf(obj)) },
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
// ReconcileFn takes the key of an object and reconciles its desired and observed state.
type ReconcileFn func(ReconcileKey) error

// HandleFnProvider returns cache.ResourceEventHandlerFuncs that may enqueue messages
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs
// HandleFnProvider returns cache.ResourceEventHandler that may enqueue messages
type HandleFnProvider func(workqueue.RateLimitingInterface) cache.ResourceEventHandler

// ReconcileKey provides a lookup key for a Kubernetes object.
type ReconcileKey struct {
Expand Down