Skip to content

Commit

Permalink
Cleanup in KubeVirt#EnsureVM
Browse files Browse the repository at this point in the history
Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Dec 6, 2023
1 parent 8d48045 commit 667a378
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,34 +298,27 @@ func (r *KubeVirt) DeleteImporterPods(pvc core.PersistentVolumeClaim) (err error
}

// Ensure the kubevirt VirtualMachine exists on the destination.
func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) (err error) {
newVM, err := r.virtualMachine(vm)
if err != nil {
err = liberr.Wrap(err)
return
}

list := &cnv.VirtualMachineList{}
err = r.Destination.Client.List(
func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) error {
vms := &cnv.VirtualMachineList{}
err := r.Destination.Client.List(
context.TODO(),
list,
vms,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(r.vmLabels(vm.Ref)),
Namespace: r.Plan.Spec.TargetNamespace,
},
)
if err != nil {
err = liberr.Wrap(err)
return
return liberr.Wrap(err)
}

var virtualMachine *cnv.VirtualMachine
if len(list.Items) == 0 {
virtualMachine = newVM
err = r.Destination.Client.Create(context.TODO(), virtualMachine)
if err != nil {
err = liberr.Wrap(err)
return
if len(vms.Items) == 0 {
if virtualMachine, err = r.virtualMachine(vm); err != nil {
return liberr.Wrap(err)
}
if err = r.Destination.Client.Create(context.TODO(), virtualMachine); err != nil {
return liberr.Wrap(err)
}
r.Log.Info(
"Created Kubevirt VM.",
Expand All @@ -336,7 +329,7 @@ func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) (err error) {
"source",
vm.String())
} else {
virtualMachine = &list.Items[0]
virtualMachine = &vms.Items[0]
}

// set DataVolume owner references so that they'll be cleaned up
Expand All @@ -350,13 +343,11 @@ func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) (err error) {
Namespace: r.Plan.Spec.TargetNamespace,
})
if err != nil {
err = liberr.Wrap(err)
return
return liberr.Wrap(err)
}
pvcs, err := r.getPVCs(vm.Ref)
if err != nil {
err = liberr.Wrap(err)
return
return liberr.Wrap(err)
}

for _, pvc := range pvcs {
Expand All @@ -366,12 +357,11 @@ func (r *KubeVirt) EnsureVM(vm *plan.VMStatus) (err error) {
patch := client.MergeFrom(pvcCopy)
err = r.Destination.Client.Patch(context.TODO(), &pvc, patch)
if err != nil {
err = liberr.Wrap(err)
return
return liberr.Wrap(err)
}
}

return
return nil
}

// Delete the Secret that was created for this VM.
Expand Down

0 comments on commit 667a378

Please sign in to comment.