Skip to content

Commit f3e4f10

Browse files
lfinmauritiusclaude
andcommitted
feat(vm-instance): allow attaching a disk to a running VM
A disk added to `disks` of an existing VMInstance never reaches the running guest. KubeVirt classifies the change as non-live-updatable, sets `RestartRequired` on the VirtualMachine, and the volume only appears after the guest is restarted. A blank disk is worse: its PVC binds on `WaitForFirstConsumer`, and the only consumer that could bind it is the VMI that is not getting the volume. KubeVirt supports this declaratively behind the `DeclarativeHotplugVolumes` feature gate (Alpha since v1.6.0, Beta in v1.9.0), and requires the volume to carry `hotpluggable: true`. The chart cannot express it: `disks[]` exposes only `name` and `bus`, so no consumer of the aggregated API can ask for a hot-attach. This adds an optional `disks[].hotpluggable` that renders it. The bus is not free here. `ValidateHotplugDiskConfiguration` in pkg/storage/admitters/storagehotplug.go refuses a hotplugged disk whose bus is neither `scsi` nor `virtio`, and nothing defaults an empty bus before that admitter runs - every hotplug test upstream passes an explicit bus. The template therefore fails early, naming the disk, rather than letting the VM patch be rejected later: a rejection at that point leaves the HelmRelease in UpgradeFailed, and a stuck release keeps every subsequent change from reaching the machine, a restart included. `bootOrder` needs no change, the same admitter only requires it to be greater than zero. Rendering is unchanged when `hotpluggable` is unset. Generated files are updated accordingly, and unit tests cover the rendered field, its absence, both accepted buses and the two refusals. The field is inert until the cluster enables `DeclarativeHotplugVolumes`. That gate is documented as incompatible with the deprecated `HotplugVolumes` one - "if both are declared, HotplugVolumes will have precedence" - so the deprecated gate has to be removed, not merely complemented. Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Loïc Fontaine <[email protected]>
1 parent c8e4d97 commit f3e4f10

7 files changed

Lines changed: 140 additions & 27 deletions

File tree

‎api/apps/v1alpha1/vminstance/types.go‎

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

‎packages/apps/vm-instance/README.md‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,33 @@ virtctl ssh <user>@<vm>
3636

3737
### Common parameters
3838

