@@ -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/vpc-go-sdk/vpcv1"
27
28
@@ -33,9 +34,12 @@ import (
33
34
ctrl "sigs.k8s.io/controller-runtime"
34
35
"sigs.k8s.io/controller-runtime/pkg/client"
35
36
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
37
+ "sigs.k8s.io/controller-runtime/pkg/handler"
38
+ "sigs.k8s.io/controller-runtime/pkg/source"
36
39
37
40
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
38
41
"sigs.k8s.io/cluster-api/util"
42
+ "sigs.k8s.io/cluster-api/util/predicates"
39
43
40
44
infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
41
45
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
@@ -132,13 +136,6 @@ func (r *IBMVPCMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques
132
136
return r .reconcileNormal (machineScope )
133
137
}
134
138
135
- // SetupWithManager creates a new IBMVPCMachine controller for a manager.
136
- func (r * IBMVPCMachineReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
137
- return ctrl .NewControllerManagedBy (mgr ).
138
- For (& infrav1beta2.IBMVPCMachine {}).
139
- Complete (r )
140
- }
141
-
142
139
func (r * IBMVPCMachineReconciler ) reconcileNormal (machineScope * scope.MachineScope ) (ctrl.Result , error ) {
143
140
if controllerutil .AddFinalizer (machineScope .IBMVPCMachine , infrav1beta2 .MachineFinalizer ) {
144
141
return ctrl.Result {}, nil
@@ -221,3 +218,76 @@ func (r *IBMVPCMachineReconciler) reconcileDelete(scope *scope.MachineScope) (_
221
218
222
219
return ctrl.Result {}, nil
223
220
}
221
+
222
+ // IBMPowerVSClusterToIBMPowerVSMachines is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
223
+ // of IBMPowerVSMachines.
224
+ func (r * IBMVPCMachineReconciler ) IBMPowerVSClusterToIBMPowerVSMachines (ctx context.Context ) handler.MapFunc {
225
+ log := ctrl .LoggerFrom (ctx )
226
+ return func (mapCtx context.Context , o client.Object ) []ctrl.Request {
227
+ result := []ctrl.Request {}
228
+
229
+ c , ok := o .(* infrav1beta2.IBMPowerVSCluster )
230
+ if ! ok {
231
+ log .Error (errors .Errorf ("expected a IBMPowerVSCluster but got a %T" , o ), "failed to get IBMPowerVSMachines for IBMPowerVSCluster" )
232
+ return nil
233
+ }
234
+
235
+ cluster , err := util .GetOwnerCluster (mapCtx , r .Client , c .ObjectMeta )
236
+ switch {
237
+ case apierrors .IsNotFound (err ) || cluster == nil :
238
+ return result
239
+ case err != nil :
240
+ log .Error (err , "failed to get owning cluster" )
241
+ return result
242
+ }
243
+
244
+ labels := map [string ]string {capiv1beta1 .ClusterNameLabel : cluster .Name }
245
+ machineList := & capiv1beta1.MachineList {}
246
+ if err := r .List (mapCtx , machineList , client .InNamespace (c .Namespace ), client .MatchingLabels (labels )); err != nil {
247
+ log .Error (err , "failed to list Machines" )
248
+ return nil
249
+ }
250
+ for _ , m := range machineList .Items {
251
+ if m .Spec .InfrastructureRef .Name == "" {
252
+ continue
253
+ }
254
+ name := client.ObjectKey {Namespace : m .Namespace , Name : m .Spec .InfrastructureRef .Name }
255
+ result = append (result , ctrl.Request {NamespacedName : name })
256
+ }
257
+
258
+ return result
259
+ }
260
+ }
261
+
262
+ // SetupWithManager creates a new IBMVPCMachine controller for a manager.
263
+ func (r * IBMVPCMachineReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager ) error {
264
+ controller , err := ctrl .NewControllerManagedBy (mgr ).
265
+ For (& infrav1beta2.IBMVPCMachine {}).
266
+ WithEventFilter (predicates .ResourceNotPaused (ctrl .LoggerFrom (ctx ))).
267
+ Watches (
268
+ & capiv1beta1.Machine {},
269
+ handler .EnqueueRequestsFromMapFunc (util .MachineToInfrastructureMapFunc (infrav1beta2 .GroupVersion .WithKind ("IBMPowerVSMachine" ))),
270
+ ).
271
+ Watches (
272
+ & infrav1beta2.IBMPowerVSCluster {},
273
+ handler .EnqueueRequestsFromMapFunc (r .IBMPowerVSClusterToIBMPowerVSMachines (ctx )),
274
+ ).
275
+ Build (r )
276
+ if err != nil {
277
+ return errors .Wrap (err , "error creating controller" )
278
+ }
279
+
280
+ clusterToObjectFunc , err := util .ClusterToTypedObjectsMapper (r .Client , & infrav1beta2.IBMPowerVSMachineList {}, mgr .GetScheme ())
281
+ if err != nil {
282
+ return errors .Wrap (err , "failed to create mapper for Cluster to GCPMachines" )
283
+ }
284
+ // Add a watch on capiv1beta1.Cluster object for unpause & ready notifications.
285
+ if err := controller .Watch (
286
+ source .Kind (mgr .GetCache (), & capiv1beta1.Cluster {}),
287
+ handler .EnqueueRequestsFromMapFunc (clusterToObjectFunc ),
288
+ predicates .ClusterUnpausedAndInfrastructureReady (ctrl .LoggerFrom (ctx )),
289
+ ); err != nil {
290
+ return errors .Wrap (err , "failed adding a watch for ready clusters" )
291
+ }
292
+ return nil
293
+ }
0 commit comments