@@ -45,8 +45,7 @@ import (
4545// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinedeployments,verbs=get;list;watch
4646// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch
4747
48- // AddVirtualMachineGroupControllerToManager adds the VirtualMachineGroup controller to the provided
49- // manager.
48+ // AddVirtualMachineGroupControllerToManager adds the VirtualMachineGroup controller to the provided manager.
5049func AddVirtualMachineGroupControllerToManager (ctx context.Context , controllerManagerCtx * capvcontext.ControllerManagerContext , mgr manager.Manager , options controller.Options ) error {
5150 predicateLog := ctrl .LoggerFrom (ctx ).WithValues ("controller" , "virtualmachinegroup" )
5251
@@ -55,27 +54,18 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
5554 Recorder : mgr .GetEventRecorderFor ("virtualmachinegroup-controller" ),
5655 }
5756
58- // Predicate: only allow VMG with the cluster-name label. Ensures the controller only works on VMG objects created by CAPV.
59- hasClusterNameLabel := predicate .NewPredicateFuncs (func (obj ctrlclient.Object ) bool {
60- labels := obj .GetLabels ()
61- if labels == nil {
62- return false
63- }
64- _ , ok := labels [clusterv1 .ClusterNameLabel ]
65- return ok
66- })
67-
6857 builder := ctrl .NewControllerManagedBy (mgr ).
69- For (& vmoprv1. VirtualMachineGroup {}).
58+ For (& clusterv1. Cluster {}).
7059 WithOptions (options ).
71- WithEventFilter (hasClusterNameLabel ).
60+ // Set the controller's name explicitly to virtualmachinegroup.
61+ Named ("virtualmachinegroup" ).
7262 Watches (
73- & clusterv1. Cluster {},
74- handler .EnqueueRequestsFromMapFunc ( reconciler .ClusterToVirtualMachineGroup ),
63+ & vmoprv1. VirtualMachineGroup {},
64+ handler .EnqueueRequestForOwner ( mgr . GetScheme (), reconciler .Client . RESTMapper (), & clusterv1. Cluster {} ),
7565 ).
7666 Watches (
7767 & vmwarev1.VSphereMachine {},
78- handler .EnqueueRequestsFromMapFunc (reconciler .VSphereMachineToVirtualMachineGroup ),
68+ handler .EnqueueRequestsFromMapFunc (reconciler .VSphereMachineToCluster ),
7969 ctrlbldr .WithPredicates (
8070 predicate.Funcs {
8171 UpdateFunc : func (event.UpdateEvent ) bool { return false },
@@ -89,26 +79,10 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
8979 return builder .Complete (reconciler )
9080}
9181
92- // ClusterToVirtualMachineGroup maps Cluster events to VirtualMachineGroup reconcile requests.
93- func (r * VirtualMachineGroupReconciler ) ClusterToVirtualMachineGroup (_ context.Context , a ctrlclient.Object ) []reconcile.Request {
94- cluster , ok := a .(* clusterv1.Cluster )
95- if ! ok {
96- return nil
97- }
98-
99- // Always enqueue a request for the "would-be VMG"
100- return []reconcile.Request {{
101- NamespacedName : apitypes.NamespacedName {
102- Namespace : cluster .Namespace ,
103- Name : cluster .Name ,
104- },
105- }}
106- }
107-
108- // VSphereMachineToVirtualMachineGroup maps VSphereMachine events to VirtualMachineGroup reconcile requests.
82+ // VSphereMachineToCluster maps VSphereMachine events to Cluster reconcile requests.
10983// This handler only processes VSphereMachine objects for Day-2 operations when VMG could be found, ensuring
11084// VMG member list in sync with VSphereMachines. If no corresponding VMG is found, this is a no-op.
111- func (r * VirtualMachineGroupReconciler ) VSphereMachineToVirtualMachineGroup (ctx context.Context , a ctrlclient.Object ) []reconcile.Request {
85+ func (r * VirtualMachineGroupReconciler ) VSphereMachineToCluster (ctx context.Context , a ctrlclient.Object ) []reconcile.Request {
11286 vSphereMachine , ok := a .(* vmwarev1.VSphereMachine )
11387 if ! ok {
11488 return nil
@@ -120,10 +94,7 @@ func (r *VirtualMachineGroupReconciler) VSphereMachineToVirtualMachineGroup(ctx
12094 }
12195
12296 vmg := & vmoprv1.VirtualMachineGroup {}
123- err := r .Client .Get (ctx , apitypes.NamespacedName {
124- Namespace : vSphereMachine .Namespace ,
125- Name : clusterName ,
126- }, vmg )
97+ err := r .Client .Get (ctx , apitypes.NamespacedName {Namespace : vSphereMachine .Namespace , Name : clusterName }, vmg )
12798
12899 if err != nil {
129100 return nil
0 commit comments