Skip to content

Commit

Permalink
fix #437: Post VMs with desired 'Running' state
Browse files Browse the repository at this point in the history
Long time ago, we posted VMs at the beginning of the migration and then
started and stopped them during the migration and therefore in order to
restore their original power state, we had to initiate another call to
patch the VM with its desired power state.

However, now that we post the VMs at the very last steps of the
migration, we can set their desired power state in their specification
and avoid another call to the target environment.

Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Nov 7, 2023
1 parent a5c5400 commit 1cbea2e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 54 deletions.
15 changes: 2 additions & 13 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,18 +427,6 @@ func (r *KubeVirt) DeleteVM(vm *plan.VMStatus) (err error) {
return
}

// Set the Running state on a Kubevirt VirtualMachine.
func (r *KubeVirt) SetRunning(vmCr *VirtualMachine, running bool) (err error) {
vmCopy := vmCr.VirtualMachine.DeepCopy()
vmCr.VirtualMachine.Spec.Running = &running
patch := client.MergeFrom(vmCopy)
err = r.Destination.Client.Patch(context.TODO(), vmCr.VirtualMachine, patch)
if err != nil {
err = liberr.Wrap(err)
}
return
}

func (r *KubeVirt) DataVolumes(vm *plan.VMStatus) (dataVolumes []cdi.DataVolume, err error) {
secret, err := r.ensureSecret(vm.Ref, r.secretDataSetterForCDI(vm.Ref))
if err != nil {
Expand Down Expand Up @@ -1006,7 +994,8 @@ func (r *KubeVirt) virtualMachine(vm *plan.VMStatus) (object *cnv.VirtualMachine
object.ObjectMeta.Annotations = annotations
}

running := false
// Power on the destination VM if the source VM was originally powered on.
running := vm.RestorePowerState == On
object.Spec.Running = &running

err = r.Builder.VirtualMachine(vm.Ref, &object.Spec, pvcs)
Expand Down
41 changes: 0 additions & 41 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1052,15 +1052,6 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) {
Durable: true,
})

// Power on the destination VM if the source VM was originally powered on.
err = r.setRunning(vm, vm.RestorePowerState == On)
if err != nil {
r.Log.Error(err,
"Could not power on destination VM.",
"vm",
vm.String())
err = nil
}
} else if vm.Error != nil {
vm.Phase = Completed
vm.SetCondition(
Expand Down Expand Up @@ -1317,38 +1308,6 @@ func (r *Migration) ensureGuestConversionPod(vm *plan.VMStatus) (err error) {
return
}

// Set the running state of the kubevirt VM.
func (r *Migration) setRunning(vm *plan.VMStatus, running bool) (err error) {
if r.vmMap == nil {
r.vmMap, err = r.kubevirt.VirtualMachineMap()
if err != nil {
return
}
}
var vmCr VirtualMachine
found := false
if vmCr, found = r.vmMap[vm.ID]; !found {
// Recreate the map and check again, the map may be stale
r.vmMap, err = r.kubevirt.VirtualMachineMap()
if err != nil {
return
}

if vmCr, found = r.vmMap[vm.ID]; !found {
msg := "VirtualMachine CR not found."
vm.AddError(msg)
return
}
}

if vmCr.Spec.Running != nil && *vmCr.Spec.Running == running {
return
}

err = r.kubevirt.SetRunning(&vmCr, running)
return
}

// Update the progress of the appropriate disk copy step. (DiskTransfer, Cutover)
func (r *Migration) updateCopyProgress(vm *plan.VMStatus, step *plan.Step) (err error) {
var pendingReason string
Expand Down

0 comments on commit 1cbea2e

Please sign in to comment.