Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"sort"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -234,6 +235,27 @@ func ApplyVirtualMachineSpec(
return err
}

diskOrder := make(map[string]int)
for i, bd := range vm.Status.BlockDeviceRefs {
diskOrder[bd.Name] = i
}
sort.Slice(hotpluggedDevices, func(i, j int) bool {
nameI, _ := GetOriginalDiskName(hotpluggedDevices[i].VolumeName)
nameJ, _ := GetOriginalDiskName(hotpluggedDevices[j].VolumeName)
orderI, okI := diskOrder[nameI]
orderJ, okJ := diskOrder[nameJ]
if !okI && !okJ {
return hotpluggedDevices[i].VolumeName < hotpluggedDevices[j].VolumeName
}
if !okI {
return false
}
if !okJ {
return true
}
return orderI < orderJ
})

for _, device := range hotpluggedDevices {
name, kind := GetOriginalDiskName(device.VolumeName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ func (v *VirtualMachineHandler) ProcessRestore(ctx context.Context) error {
return fmt.Errorf("failed to update the `VirtualMachine`: %w", updErr)
}

vm.Status.BlockDeviceRefs = v.vm.Status.BlockDeviceRefs
updErr = v.client.Status().Update(ctx, vm)
if updErr != nil {
if apierrors.IsConflict(updErr) {
return fmt.Errorf("waiting for the `VirtualMachine` status %w", common.ErrUpdating)
}
return fmt.Errorf("failed to update the `VirtualMachine` status: %w", updErr)
}

// Always clean up VMBDAs first, regardless of VM state
err = v.deleteCurrentVirtualMachineBlockDeviceAttachments(ctx)
if err != nil {
Expand Down
Loading