39-
| Name | Description | Type | Value |
40-
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ----------- |
41-
| `external` | Enable external access from outside the cluster. | `bool` | `false` |
42-
| `externalMethod` | Method to pass through traffic to the VM. | `string` | `PortList` |
43-
| `externalPorts` | Ports to forward from outside the cluster. | `[]int` | `[22]` |
44-
| `externalAllowICMP` | Whether to accept ICMP traffic to the VM in PortList mode (preserves ping and PMTU discovery). No effect in WholeIP mode. Default true so ping behaves as users expect even when port filtering is in effect. | `bool` | `true` |
45-
| `runStrategy` | Requested running state of the VirtualMachineInstance | `string` | `Always` |
46-
| `instanceType` | Virtual Machine instance type. | `string` | `u1.medium` |
47-
| `instanceProfile` | Virtual Machine preferences profile. | `string` | `ubuntu` |
48-
| `disks` | List of disks to attach. | `[]object` | `[]` |
49-
| `disks[i].name` | Disk name. | `string` | `""` |
50-
| `disks[i].bus` | Disk bus type (e.g. "sata"). | `string` | `""` |
51-
| `networks` | Networks to attach the VM to. | `[]object` | `[]` |
52-
| `networks[i].name` | Network attachment name. | `string` | `""` |
53-
| `subnets` | Deprecated: use networks instead. | `[]object` | `[]` |
54-
| `subnets[i].name` | Network attachment name. | `string` | `""` |
55-
| `gpus` | List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM). | `[]object` | `[]` |
56-
| `gpus[i].name` | The name of the GPU resource to attach. | `string` | `""` |
57-
| `cpuModel` | Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map | `string` | `""` |
58-
| `resources` | Resource configuration for the virtual machine. | `object` | `{}` |
59-
| `resources.cpu` | Number of CPU cores allocated. | `quantity` | `""` |
60-
| `resources.memory` | Amount of memory allocated. | `quantity` | `""` |
61-
| `resources.sockets` | Number of CPU sockets (vCPU topology). | `quantity` | `""` |
62-
| `sshKeys` | List of SSH public keys for authentication. | `[]string` | `[]` |
63-
| `cloudInit` | Cloud-init user data. | `string` | `""` |
64-
| `cloudInitSeed` | Seed string to generate SMBIOS UUID for the VM. | `string` | `""` |
39+
| Name | Description | Type | Value |
40+
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ----------- |
41+
| `external` | Enable external access from outside the cluster. | `bool` | `false` |
42+
| `externalMethod` | Method to pass through traffic to the VM. | `string` | `PortList` |
43+
| `externalPorts` | Ports to forward from outside the cluster. | `[]int` | `[22]` |
44+
| `externalAllowICMP` | Whether to accept ICMP traffic to the VM in PortList mode (preserves ping and PMTU discovery). No effect in WholeIP mode. Default true so ping behaves as users expect even when port filtering is in effect. | `bool` | `true` |
45+
| `runStrategy` | Requested running state of the VirtualMachineInstance | `string` | `Always` |
46+
| `instanceType` | Virtual Machine instance type. | `string` | `u1.medium` |
47+
| `instanceProfile` | Virtual Machine preferences profile. | `string` | `ubuntu` |
48+
| `disks` | List of disks to attach. | `[]object` | `[]` |
49+
| `disks[i].name` | Disk name. | `string` | `""` |
50+
| `disks[i].bus` | Disk bus type (e.g. "sata"). | `string` | `""` |
51+
| `disks[i].hotpluggable` | Allow attaching and detaching the disk while the VM is running. Requires the DeclarativeHotplugVolumes feature gate on the cluster, and bus to be "scsi" or "virtio". | `bool` | `false` |
52+
| `networks` | Networks to attach the VM to. | `[]object` | `[]` |
53+
| `networks[i].name` | Network attachment name. | `string` | `""` |
54+
| `subnets` | Deprecated: use networks instead. | `[]object` | `[]` |
55+
| `subnets[i].name` | Network attachment name. | `string` | `""` |
56+
| `gpus` | List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM). | `[]object` | `[]` |
57+
| `gpus[i].name` | The name of the GPU resource to attach. | `string` | `""` |
58+
| `cpuModel` | Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map | `string` | `""` |
59+
| `resources` | Resource configuration for the virtual machine. | `object` | `{}` |
60+
| `resources.cpu` | Number of CPU cores allocated. | `quantity` | `""` |
61+
| `resources.memory` | Amount of memory allocated. | `quantity` | `""` |
62+
| `resources.sockets` | Number of CPU sockets (vCPU topology). | `quantity` | `""` |
63+
| `sshKeys` | List of SSH public keys for authentication. | `[]string` | `[]` |
64+
| `cloudInit` | Cloud-init user data. | `string` | `""` |
65+
| `cloudInitSeed` | Seed string to generate SMBIOS UUID for the VM. | `string` | `""` |
6566

6667

6768
## U Series

‎packages/apps/vm-instance/templates/vm.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ spec:
109109

