Skip to content

Commit 36c3550

Browse files
committed
Add VMG reconciler unit test and bump VMOP
- Add VMG recociler unit test - Bump VMOP due to API change - Filter out VSphereMachine event except create/delete events Signed-off-by: Gong Zhang <[email protected]>
1 parent 833cdb5 commit 36c3550

File tree

7 files changed

+268
-387
lines changed

7 files changed

+268
-387
lines changed

controllers/vmware/controllers_suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
. "github.com/onsi/ginkgo/v2"
2727
"github.com/onsi/ginkgo/v2/types"
2828
. "github.com/onsi/gomega"
29+
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
2930
corev1 "k8s.io/api/core/v1"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -71,6 +72,7 @@ func setup(ctx context.Context) (*helpers.TestEnvironment, clustercache.ClusterC
7172
utilruntime.Must(infrav1.AddToScheme(scheme.Scheme))
7273
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
7374
utilruntime.Must(vmwarev1.AddToScheme(scheme.Scheme))
75+
utilruntime.Must(vmoprv1.AddToScheme(scheme.Scheme))
7476

7577
testEnv := helpers.NewTestEnvironment(ctx)
7678

controllers/vmware/virtualmachinegroup_controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)