Skip to content

Commit 3aaeecc

Browse files
committed
Checks for feature flag during VM reconciliation
Adds the check for the state of the anti affinity feature flag when reconciling VM. When reconciling the VM, the cluster module info should be looked up only when the NodeAntiAffinity feature flag is turned on. This also solves the issue when the control plane provider is something other than the KubeadmControlPlane, in which case we do not want to allow checks for cluster module info. Signed-off-by: Sagar Muchhal <[email protected]>
1 parent 5e5920c commit 3aaeecc

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

controllers/vspherevm_controller.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"sigs.k8s.io/controller-runtime/pkg/source"
4949

5050
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
51+
"sigs.k8s.io/cluster-api-provider-vsphere/feature"
5152
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/clustermodule"
5253
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
5354
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/identity"
@@ -285,14 +286,16 @@ func (r vmReconciler) Reconcile(ctx goctx.Context, req ctrl.Request) (_ ctrl.Res
285286
// This logic was moved to a smaller function outside of the main Reconcile() loop
286287
// for the ease of testing.
287288
func (r vmReconciler) reconcile(ctx *context.VMContext, input fetchClusterModuleInput) (reconcile.Result, error) {
288-
clusterModuleInfo, err := r.fetchClusterModuleInfo(input)
289-
// If cluster module information cannot be fetched for a VM being deleted,
290-
// we should not block VM deletion since the cluster module is updated
291-
// once the VM gets removed.
292-
if err != nil && ctx.VSphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
293-
return reconcile.Result{}, err
289+
if feature.Gates.Enabled(feature.NodeAntiAffinity) {
290+
clusterModuleInfo, err := r.fetchClusterModuleInfo(input)
291+
// If cluster module information cannot be fetched for a VM being deleted,
292+
// we should not block VM deletion since the cluster module is updated
293+
// once the VM gets removed.
294+
if err != nil && ctx.VSphereVM.ObjectMeta.DeletionTimestamp.IsZero() {
295+
return reconcile.Result{}, err
296+
}
297+
ctx.ClusterModuleInfo = clusterModuleInfo
294298
}
295-
ctx.ClusterModuleInfo = clusterModuleInfo
296299

297300
// Handle deleted machines
298301
if !ctx.VSphereVM.ObjectMeta.DeletionTimestamp.IsZero() {

controllers/vspherevm_controller_test.go

+35-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/log"
3939

4040
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
41+
"sigs.k8s.io/cluster-api-provider-vsphere/feature"
4142
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
4243
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/fake"
4344
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/identity"
@@ -522,18 +523,42 @@ func Test_reconcile(t *testing.T) {
522523
t.Run("during VM creation", func(t *testing.T) {
523524
initObjs := []client.Object{vsphereCluster, machine, vsphereVM}
524525
t.Run("when info cannot be fetched", func(t *testing.T) {
525-
r := setupReconciler(new(fake_svc.VMService), initObjs...)
526-
_, err := r.reconcile(&context.VMContext{
527-
ControllerContext: r.ControllerContext,
528-
VSphereVM: vsphereVM,
529-
Logger: r.Logger,
530-
}, fetchClusterModuleInput{
531-
VSphereCluster: vsphereCluster,
532-
Machine: machine,
526+
t.Run("when anti affinity feature gate is turned off", func(t *testing.T) {
527+
fakeVMSvc := new(fake_svc.VMService)
528+
fakeVMSvc.On("ReconcileVM", mock.Anything).Return(infrav1.VirtualMachine{
529+
Name: vsphereVM.Name,
530+
BiosUUID: "265104de-1472-547c-b873-6dc7883fb6cb",
531+
State: infrav1.VirtualMachineStateReady,
532+
}, nil)
533+
r := setupReconciler(fakeVMSvc, initObjs...)
534+
_, err := r.reconcile(&context.VMContext{
535+
ControllerContext: r.ControllerContext,
536+
VSphereVM: vsphereVM,
537+
Logger: r.Logger,
538+
}, fetchClusterModuleInput{
539+
VSphereCluster: vsphereCluster,
540+
Machine: machine,
541+
})
542+
543+
g := NewWithT(t)
544+
g.Expect(err).NotTo(HaveOccurred())
533545
})
534546

535-
g := NewWithT(t)
536-
g.Expect(err).To(HaveOccurred())
547+
t.Run("when anti affinity feature gate is turned on", func(t *testing.T) {
548+
_ = feature.MutableGates.Set("NodeAntiAffinity=true")
549+
r := setupReconciler(new(fake_svc.VMService), initObjs...)
550+
_, err := r.reconcile(&context.VMContext{
551+
ControllerContext: r.ControllerContext,
552+
VSphereVM: vsphereVM,
553+
Logger: r.Logger,
554+
}, fetchClusterModuleInput{
555+
VSphereCluster: vsphereCluster,
556+
Machine: machine,
557+
})
558+
559+
g := NewWithT(t)
560+
g.Expect(err).To(HaveOccurred())
561+
})
537562
})
538563

539564
t.Run("when info can be fetched", func(t *testing.T) {

0 commit comments

Comments
 (0)