Skip to content

Commit f35d811

Browse files
committed
⚠️ Handler: Remove MapObject type and use client.Object directly
We updated the handlers MapObject type to not include a metav1.Object and a runtime.Object representation of the same object but instead a client.Object, which is a union of the two above. The only reason for the MapObject type to exist in the first place though was to hold these two fields. Now that there is only the client.Object left, we can use that directly and get rid of the MapObject type.
1 parent 8ce28b5 commit f35d811

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

pkg/handler/enqueue_mapped.go

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

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

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

5454
// Create implements EventHandler
5555
func (e *enqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
56-
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
56+
e.mapAndEnqueue(q, evt.Object)
5757
}
5858

5959
// Update implements EventHandler
6060
func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
61-
e.mapAndEnqueue(q, MapObject{Object: evt.ObjectOld})
62-
e.mapAndEnqueue(q, MapObject{Object: evt.ObjectNew})
61+
e.mapAndEnqueue(q, evt.ObjectOld)
62+
e.mapAndEnqueue(q, evt.ObjectNew)
6363
}
6464

6565
// Delete implements EventHandler
6666
func (e *enqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
67-
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
67+
e.mapAndEnqueue(q, evt.Object)
6868
}
6969

7070
// Generic implements EventHandler
7171
func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
72-
e.mapAndEnqueue(q, MapObject{Object: evt.Object})
72+
e.mapAndEnqueue(q, evt.Object)
7373
}
7474

75-
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
75+
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object client.Object) {
7676
for _, req := range e.toRequests(object) {
7777
q.Add(req)
7878
}
@@ -87,8 +87,3 @@ func (e *enqueueRequestsFromMapFunc) InjectFunc(f inject.Func) error {
8787
}
8888
return f(e.toRequests)
8989
}
90-
91-
// MapObject contains information from an event to be transformed into a Request.
92-
type MapObject struct {
93-
Object client.Object
94-
}

pkg/handler/eventhandler_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/*
2-
Copyright 2018 The Kubernetes Authors.
2+
Copyright 2018 The Kubernetes Authors.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
1616

1717
package handler_test
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/types"
2828
"k8s.io/client-go/kubernetes/scheme"
2929
"k8s.io/client-go/util/workqueue"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
3031
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3132
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
3233
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -191,9 +192,9 @@ var _ = Describe("Eventhandler", func() {
191192
Describe("EnqueueRequestsFromMapFunc", func() {
192193
It("should enqueue a Request with the function applied to the CreateEvent.", func() {
193194
req := []reconcile.Request{}
194-
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
195+
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
195196
defer GinkgoRecover()
196-
Expect(a.Object).To(Equal(pod))
197+
Expect(a).To(Equal(pod))
197198
req = []reconcile.Request{
198199
{
199200
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},
@@ -222,9 +223,9 @@ var _ = Describe("Eventhandler", func() {
222223

223224
It("should enqueue a Request with the function applied to the DeleteEvent.", func() {
224225
req := []reconcile.Request{}
225-
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
226+
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
226227
defer GinkgoRecover()
227-
Expect(a.Object).To(Equal(pod))
228+
Expect(a).To(Equal(pod))
228229
req = []reconcile.Request{
229230
{
230231
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},
@@ -259,14 +260,14 @@ var _ = Describe("Eventhandler", func() {
259260

260261
req := []reconcile.Request{}
261262

262-
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
263+
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
263264
defer GinkgoRecover()
264265
req = []reconcile.Request{
265266
{
266-
NamespacedName: types.NamespacedName{Namespace: "foo", Name: a.Object.GetName() + "-bar"},
267+
NamespacedName: types.NamespacedName{Namespace: "foo", Name: a.GetName() + "-bar"},
267268
},
268269
{
269-
NamespacedName: types.NamespacedName{Namespace: "biz", Name: a.Object.GetName() + "-baz"},
270+
NamespacedName: types.NamespacedName{Namespace: "biz", Name: a.GetName() + "-baz"},
270271
},
271272
}
272273
return req
@@ -298,9 +299,9 @@ var _ = Describe("Eventhandler", func() {
298299

299300
It("should enqueue a Request with the function applied to the GenericEvent.", func() {
300301
req := []reconcile.Request{}
301-
instance := handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
302+
instance := handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
302303
defer GinkgoRecover()
303-
Expect(a.Object).To(Equal(pod))
304+
Expect(a).To(Equal(pod))
304305
req = []reconcile.Request{
305306
{
306307
NamespacedName: types.NamespacedName{Namespace: "foo", Name: "bar"},

pkg/handler/example_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
corev1 "k8s.io/api/core/v1"
2222
"k8s.io/apimachinery/pkg/types"
2323
"k8s.io/client-go/util/workqueue"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
2425
"sigs.k8s.io/controller-runtime/pkg/controller"
2526
"sigs.k8s.io/controller-runtime/pkg/event"
2627
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -65,15 +66,15 @@ func ExampleEnqueueRequestsFromMapFunc() {
6566
// controller is a controller.controller
6667
err := c.Watch(
6768
&source.Kind{Type: &appsv1.Deployment{}},
68-
handler.EnqueueRequestsFromMapFunc(func(a handler.MapObject) []reconcile.Request {
69+
handler.EnqueueRequestsFromMapFunc(func(a client.Object) []reconcile.Request {
6970
return []reconcile.Request{
7071
{NamespacedName: types.NamespacedName{
71-
Name: a.Object.GetName() + "-1",
72-
Namespace: a.Object.GetNamespace(),
72+
Name: a.GetName() + "-1",
73+
Namespace: a.GetNamespace(),
7374
}},
7475
{NamespacedName: types.NamespacedName{
75-
Name: a.Object.GetName() + "-2",
76-
Namespace: a.Object.GetNamespace(),
76+
Name: a.GetName() + "-2",
77+
Namespace: a.GetNamespace(),
7778
}},
7879
}
7980
}),

0 commit comments

Comments
 (0)