110110
volumes:
111111
{{- range .Values.disks }}
112+
{{- if and .hotpluggable (not (has (default "" .bus) (list "scsi" "virtio"))) }}
113+
{{- fail (printf "Disk %s: hotpluggable requires bus to be 'scsi' or 'virtio', got %q" .name (default "" .bus)) }}
114+
{{- end }}
112115
- name: disk-{{ .name }}
113116
dataVolume:
114117
name: vm-disk-{{ .name }}
118+
{{- if .hotpluggable }}
119+
hotpluggable: true
120+
{{- end }}
115121
{{- end }}
116122
{{- if or .Values.cloudInit .Values.sshKeys }}
117123
- name: cloudinitdisk
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
suite: vm-instance hotpluggable disks
2+
3+
# The disks loop resolves every disk against a DataVolume in the cluster, so the
4+
# suite mocks one per disk it declares. instanceType and instanceProfile are
5+
# cleared (and resources given instead) so the two cluster lookups at the top of
6+
# the template do not need mocks of their own.
7+
release:
8+
name: test-vm
9+
namespace: tenant-test
10+
templates:
11+
- templates/vm.yaml
12+
set:
13+
fullnameOverride: test-vm
14+
_cluster: {}
15+
instanceType: ""
16+
instanceProfile: ""
17+
resources:
18+
cpu: "2"
19+
sockets: "1"
20+
memory: 2Gi
21+
kubernetesProvider:
22+
scheme:
23+
"cdi.kubevirt.io/v1beta1/DataVolume":
24+
gvr:
25+
group: "cdi.kubevirt.io"
26+
version: "v1beta1"
27+
resource: "datavolumes"
28+
namespaced: true
29+
objects:
30+
- kind: DataVolume
31+
apiVersion: cdi.kubevirt.io/v1beta1
32+
metadata:
33+
name: vm-disk-test-boot
34+
namespace: tenant-test
35+
annotations: {}
36+
- kind: DataVolume
37+
apiVersion: cdi.kubevirt.io/v1beta1
38+
metadata:
39+
name: vm-disk-test-data
40+
namespace: tenant-test
41+
annotations: {}
42+
tests:
43+
- it: leaves the volume untouched when hotpluggable is not set
44+
set:
45+
disks:
46+
- name: test-boot
47+
asserts:
48+
- equal:
49+
path: spec.template.spec.volumes[0].dataVolume.name
50+
value: vm-disk-test-boot
51+
- notExists:
52+
path: spec.template.spec.volumes[0].dataVolume.hotpluggable
53+
54+
- it: marks the volume hotpluggable when asked
55+
set:
56+
disks:
57+
- name: test-boot
58+
- name: test-data
59+
bus: scsi
60+
hotpluggable: true
61+
asserts:
62+
- equal:
63+
path: spec.template.spec.volumes[1].dataVolume.hotpluggable
64+
value: true
65+
- notExists:
66+
path: spec.template.spec.volumes[0].dataVolume.hotpluggable
67+
68+
- it: accepts the virtio bus as well
69+
set:
70+
disks:
71+
- name: test-data
72+
bus: virtio
73+
hotpluggable: true
74+
asserts:
75+
- equal:
76+
path: spec.template.spec.volumes[0].dataVolume.hotpluggable
77+
value: true
78+
79+
- it: refuses a hotpluggable disk on a bus KubeVirt would reject
80+
set:
81+
disks:
82+
- name: test-data
83+
bus: sata
84+
hotpluggable: true
85+
asserts:
86+
- failedTemplate:
87+
errorMessage: "Disk test-data: hotpluggable requires bus to be 'scsi' or 'virtio', got \"sata\""
88+
89+
- it: refuses a hotpluggable disk with no bus at all
90+
set:
91+
disks:
92+
- name: test-data
93+
hotpluggable: true
94+
asserts:
95+
- failedTemplate:
96+
errorMessage: "Disk test-data: hotpluggable requires bus to be 'scsi' or 'virtio', got \"\""

‎packages/apps/vm-instance/values.schema.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
"description": "Disk bus type (e.g. \"sata\").",
7474
"type": "string"
7575
},
76+
"hotpluggable": {
77+
"description": "Allow attaching and detaching the disk while the VM is running. Requires the DeclarativeHotplugVolumes feature gate on the cluster, and bus to be \"scsi\" or \"virtio\".",
78+
"type": "boolean"
79+
},
7680
"name": {
7781
"description": "Disk name.",
7882
"type": "string",

‎packages/apps/vm-instance/values.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## @field {string} name - Disk name.
1111
## @x-cozystack-options {source: vmdisk}
1212
## @field {string} [bus] - Disk bus type (e.g. "sata").
13+
## @field {bool} [hotpluggable] - Allow attaching and detaching the disk while the VM is running. Requires the DeclarativeHotplugVolumes feature gate on the cluster, and bus to be "scsi" or "virtio".
1314

1415
## @typedef {struct} Network - Network to attach the VM to.
1516
## @field {string} [name] - Network attachment name.
@@ -62,6 +63,9 @@ disks: []
6263
## - name: example-system
6364
## - name: example-data
6465
## bus: sata
66+
## - name: example-hotplug
67+
## bus: scsi
68+
## hotpluggable: true
6569

6670
## @param {[]Network} networks - Networks to attach the VM to.
6771
networks: []

0 commit comments

Comments
 (0)