From d36389cfec055555e628d1fa3341b489ae3f841a Mon Sep 17 00:00:00 2001 From: Sagar Muchhal Date: Wed, 27 Dec 2023 10:52:05 -0800 Subject: [PATCH] Skip updating immutable VMOp fields Co-authored-by: Sagar Muchhal --- pkg/services/vmoperator/vmopmachine.go | 30 ++++++++++++++------- pkg/services/vmoperator/vmopmachine_test.go | 8 ++++++ pkg/util/testutil.go | 12 ++++++--- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/pkg/services/vmoperator/vmopmachine.go b/pkg/services/vmoperator/vmopmachine.go index 215c1f2d17..45264f69ae 100644 --- a/pkg/services/vmoperator/vmopmachine.go +++ b/pkg/services/vmoperator/vmopmachine.go @@ -155,8 +155,18 @@ func (v *VmopMachineService) ReconcileNormal(c context.MachineContext) (bool, er // Set the VM state. Will get reset throughout the reconcile ctx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStatePending - // Define the VM Operator VirtualMachine resource to reconcile. - vmOperatorVM := v.newVMOperatorVM(ctx) + // Check for the presence of an existing object + vmOperatorVM := &vmoprv1.VirtualMachine{} + if err := ctx.Client.Get(ctx, client.ObjectKey{ + Namespace: ctx.Machine.Namespace, + Name: ctx.Machine.Name, + }, vmOperatorVM); err != nil { + if !apierrors.IsNotFound(err) { + return false, err + } + // Define the VM Operator VirtualMachine resource to reconcile. + vmOperatorVM = v.newVMOperatorVM(ctx) + } // Reconcile the VM Operator VirtualMachine. if err := v.reconcileVMOperatorVM(ctx, vmOperatorVM); err != nil { @@ -252,10 +262,6 @@ func (v VmopMachineService) newVMOperatorVM(ctx *vmware.SupervisorMachineContext Name: ctx.Machine.Name, Namespace: ctx.Machine.Namespace, }, - TypeMeta: metav1.TypeMeta{ - APIVersion: vmoprv1.SchemeGroupVersion.String(), - Kind: "VirtualMachine", - }, } } @@ -278,9 +284,15 @@ func (v VmopMachineService) reconcileVMOperatorVM(ctx *vmware.SupervisorMachineC // Define a new VM Operator virtual machine. // NOTE: Set field-by-field in order to preserve changes made directly // to the VirtualMachine spec by other sources (e.g. the cloud provider) - vmOperatorVM.Spec.ImageName = ctx.VSphereMachine.Spec.ImageName - vmOperatorVM.Spec.ClassName = ctx.VSphereMachine.Spec.ClassName - vmOperatorVM.Spec.StorageClass = ctx.VSphereMachine.Spec.StorageClass + if vmOperatorVM.Spec.ImageName == "" { + vmOperatorVM.Spec.ImageName = ctx.VSphereMachine.Spec.ImageName + } + if vmOperatorVM.Spec.ClassName == "" { + vmOperatorVM.Spec.ClassName = ctx.VSphereMachine.Spec.ClassName + } + if vmOperatorVM.Spec.StorageClass == "" { + vmOperatorVM.Spec.StorageClass = ctx.VSphereMachine.Spec.StorageClass + } vmOperatorVM.Spec.PowerState = vmoprv1.VirtualMachinePoweredOn vmOperatorVM.Spec.ResourcePolicyName = ctx.VSphereCluster.Status.ResourcePolicyName vmOperatorVM.Spec.VmMetadata = &vmoprv1.VirtualMachineMetadata{ diff --git a/pkg/services/vmoperator/vmopmachine_test.go b/pkg/services/vmoperator/vmopmachine_test.go index 7f1fc77b69..500a98a7ef 100644 --- a/pkg/services/vmoperator/vmopmachine_test.go +++ b/pkg/services/vmoperator/vmopmachine_test.go @@ -279,6 +279,14 @@ var _ = Describe("VirtualMachine tests", func() { } requeue, err = vmService.ReconcileNormal(ctx) verifyOutput(ctx) + + By("Updates to immutable VMOp fields are dropped", func() { + vsphereMachine.Spec.ImageName = "new-image" + vsphereMachine.Spec.ClassName = "new-class" + + requeue, err = vmService.ReconcileNormal(ctx) + verifyOutput(ctx) + }) }) Specify("Reconcile will add a probe once the cluster reports that the control plane is ready", func() { diff --git a/pkg/util/testutil.go b/pkg/util/testutil.go index 456f11cb12..7553ac30d1 100644 --- a/pkg/util/testutil.go +++ b/pkg/util/testutil.go @@ -52,7 +52,8 @@ func CreateCluster(clusterName string) *clusterv1.Cluster { Kind: clusterKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: clusterName, + Name: clusterName, + Namespace: corev1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ InfrastructureRef: &corev1.ObjectReference{ @@ -71,7 +72,8 @@ func CreateVSphereCluster(clusterName string) *infrav1.VSphereCluster { Kind: infraClusterKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: clusterName, + Name: clusterName, + Namespace: corev1.NamespaceDefault, }, } } @@ -83,7 +85,8 @@ func CreateMachine(machineName, clusterName, k8sVersion string, controlPlaneLabe Kind: machineKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: machineName, + Name: machineName, + Namespace: corev1.NamespaceDefault, Labels: map[string]string{ clusterNameLabelName: clusterName, }, @@ -118,7 +121,8 @@ func CreateVSphereMachine(machineName, clusterName, className, imageName, storag Kind: infraMachineKind, }, ObjectMeta: metav1.ObjectMeta{ - Name: machineName, + Name: machineName, + Namespace: corev1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterNameLabel: clusterName, },