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

⚠️ Handler: Remove MapObject type and use client.Object directly #1207

Merged
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
19 changes: 7 additions & 12 deletions pkg/handler/enqueue_mapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// MapFunc is the signature required for enqueueing requests from a generic function.
// This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
type MapFunc func(MapObject) []reconcile.Request
type MapFunc func(client.Object) []reconcile.Request

// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
Expand All @@ -53,26 +53,26 @@ type enqueueRequestsFromMapFunc struct {

// Create implements EventHandler
func (e *enqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
e.mapAndEnqueue(q, evt.Object)
}

// Update implements EventHandler
func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, MapObject{Object: evt.ObjectOld})
e.mapAndEnqueue(q, MapObject{Object: evt.ObjectNew})
e.mapAndEnqueue(q, evt.ObjectOld)
e.mapAndEnqueue(q, evt.ObjectNew)
}

// Delete implements EventHandler
func (e *enqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
e.mapAndEnqueue(q, evt.Object)
}

// Generic implements EventHandler
func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
e.mapAndEnqueue(q, evt.Object)
}

func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object client.Object) {
for _, req := range e.toRequests(object) {
q.Add(req)
}
Expand All @@ -87,8 +87,3 @@ func (e *enqueueRequestsFromMapFunc) InjectFunc(f inject.Func) error {
}
return f(e.toRequests)
}

// MapObject contains information from an event to be transformed into a Request.
type MapObject struct {
Object client.Object
}
39 changes: 20 additions & 19 deletions pkg/handler/eventhandler_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package handler_test
Expand All @@ -27,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
"sigs.k8s.io/controller-runtime/pkg/event"
Expand Down Expand Up @@ -191,9 +192,9 @@ var _ = Describe("Eventhandler", func() {
Describe("EnqueueRequestsFromMapFunc", func() {
It("should enqueue a Request with the function applied to the CreateEvent.", func() {
req := []reconcile.Request{}
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
defer GinkgoRecover()
Expect(a.Object).To(Equal(pod))
Expect(a).To(Equal(pod))
req = []reconcile.Request{
{
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},
Expand Down Expand Up @@ -222,9 +223,9 @@ var _ = Describe("Eventhandler", func() {

It("should enqueue a Request with the function applied to the DeleteEvent.", func() {
req := []reconcile.Request{}
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
defer GinkgoRecover()
Expect(a.Object).To(Equal(pod))
Expect(a).To(Equal(pod))
req = []reconcile.Request{
{
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},
Expand Down Expand Up @@ -259,14 +260,14 @@ var _ = Describe("Eventhandler", func() {

req := []reconcile.Request{}

instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
defer GinkgoRecover()
req = []reconcile.Request{
{
NamespacedName: types.NamespacedName{Namespace: "foo", Name: a.Object.GetName() + "-bar"},
NamespacedName: types.NamespacedName{Namespace: "foo", Name: a.GetName() + "-bar"},
},
{
NamespacedName: types.NamespacedName{Namespace: "biz", Name: a.Object.GetName() + "-baz"},
NamespacedName: types.NamespacedName{Namespace: "biz", Name: a.GetName() + "-baz"},
},
}
return req
Expand Down Expand Up @@ -298,9 +299,9 @@ var _ = Describe("Eventhandler", func() {

It("should enqueue a Request with the function applied to the GenericEvent.", func() {
req := []reconcile.Request{}
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
defer GinkgoRecover()
Expect(a.Object).To(Equal(pod))
Expect(a).To(Equal(pod))
req = []reconcile.Request{
{
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},
Expand Down
11 changes: 6 additions & 5 deletions pkg/handler/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -65,15 +66,15 @@ func ExampleEnqueueRequestsFromMapFunc() {
// controller is a controller.controller
err := c.Watch(
&source.Kind{Type: &appsv1.Deployment{}},
handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example basically shows how this makes things easier

return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Object.GetName() + "-1",
Namespace: a.Object.GetNamespace(),
Name: a.GetName() + "-1",
Namespace: a.GetNamespace(),
}},
{NamespacedName: types.NamespacedName{
Name: a.Object.GetName() + "-2",
Namespace: a.Object.GetNamespace(),
Name: a.GetName() + "-2",
Namespace: a.GetNamespace(),
}},
}
}),
Expand Down