Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [release-1.7] Skip updating immutable VMOp fields #2580

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions pkg/services/vmoperator/vmopmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
},
}
}

Expand All @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions pkg/services/vmoperator/vmopmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 8 additions & 4 deletions pkg/util/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -71,7 +72,8 @@ func CreateVSphereCluster(clusterName string) *infrav1.VSphereCluster {
Kind: infraClusterKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Name: clusterName,
Namespace: corev1.NamespaceDefault,
},
}
}
Expand All @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down