@@ -22,6 +22,7 @@ import (
22
22
"time"
23
23
24
24
"github.com/go-logr/logr"
25
+ "github.com/pkg/errors"
25
26
26
27
"github.com/IBM-Cloud/power-go-client/power/models"
27
28
@@ -34,11 +35,14 @@ import (
34
35
ctrl "sigs.k8s.io/controller-runtime"
35
36
"sigs.k8s.io/controller-runtime/pkg/client"
36
37
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
38
+ "sigs.k8s.io/controller-runtime/pkg/handler"
39
+ "sigs.k8s.io/controller-runtime/pkg/source"
37
40
38
41
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
39
42
capierrors "sigs.k8s.io/cluster-api/errors"
40
43
"sigs.k8s.io/cluster-api/util"
41
44
"sigs.k8s.io/cluster-api/util/conditions"
45
+ "sigs.k8s.io/cluster-api/util/predicates"
42
46
43
47
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
44
48
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
@@ -156,13 +160,6 @@ func (r *IBMPowerVSMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
156
160
return r .reconcileNormal (machineScope )
157
161
}
158
162
159
- // SetupWithManager creates a new IBMPowerVSMachine controller for a manager.
160
- func (r * IBMPowerVSMachineReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
161
- return ctrl .NewControllerManagedBy (mgr ).
162
- For (& infrav1beta2.IBMPowerVSMachine {}).
163
- Complete (r )
164
- }
165
-
166
163
func (r * IBMPowerVSMachineReconciler ) reconcileDelete (scope * scope.PowerVSMachineScope ) (_ ctrl.Result , reterr error ) {
167
164
scope .Info ("Handling deleted IBMPowerVSMachine" )
168
165
@@ -311,3 +308,76 @@ func (r *IBMPowerVSMachineReconciler) reconcileNormal(machineScope *scope.PowerV
311
308
312
309
return ctrl.Result {}, nil
313
310
}
311
+
312
+ // IBMPowerVSClusterToIBMPowerVSMachines is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
313
+ // of IBMPowerVSMachines.
314
+ func (r * IBMPowerVSMachineReconciler ) IBMPowerVSClusterToIBMPowerVSMachines (ctx context.Context ) handler.MapFunc {
315
+ log := ctrl .LoggerFrom (ctx )
316
+ return func (mapCtx context.Context , o client.Object ) []ctrl.Request {
317
+ result := []ctrl.Request {}
318
+
319
+ c , ok := o .(* infrav1beta2.IBMPowerVSCluster )
320
+ if ! ok {
321
+ log .Error (errors .Errorf ("expected a IBMPowerVSCluster but got a %T" , o ), "failed to get IBMPowerVSMachines for IBMPowerVSCluster" )
322
+ return nil
323
+ }
324
+
325
+ cluster , err := util .GetOwnerCluster (mapCtx , r .Client , c .ObjectMeta )
326
+ switch {
327
+ case apierrors .IsNotFound (err ) || cluster == nil :
328
+ return result
329
+ case err != nil :
330
+ log .Error (err , "failed to get owning cluster" )
331
+ return result
332
+ }
333
+
334
+ labels := map [string ]string {capiv1beta1 .ClusterNameLabel : cluster .Name }
335
+ machineList := & capiv1beta1.MachineList {}
336
+ if err := r .List (mapCtx , machineList , client .InNamespace (c .Namespace ), client .MatchingLabels (labels )); err != nil {
337
+ log .Error (err , "failed to list Machines" )
338
+ return nil
339
+ }
340
+ for _ , m := range machineList .Items {
341
+ if m .Spec .InfrastructureRef .Name == "" {
342
+ continue
343
+ }
344
+ name := client.ObjectKey {Namespace : m .Namespace , Name : m .Spec .InfrastructureRef .Name }
345
+ result = append (result , ctrl.Request {NamespacedName : name })
346
+ }
347
+
348
+ return result
349
+ }
350
+ }
351
+
352
+ // SetupWithManager creates a new IBMVPCMachine controller for a manager.
353
+ func (r * IBMPowerVSMachineReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager ) error {
354
+ controller , err := ctrl .NewControllerManagedBy (mgr ).
355
+ For (& infrav1beta2.IBMPowerVSMachine {}).
356
+ WithEventFilter (predicates .ResourceNotPaused (ctrl .LoggerFrom (ctx ))).
357
+ Watches (
358
+ & capiv1beta1.Machine {},
359
+ handler .EnqueueRequestsFromMapFunc (util .MachineToInfrastructureMapFunc (infrav1beta2 .GroupVersion .WithKind ("IBMPowerVSMachine" ))),
360
+ ).
361
+ Watches (
362
+ & infrav1beta2.IBMPowerVSCluster {},
363
+ handler .EnqueueRequestsFromMapFunc (r .IBMPowerVSClusterToIBMPowerVSMachines (ctx )),
364
+ ).
365
+ Build (r )
366
+ if err != nil {
367
+ return errors .Wrap (err , "error creating controller" )
368
+ }
369
+
370
+ clusterToObjectFunc , err := util .ClusterToTypedObjectsMapper (r .Client , & infrav1beta2.IBMPowerVSMachineList {}, mgr .GetScheme ())
371
+ if err != nil {
372
+ return errors .Wrap (err , "failed to create mapper for Cluster to IBMPowerVSMachines" )
373
+ }
374
+ // Add a watch on capiv1beta1.Cluster object for unpause & ready notifications.
375
+ if err := controller .Watch (
376
+ source .Kind [client.Object ](mgr .GetCache (), & capiv1beta1.Cluster {},
377
+ handler .EnqueueRequestsFromMapFunc (clusterToObjectFunc ),
378
+ predicates .ClusterUnpausedAndInfrastructureReady (ctrl .LoggerFrom (ctx )),
379
+ )); err != nil {
380
+ return errors .Wrap (err , "failed adding a watch for ready clusters" )
381
+ }
382
+ return nil
383
+ }
0 commit comments