Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Jul 17, 2024
1 parent 0d4d8bb commit 904ea55
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions api/core/v1alpha2/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
// ReasonUnknownHotPluggedVolume is event reason that volume was hot plugged to VirtualMachineInstance, but it is not a VirtualDisk.
ReasonUnknownHotPluggedVolume = "UnknownHotPluggedVolume"

// ReasonVDAlreadyInUse is event reason that VirtualDisk was not attached to VirtualMachine, because VirtualDisk attached to another VirtualMachine.
ReasonVDAlreadyInUse = "VirtualDiskAlreadyInUse"
// ReasonVMChangesApplied is event reason that changes applied from VM to underlying KVVM.
ReasonVMChangesApplied = "ChangesApplied"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func GenerateCVMIDiskName(name string) string {
return CVMIDiskPrefix + name
}

func GerOriginalDiskName(prefixedName string) (string, bool) {
func GetOriginalDiskName(prefixedName string) (string, bool) {
if strings.HasPrefix(prefixedName, VMDDiskPrefix) {
return strings.TrimPrefix(prefixedName, VMDDiskPrefix), true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h AttacheeHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (re

for _, vm := range attachedVMs {
vd.Status.AttachedToVirtualMachines = append(vd.Status.AttachedToVirtualMachines, virtv2.AttachedVirtualMachine{
Name: vm.Name,
Name: vm.GetName(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *BlockDeviceHandler) Handle(ctx context.Context, s state.VirtualMachineS
continue
}

vdName, ok := kvbuilder.GerOriginalDiskName(vs.Name)
vdName, ok := kvbuilder.GetOriginalDiskName(vs.Name)
if !ok {
h.logger.Warn("volume %s was hot plugged to VirtualMachineInstance %s, but it is not a VirtualDisk.", vdName, kvvmi.Name)
h.recorder.Eventf(changed, corev1.EventTypeNormal, virtv2.ReasonUnknownHotPluggedVolume, "Volume %s was hot plugged to VirtualMachineInstance %s, but it is not a VirtualDisk.", vdName, kvvmi.Name)
Expand Down Expand Up @@ -237,7 +237,19 @@ func (h *BlockDeviceHandler) countReadyBlockDevices(vm *virtv2.VirtualMachine, s
}
case virtv2.DiskDevice:
if vd, hasKey := s.VDByName[bd.Name]; hasKey {
if vd.Status.Phase == virtv2.DiskReady {
// we can attach vd to only one vm
attachReady := len(vd.Status.AttachedToVirtualMachines) == 0
for _, attached := range vd.Status.AttachedToVirtualMachines {
if attached.Name != vm.GetName() {
attachReady = false
msg := fmt.Sprintf("VirtualDisk was attached to another virtual machine %q", attached.Name)
h.logger.Warn(msg)
h.recorder.Event(vm, corev1.EventTypeWarning, virtv2.ReasonVDAlreadyInUse, msg)
break
}
attachReady = true
}
if vd.Status.Phase == virtv2.DiskReady && attachReady {
ready++
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (eh KVVMIEventHandler) getHotPluggedVolumeStatuses(obj client.Object) map[s
for _, vs := range kvvmi.Status.VolumeStatus {
if vs.HotplugVolume != nil {
var name string
name, ok = kvbuilder.GerOriginalDiskName(vs.Name)
name, ok = kvbuilder.GetOriginalDiskName(vs.Name)
if !ok {
eh.logger.Warn("VolumeStatus is not a Disk", "vsName", vs.Name, "name", kvvmi.Name, "ns", kvvmi.Namespace)
continue
Expand Down

0 comments on commit 904ea55

Please sign in to comment.