@@ -22,7 +22,6 @@ import (
22
22
"time"
23
23
24
24
cu "github.com/coderanger/controller-utils"
25
- "github.com/go-logr/logr"
26
25
"github.com/pkg/errors"
27
26
appsv1 "k8s.io/api/apps/v1"
28
27
batchv1 "k8s.io/api/batch/v1"
@@ -48,11 +47,6 @@ import (
48
47
49
48
type migrationsComponent struct {}
50
49
51
- type migrationsComponentWatchMap struct {
52
- client client.Client
53
- log logr.Logger
54
- }
55
-
56
50
func Migrations () * migrationsComponent {
57
51
return & migrationsComponent {}
58
52
}
@@ -65,33 +59,30 @@ func (comp *migrationsComponent) Setup(ctx *cu.Context, bldr *ctrl.Builder) erro
65
59
bldr .Owns (& batchv1.Job {})
66
60
bldr .Watches (
67
61
& source.Kind {Type : & corev1.Pod {}},
68
- & handler.EnqueueRequestsFromMapFunc {ToRequests : & migrationsComponentWatchMap {client : ctx .Client , log : ctx .Log }},
62
+ handler .EnqueueRequestsFromMapFunc (func (obj client.Object ) []reconcile.Request {
63
+ // Obj is a Pod that just got an event, map it back to any matching Migrators.
64
+ requests := []reconcile.Request {}
65
+ // Find any Migrator objects that match this pod.
66
+ migrators , err := utils .ListMatchingMigrators (context .Background (), ctx .Client , obj )
67
+ if err != nil {
68
+ ctx .Log .Error (err , "error listing matching migrators" )
69
+ // TODO Metric to track this for alerting.
70
+ return requests
71
+ }
72
+ for _ , migrator := range migrators {
73
+ requests = append (requests , reconcile.Request {
74
+ NamespacedName : types.NamespacedName {
75
+ Name : migrator .Name ,
76
+ Namespace : migrator .Namespace ,
77
+ },
78
+ })
79
+ }
80
+ return requests
81
+ }),
69
82
)
70
83
return nil
71
84
}
72
85
73
- // Watch map function used above.
74
- // Obj is a Pod that just got an event, map it back to any matching Migrators.
75
- func (m * migrationsComponentWatchMap ) Map (obj handler.MapObject ) []reconcile.Request {
76
- requests := []reconcile.Request {}
77
- // Find any Migrator objects that match this pod.
78
- migrators , err := utils .ListMatchingMigrators (context .Background (), m .client , obj .Meta )
79
- if err != nil {
80
- m .log .Error (err , "error listing matching migrators" )
81
- // TODO Metric to track this for alerting.
82
- return requests
83
- }
84
- for _ , migrator := range migrators {
85
- requests = append (requests , reconcile.Request {
86
- NamespacedName : types.NamespacedName {
87
- Name : migrator .Name ,
88
- Namespace : migrator .Namespace ,
89
- },
90
- })
91
- }
92
- return requests
93
- }
94
-
95
86
func (comp * migrationsComponent ) Reconcile (ctx * cu.Context ) (cu.Result , error ) {
96
87
obj := ctx .Object .(* migrationsv1beta1.Migrator )
97
88
@@ -310,9 +301,9 @@ func (comp *migrationsComponent) Reconcile(ctx *cu.Context) (cu.Result, error) {
310
301
return cu.Result {}, nil
311
302
}
312
303
313
- func (_ * migrationsComponent ) findOwners (ctx * cu.Context , obj cu .Object ) ([]cu .Object , error ) {
304
+ func (_ * migrationsComponent ) findOwners (ctx * cu.Context , obj client .Object ) ([]client .Object , error ) {
314
305
namespace := obj .GetNamespace ()
315
- owners := []cu .Object {}
306
+ owners := []client .Object {}
316
307
for {
317
308
owners = append (owners , obj )
318
309
ref := metav1 .GetControllerOfNoCopy (obj )
@@ -329,15 +320,15 @@ func (_ *migrationsComponent) findOwners(ctx *cu.Context, obj cu.Object) ([]cu.O
329
320
}
330
321
return nil , errors .Wrapf (err , "error finding object type for owner reference %v" , ref )
331
322
}
332
- err = ctx .Client .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : namespace }, ownerObj )
323
+ obj = ownerObj .(client.Object )
324
+ err = ctx .Client .Get (ctx , types.NamespacedName {Name : ref .Name , Namespace : namespace }, obj )
333
325
if err != nil {
334
326
// Gracefully handle objects we don't have access to
335
327
if kerrors .IsForbidden (err ) {
336
328
break
337
329
}
338
330
return nil , errors .Wrapf (err , "error finding object type for owner reference %v" , ref )
339
331
}
340
- obj = ownerObj .(cu.Object )
341
332
}
342
333
// Reverse the slice so it goes top -> bottom.
343
334
for i , j := 0 , len (owners )- 1 ; i < j ; i , j = i + 1 , j - 1 {
@@ -346,7 +337,7 @@ func (_ *migrationsComponent) findOwners(ctx *cu.Context, obj cu.Object) ([]cu.O
346
337
return owners , nil
347
338
}
348
339
349
- func (_ * migrationsComponent ) findSpecFor (ctx * cu.Context , obj cu .Object ) * corev1.PodSpec {
340
+ func (_ * migrationsComponent ) findSpecFor (ctx * cu.Context , obj client .Object ) * corev1.PodSpec {
350
341
switch v := obj .(type ) {
351
342
case * corev1.Pod :
352
343
return & v .Spec
@@ -373,7 +364,7 @@ func (_ *migrationsComponent) findSpecFor(ctx *cu.Context, obj cu.Object) *corev
373
364
}
374
365
}
375
366
376
- func (comp * migrationsComponent ) findOwnerSpec (ctx * cu.Context , obj cu .Object ) (* corev1.PodSpec , error ) {
367
+ func (comp * migrationsComponent ) findOwnerSpec (ctx * cu.Context , obj client .Object ) (* corev1.PodSpec , error ) {
377
368
owners , err := comp .findOwners (ctx , obj )
378
369
if err != nil {
379
370
return nil , err
0 commit comments