Skip to content

Commit c30a1cb

Browse files
author
Roman Sysoev
committed
feat: add bootOrder to vm block device refs status
Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
1 parent 981aa64 commit c30a1cb

5 files changed

Lines changed: 54 additions & 15 deletions

File tree

api/core/v1alpha2/block_device.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ type BlockDeviceSpecRef struct {
2222
Name string `json:"name"`
2323
// Boot order of the block device. A smaller value means a higher priority.
2424
// If the parameter is not set for any device, the boot order follows the device position in the list (starting from 1).
25-
// If the parameter is set for at least one device, the boot order is determined by the specified values.
25+
// If the parameter is set for at least one device, the boot order is determined by the specified values.
2626
// +optional
2727
// +kubebuilder:validation:Minimum=1
28-
BootOrder *int `json:"bootOrder,omitempty"`
28+
BootOrder *uint `json:"bootOrder,omitempty"`
2929
}
3030

3131
type BlockDeviceStatusRef struct {
@@ -43,6 +43,8 @@ type BlockDeviceStatusRef struct {
4343
Hotplugged bool `json:"hotplugged,omitempty"`
4444
// The name of the `VirtualMachineBlockDeviceAttachment` resource that defines hot plug disk connection to the virtual machine.
4545
VirtualMachineBlockDeviceAttachmentName string `json:"virtualMachineBlockDeviceAttachmentName,omitempty"`
46+
// Boot order of the block device. A smaller value means a higher priority.
47+
BootOrder *uint `json:"bootOrder,omitempty"`
4648
}
4749

4850
// The BlockDeviceKind is a type of the block device. Options are:

api/core/v1alpha2/zz_generated.deepcopy.go

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crds/virtualmachines.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,10 @@ spec:
11141114
type: string
11151115
description: |
11161116
Name of the VirtualMachineBlockDeviceAttachment resource that defines the hot-plug disk connection to the VM.
1117+
bootOrder:
1118+
type: integer
1119+
description: |
1120+
Boot order of the block device. A smaller value means a higher priority.
11171121
migrationState:
11181122
type: object
11191123
description: Details on the VM migration.

images/virtualization-artifact/pkg/controller/vm/internal/block_device_status.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type nameKindKey struct {
3333
name string
3434
}
3535

36+
type kvvmiDiskInfo struct {
37+
d virtv1.Disk
38+
vs virtv1.VolumeStatus
39+
}
40+
3641
// getBlockDeviceStatusRefs returns block device refs to populate .status.blockDeviceRefs of the virtual machine.
3742
// If kvvm is present, this method will reflect all volumes with prefixes (vi,vd, or cvi) into the slice of `BlockDeviceStatusRef`.
3843
// Block devices from the virtual machine specification will be added to the resulting slice if they have not been included in the previous step.
@@ -74,11 +79,20 @@ func (h *BlockDeviceHandler) getBlockDeviceStatusRefs(ctx context.Context, s sta
7479
return nil, err
7580
}
7681

77-
var kvvmiVolumeStatusByName map[string]virtv1.VolumeStatus
82+
var kvvmiDiskInfoByName map[string]kvvmiDiskInfo
7883
if kvvmi != nil {
79-
kvvmiVolumeStatusByName = make(map[string]virtv1.VolumeStatus)
84+
kvvmiDiskInfoByName = make(map[string]kvvmiDiskInfo, len(kvvmi.Status.VolumeStatus))
8085
for _, vs := range kvvmi.Status.VolumeStatus {
81-
kvvmiVolumeStatusByName[vs.Name] = vs
86+
di := kvvmiDiskInfo{
87+
vs: vs,
88+
}
89+
for _, d := range kvvmi.Spec.Domain.Devices.Disks {
90+
if d.Name == vs.Name {
91+
di.d = d
92+
break
93+
}
94+
}
95+
kvvmiDiskInfoByName[vs.Name] = di
8296
}
8397
}
8498

@@ -96,13 +110,17 @@ func (h *BlockDeviceHandler) getBlockDeviceStatusRefs(ctx context.Context, s sta
96110
key := nameKindKey{kind: kind, name: bdName}
97111

98112
ref := h.getBlockDeviceStatusRef(kind, bdName)
99-
_, ref.Attached = h.getBlockDeviceTarget(volume, kvvmiVolumeStatusByName)
113+
_, ref.Attached = h.getBlockDeviceTarget(volume, kvvmiDiskInfoByName)
100114
ref.Size, err = h.getBlockDeviceRefSize(ctx, ref, s)
101115
if err != nil {
102116
return nil, err
103117
}
118+
bootOrder := h.getBlockDeviceBootOrder(volume, kvvmiDiskInfoByName)
119+
if bootOrder != nil {
120+
ref.BootOrder = bootOrder
121+
}
104122

105-
ref.Hotplugged = h.isHotplugged(volume, kvvmiVolumeStatusByName)
123+
ref.Hotplugged = h.isHotplugged(volume, kvvmiDiskInfoByName)
106124
if ref.Hotplugged {
107125
_, isSpecDevice := specDevices[key]
108126
if !isSpecDevice {
@@ -188,15 +206,15 @@ func (h *BlockDeviceHandler) getBlockDeviceRefSize(ctx context.Context, ref v1al
188206
return "", nil
189207
}
190208

191-
func (h *BlockDeviceHandler) getBlockDeviceTarget(volume virtv1.Volume, kvvmiVolumeStatusByName map[string]virtv1.VolumeStatus) (string, bool) {
192-
vs, ok := kvvmiVolumeStatusByName[volume.Name]
193-
return vs.Target, ok
209+
func (h *BlockDeviceHandler) getBlockDeviceTarget(volume virtv1.Volume, kvvmiDiskInfoByName map[string]kvvmiDiskInfo) (string, bool) {
210+
di, ok := kvvmiDiskInfoByName[volume.Name]
211+
return di.vs.Target, ok
194212
}
195213

196-
func (h *BlockDeviceHandler) isHotplugged(volume virtv1.Volume, kvvmiVolumeStatusByName map[string]virtv1.VolumeStatus) bool {
214+
func (h *BlockDeviceHandler) isHotplugged(volume virtv1.Volume, kvvmiDiskInfoByName map[string]kvvmiDiskInfo) bool {
197215
switch {
198216
// 1. If kvvmi has volume status with hotplugVolume reference then it's 100% hot-plugged volume.
199-
case kvvmiVolumeStatusByName[volume.Name].HotplugVolume != nil:
217+
case kvvmiDiskInfoByName[volume.Name].vs.HotplugVolume != nil:
200218
return true
201219

202220
// 2. If kvvm has volume with hot-pluggable pvc reference then it's 100% hot-plugged volume.
@@ -236,3 +254,11 @@ func (h *BlockDeviceHandler) getBlockDeviceAttachmentName(ctx context.Context, k
236254

237255
return vmbdas[0].Name, nil
238256
}
257+
258+
func (h *BlockDeviceHandler) getBlockDeviceBootOrder(volume virtv1.Volume, kvvmiDiskInfoByName map[string]kvvmiDiskInfo) *uint {
259+
di, ok := kvvmiDiskInfoByName[volume.Name]
260+
if ok {
261+
return di.d.BootOrder
262+
}
263+
return nil
264+
}

images/virtualization-artifact/pkg/controller/vm/internal/validators/block_device_refs_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (v *BlockDeviceSpecRefsValidator) noDoubles(vm *v1alpha2.VirtualMachine) er
9191
}
9292

9393
func (v *BlockDeviceSpecRefsValidator) validateBootOrder(vm *v1alpha2.VirtualMachine) error {
94-
seen := make(map[int]string)
94+
seen := make(map[uint]string)
9595
for _, bdRef := range vm.Spec.BlockDeviceRefs {
9696
if bdRef.BootOrder == nil {
9797
continue

0 commit comments

Comments
 (0)