@@ -22,7 +22,6 @@ import (
2222 "time"
2323
2424 cu "github.com/coderanger/controller-utils"
25- "github.com/go-logr/logr"
2625 "github.com/pkg/errors"
2726 appsv1 "k8s.io/api/apps/v1"
2827 batchv1 "k8s.io/api/batch/v1"
@@ -48,11 +47,6 @@ import (
4847
4948type migrationsComponent struct {}
5049
51- type migrationsComponentWatchMap struct {
52- client client.Client
53- log logr.Logger
54- }
55-
5650func Migrations () * migrationsComponent {
5751 return & migrationsComponent {}
5852}
@@ -65,33 +59,30 @@ func (comp *migrationsComponent) Setup(ctx *cu.Context, bldr *ctrl.Builder) erro
6559 bldr .Owns (& batchv1.Job {})
6660 bldr .Watches (
6761 & 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+ }),
6982 )
7083 return nil
7184}
7285
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-
9586func (comp * migrationsComponent ) Reconcile (ctx * cu.Context ) (cu.Result , error ) {
9687 obj := ctx .Object .(* migrationsv1beta1.Migrator )
9788
@@ -310,9 +301,9 @@ func (comp *migrationsComponent) Reconcile(ctx *cu.Context) (cu.Result, error) {
310301 return cu.Result {}, nil
311302}
312303
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 ) {
314305 namespace := obj .GetNamespace ()
315- owners := []cu .Object {}
306+ owners := []client .Object {}
316307 for {
317308 owners = append (owners , obj )
318309 ref := metav1 .GetControllerOfNoCopy (obj )
@@ -329,15 +320,15 @@ func (_ *migrationsComponent) findOwners(ctx *cu.Context, obj cu.Object) ([]cu.O
329320 }
330321 return nil , errors .Wrapf (err , "error finding object type for owner reference %v" , ref )
331322 }
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 )
333325 if err != nil {
334326 // Gracefully handle objects we don't have access to
335327 if kerrors .IsForbidden (err ) {
336328 break
337329 }
338330 return nil , errors .Wrapf (err , "error finding object type for owner reference %v" , ref )
339331 }
340- obj = ownerObj .(cu.Object )
341332 }
342333 // Reverse the slice so it goes top -> bottom.
343334 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
346337 return owners , nil
347338}
348339
349- func (_ * migrationsComponent ) findSpecFor (ctx * cu.Context , obj cu .Object ) * corev1.PodSpec {
340+ func (_ * migrationsComponent ) findSpecFor (ctx * cu.Context , obj client .Object ) * corev1.PodSpec {
350341 switch v := obj .(type ) {
351342 case * corev1.Pod :
352343 return & v .Spec
@@ -373,7 +364,7 @@ func (_ *migrationsComponent) findSpecFor(ctx *cu.Context, obj cu.Object) *corev
373364 }
374365}
375366
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 ) {
377368 owners , err := comp .findOwners (ctx , obj )
378369 if err != nil {
379370 return nil , err
0 commit comments