@@ -26,8 +26,10 @@ import (
2626 clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2727 "sigs.k8s.io/cluster-api/util/predicates"
2828 ctrl "sigs.k8s.io/controller-runtime"
29+ ctrlbldr "sigs.k8s.io/controller-runtime/pkg/builder"
2930 ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3031 "sigs.k8s.io/controller-runtime/pkg/controller"
32+ "sigs.k8s.io/controller-runtime/pkg/event"
3133 "sigs.k8s.io/controller-runtime/pkg/handler"
3234 "sigs.k8s.io/controller-runtime/pkg/manager"
3335 "sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -38,7 +40,6 @@ import (
3840// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters/status,verbs=get
3941// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachinegroups,verbs=get;list;watch;create;update;patch;delete
4042// +kubebuilder:rbac:groups=vmoperator.vmware.com,resources=virtualmachinegroups/status,verbs=get
41- // +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vsphereclusters,verbs=get;list;watch
4243// +kubebuilder:rbac:groups=vmware.infrastructure.cluster.x-k8s.io,resources=vspheremachines,verbs=get;list;watch
4344// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinedeployments,verbs=get;list;watch
4445// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch
@@ -53,7 +54,7 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
5354 Recorder : mgr .GetEventRecorderFor ("virtualmachinegroup-controller" ),
5455 }
5556
56- // Predicate: only allow VMG with the cluster-name label
57+ // Predicate: only allow VMG with the cluster-name label. Ensures the controller only works on VMG objects created by CAPV.
5758 hasClusterNameLabel := predicate .NewPredicateFuncs (func (obj ctrlclient.Object ) bool {
5859 labels := obj .GetLabels ()
5960 if labels == nil {
@@ -74,6 +75,13 @@ func AddVirtualMachineGroupControllerToManager(ctx context.Context, controllerMa
7475 Watches (
7576 & vmwarev1.VSphereMachine {},
7677 handler .EnqueueRequestsFromMapFunc (reconciler .VSphereMachineToVirtualMachineGroup ),
78+ ctrlbldr .WithPredicates (
79+ predicate.Funcs {
80+ UpdateFunc : func (e event.UpdateEvent ) bool { return false },
81+ CreateFunc : func (event.CreateEvent ) bool { return true },
82+ DeleteFunc : func (event.DeleteEvent ) bool { return true },
83+ GenericFunc : func (event.GenericEvent ) bool { return false },
84+ }),
7785 ).
7886 WithEventFilter (predicates .ResourceHasFilterLabel (mgr .GetScheme (), predicateLog , controllerManagerCtx .WatchFilterValue ))
7987
0 commit comments