feat(vm-instance): name a GPUClass and a count in gpus

Tenants need to select an offer independently of the hoster's device-
plugin naming. Class references preserve that distinction when the
backend resource name changes.

Mixing raw GPU entries with class references would create two sources of
GPU requests on one VMI. A homogeneous list avoids that ambiguity. A
cluster without the resolving webhook must refuse references rather than
silently boot a VM without its requested GPUs.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-28 09:25:43 +03:00
1 parent 38c7ca8e8f
commit 82347bf5f5
8 files changed
+245 -14

No files matched your search

+6 -2
View File
@@ -102,8 +102,12 @@ type Firmware struct {
}
type GPU struct {
// The name of the GPU resource to attach.
Name string `json:"name"`
// GPUClass to take GPUs of.
ClassName string `json:"className,omitempty"`
// GPUs of the class; 1 when unset.
Count int `json:"count,omitempty"`
// Resource name of the GPU to request directly.
Name string `json:"name,omitempty"`
}
type Network struct {
+3 -1
View File
@@ -55,7 +55,9 @@ virtctl ssh <user>@<vm>
| `subnets[i].name` | Network attachment name. | `string` | `""` |
| `routeTable` | Name of a cozyplane RouteTable in the VM's namespace that routes the VM's traffic. Empty uses the VPC's default table. Requires the cozyplane networking variant and a VPC in `networks`. | `string` | `""` |
| `gpus` | List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM). | `[]object` | `[]` |
| `gpus[i].name` | The name of the GPU resource to attach. | `string` | `""` |
| `gpus[i].name` | Resource name of the GPU to request directly. | `string` | `""` |
| `gpus[i].className` | GPUClass to take GPUs of. | `string` | `""` |
| `gpus[i].count` | GPUs of the class; 1 when unset. | `int` | `0` |
| `cpuModel` | Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map | `string` | `""` |
| `firmware` | Firmware and boot configuration (UEFI/BIOS selection, Secure Boot, persistent EFI NVRAM). | `object` | `{}` |
| `firmware.bootloader` | Bootloader to boot the VM with: "uefi" (OVMF) or "bios" (SeaBIOS). Empty inherits the instanceProfile default. | `string` | `""` |
+10 -3
View File
@@ -36,6 +36,7 @@
{{- fail (printf "routeTable %q needs the VM to join a VPC: name the VirtualPrivateCloud in networks" $routeTable) }}
{{- end }}
{{- end }}
{{- $gpus := include "cozy-lib.gpu.entries" (list .Values.gpus .Values._cluster) | fromJson }}
apiVersion: kubevirt.io/v1
kind: VirtualMachine
@@ -80,8 +81,14 @@ spec:
sdn.cozystack.io/route-table: {{ . | quote }}
{{- end }}
{{- end }}
{{- with $gpus.classes }}
cozystack.io/gpus: {{ toJson . | quote }}
{{- end }}
labels:
{{- include "virtual-machine.labels" . | nindent 8 }}
{{- if $gpus.classes }}
cozystack.io/gpu-reference: "true"
{{- end }}
spec:
domain:
{{- $domainRes := include "virtual-machine.domainResources" . | fromJson -}}
@@ -109,11 +116,11 @@ spec:
enabled: true
{{- end }}
devices:
{{- if .Values.gpus }}
{{- with $gpus.names }}
gpus:
{{- range $i, $gpu := .Values.gpus }}
{{- range $i, $name := . }}
- name: gpu{{ add $i 1 }}
deviceName: {{ $gpu.name }}
deviceName: {{ $name }}
{{- end }}
{{- end }}
disks:
@@ -0,0 +1,155 @@
suite: vm-instance gpus
templates:
- templates/vm.yaml
release:
name: test-vm
namespace: tenant-test
set:
fullnameOverride: test-vm
_cluster:
gpu-vm-enabled: "true"
instanceType: ""
instanceProfile: ""
resources:
cpu: 2
sockets: 1
memory: 8Gi
tests:
- it: renders name entries as gpus
set:
gpus:
- name: nvidia.com/GA102GL_A10
- name: nvidia.com/GA102GL_A10
asserts:
- equal:
path: spec.template.spec.domain.devices.gpus
value:
- name: gpu1
deviceName: nvidia.com/GA102GL_A10
- name: gpu2
deviceName: nvidia.com/GA102GL_A10
- notExists:
path: spec.template.metadata.labels["cozystack.io/gpu-reference"]
- notExists:
path: spec.template.metadata.annotations["cozystack.io/gpus"]
- it: renders class entries as the annotation and the reference label only
set:
gpus:
- className: h100
count: 2
- className: a100
asserts:
- equal:
path: spec.template.metadata.annotations["cozystack.io/gpus"]
value: '[{"className":"h100","count":2},{"className":"a100","count":1}]'
- equal:
path: spec.template.metadata.labels["cozystack.io/gpu-reference"]
value: "true"
- notExists:
path: spec.template.spec.domain.devices.gpus
# A typed client sends the zero value of every field.
- it: reads a zero count and an empty name as unset
set:
gpus:
- className: h100
name: ""
count: 0
asserts:
- equal:
path: spec.template.metadata.annotations["cozystack.io/gpus"]
value: '[{"className":"h100","count":1}]'
- it: renders no gpus without entries
asserts:
- notExists:
path: spec.template.spec.domain.devices.gpus
- notExists:
path: spec.template.metadata.labels["cozystack.io/gpu-reference"]
- it: refuses an entry with both name and className
set:
gpus:
- name: nvidia.com/GA102GL_A10
className: a10
asserts:
- failedTemplate:
errorPattern: "gpus\\[0\\]: set exactly one of name and className"
- it: refuses an entry with neither
set:
gpus:
- count: 1
asserts:
- failedTemplate:
errorPattern: "gpus\\[0\\]: set exactly one of name and className"
- it: refuses count on a name entry
set:
gpus:
- name: nvidia.com/GA102GL_A10
count: 2
asserts:
- failedTemplate:
errorPattern: "gpus\\[0\\]: count applies to className entries only"
- it: refuses a count below one
set:
gpus:
- className: h100
count: -1
asserts:
- failedTemplate:
errorPattern: "gpus\\[0\\].count must be a whole number of at least 1"
- it: refuses a mixed list
set:
gpus:
- name: nvidia.com/GA102GL_A10
- className: h100
asserts:
- failedTemplate:
errorPattern: "gpus: list either only name entries or only className entries"
- it: refuses class entries where GPUs do not serve VMs
set:
_cluster:
gpu-vm-enabled: "false"
gpus:
- className: h100
asserts:
- failedTemplate:
errorPattern: "gpus: className entries need GPUs for VMs"
- it: keeps name entries where GPUs do not serve VMs
set:
_cluster:
gpu-vm-enabled: "false"
gpus:
- name: nvidia.com/GA102GL_A10
asserts:
- equal:
path: spec.template.spec.domain.devices.gpus[0].deviceName
value: nvidia.com/GA102GL_A10
- it: refuses more GPU entries than the admission expansion limit
set:
gpus:
- className: h100
count: 1025
asserts:
- failedTemplate:
errorPattern: "at most 1024 GPU units"
- it: applies the admission expansion limit across class entries
set:
gpus:
- className: h100
count: 600
- className: a100
count: 600
asserts:
- failedTemplate:
errorPattern: "at most 1024 GPU units"
+12 -4
View File
@@ -135,12 +135,20 @@
"default": [],
"items": {
"type": "object",
"required": [
"name"
],
"properties": {
"className": {
"description": "GPUClass to take GPUs of.",
"type": "string",
"x-cozystack-options": {
"source": "gpuclass"
}
},
"count": {
"description": "GPUs of the class; 1 when unset.",
"type": "integer"
},
"name": {
"description": "The name of the GPU resource to attach.",
"description": "Resource name of the GPU to request directly.",
"type": "string",
"x-cozystack-options": {
"source": "gpu"
+7 -3
View File
@@ -17,9 +17,12 @@
## @field {string} [name] - Network attachment name.
## @x-cozystack-options {source: network}
## @typedef {struct} GPU - GPU device configuration.
## @field {string} name - The name of the GPU resource to attach.
## @typedef {struct} GPU - GPU configuration. Set exactly one of name and className; a list holds either only name entries or only className entries.
## @field {string} [name] - Resource name of the GPU to request directly.
## @x-cozystack-options {source: gpu}
## @field {string} [className] - GPUClass to take GPUs of.
## @x-cozystack-options {source: gpuclass}
## @field {int} [count] - GPUs of the class; 1 when unset.
## @typedef {struct} Resources - Resource configuration for the virtual machine.
## @field {quantity} [cpu] - Number of CPU cores allocated, as a positive whole number such as `2`. Millicore or other unit-suffixed, fractional, zero, negative, above 4294967295 and quoted leading-zero values are rejected at render time, because the value becomes the VM's unsigned integer `domain.cpu.cores`.
@@ -95,7 +98,8 @@ routeTable: ""
gpus: []
## Example:
## gpus:
## - name: nvidia.com/GA102GL_A10
## - className: l40s-24c
## count: 1
## @param {string} cpuModel - Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map
cpuModel: ""
@@ -0,0 +1,51 @@
{{/*
cozy-lib.gpu.entries reads a gpus list of name and className entries and
returns, as JSON, {"names": [...], "classes": [{className, count}]}. An entry
sets exactly one of name and className, count goes with className only and
defaults to 1, and a list holds one kind. Class entries fail without
_cluster.gpu-vm-enabled, since no webhook would resolve them and the VM would
boot without its GPUs. A typed client sends the zero value of every field, so
an empty string and a count of 0 read as unset.
{{- $gpus := include "cozy-lib.gpu.entries" (list .Values.gpus .Values._cluster) | fromJson }}
*/}}
{{- define "cozy-lib.gpu.entries" -}}
{{- $gpus := index . 0 | default list -}}
{{- $cluster := index . 1 | default dict -}}
{{- $names := list -}}
{{- $classes := list -}}
{{- $units := 0 -}}
{{- range $i, $gpu := $gpus -}}
{{- $name := $gpu.name | default "" | toString -}}
{{- $class := $gpu.className | default "" | toString -}}
{{- if eq (empty $name) (empty $class) -}}
{{- fail (printf "gpus[%d]: set exactly one of name and className" $i) -}}
{{- end -}}
{{- $count := "1" -}}
{{- if not (empty $gpu.count) -}}
{{- if $name -}}
{{- fail (printf "gpus[%d]: count applies to className entries only" $i) -}}
{{- end -}}
{{- $count = printf "%v" $gpu.count -}}
{{- end -}}
{{- if not (regexMatch "^[1-9][0-9]{0,8}$" $count) -}}
{{- fail (printf "gpus[%d].count must be a whole number of at least 1" $i) -}}
{{- end -}}
{{- if $name -}}
{{- $names = append $names $name -}}
{{- else -}}
{{- $units = add $units ($count | atoi) -}}
{{- if gt $units 1024 -}}
{{- fail "a VMI can expand at most 1024 GPU units from class entries" -}}
{{- end -}}
{{- $classes = append $classes (dict "className" $class "count" ($count | atoi)) -}}
{{- end -}}
{{- end -}}
{{- if and $names $classes -}}
{{- fail "gpus: list either only name entries or only className entries" -}}
{{- end -}}
{{- if and $classes (ne (toString (index $cluster "gpu-vm-enabled" | default "false")) "true") -}}
{{- fail "gpus: className entries need GPUs for VMs, which this cluster does not serve; use name entries or ask the hoster to enable GPUs" -}}
{{- end -}}
{{- dict "names" $names "classes" $classes | toJson -}}
{{- end -}}
+1 -1
View File
@@ -12,7 +12,7 @@ spec:
singular: vminstance
plural: vminstances
openAPISchema: |-
{"title":"Chart Values","type":"object","properties":{"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"externalMethod":{"description":"Method to pass through traffic to the VM.","type":"string","default":"PortList","enum":["PortList","WholeIP"]},"externalPorts":{"description":"Ports to forward from outside the cluster.","type":"array","default":[22],"items":{"type":"integer"}},"externalAllowICMP":{"description":"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.","type":"boolean","default":true},"runStrategy":{"description":"Requested running state of the VirtualMachineInstance","type":"string","default":"Always","enum":["Always","Halted","Manual","RerunOnFailure","Once"]},"instanceType":{"description":"Virtual Machine instance type. Ignored when `resources` sizes the VM, which needs whole-number cpu and sockets plus memory; the whole type is dropped then, so any dedicated CPU placement, hugepages, NUMA or realtime settings it carries go with it. While this field is set, a `resources` block that sizes only part of the VM is rejected outright instead of ignored, so complete it or clear it.","type":"string","default":"u1.medium","x-cozystack-options":{"source":"instancetype"}},"instanceProfile":{"description":"Virtual Machine preferences profile.","type":"string","default":"ubuntu","x-cozystack-options":{"source":"instanceprofile"}},"disks":{"description":"List of disks to attach.","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"bootOrder":{"description":"Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.","type":"integer","minimum":1},"bus":{"description":"Disk bus type (e.g. \"sata\").","type":"string"},"name":{"description":"Disk name.","type":"string","x-cozystack-options":{"source":"vmdisk"}}}}},"networks":{"description":"Networks to attach the VM to. On the cozyplane networking variant the single entry names the VirtualPrivateCloud the VM joins (at most one; the VM keeps its one pod interface).","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"subnets":{"description":"Deprecated: use networks instead.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"routeTable":{"description":"Name of a cozyplane RouteTable in the VM's namespace that routes the VM's traffic. Empty uses the VPC's default table. Requires the cozyplane networking variant and a VPC in `networks`.","type":"string","default":"","maxLength":253,"pattern":"^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*)?$"},"gpus":{"description":"List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"name":{"description":"The name of the GPU resource to attach.","type":"string","x-cozystack-options":{"source":"gpu"}}}}},"cpuModel":{"description":"Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map","type":"string","default":""},"firmware":{"description":"Firmware and boot configuration (UEFI/BIOS selection, Secure Boot, persistent EFI NVRAM).","type":"object","default":{},"properties":{"bootloader":{"description":"Bootloader to boot the VM with: \"uefi\" (OVMF) or \"bios\" (SeaBIOS). Empty inherits the instanceProfile default.","type":"string"},"efiPersistent":{"description":"Persist EFI NVRAM (e.g. enrolled Secure Boot keys, such as an updated Microsoft UEFI CA) across reboots. Only applies when bootloader is \"uefi\".","type":"boolean"},"secureBoot":{"description":"Enable UEFI Secure Boot. Only applies when bootloader is \"uefi\".","type":"boolean"}}},"resources":{"description":"Resource configuration for the virtual machine. Set whole-number cpu and sockets together with memory to size the VM directly, which overrides `instanceType`. Alongside an `instanceType`, a block that sizes only part of the VM is rejected; a whole-number cpu or sockets on its own sizes nothing and is ignored.","type":"object","default":{},"properties":{"cpu":{"description":"Number of CPU cores allocated, as a positive whole number such as `2`. Millicore or other unit-suffixed, fractional, zero, negative, above 4294967295 and quoted leading-zero valLine truncated
{"title":"Chart Values","type":"object","properties":{"external":{"description":"Enable external access from outside the cluster.","type":"boolean","default":false},"externalMethod":{"description":"Method to pass through traffic to the VM.","type":"string","default":"PortList","enum":["PortList","WholeIP"]},"externalPorts":{"description":"Ports to forward from outside the cluster.","type":"array","default":[22],"items":{"type":"integer"}},"externalAllowICMP":{"description":"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.","type":"boolean","default":true},"runStrategy":{"description":"Requested running state of the VirtualMachineInstance","type":"string","default":"Always","enum":["Always","Halted","Manual","RerunOnFailure","Once"]},"instanceType":{"description":"Virtual Machine instance type. Ignored when `resources` sizes the VM, which needs whole-number cpu and sockets plus memory; the whole type is dropped then, so any dedicated CPU placement, hugepages, NUMA or realtime settings it carries go with it. While this field is set, a `resources` block that sizes only part of the VM is rejected outright instead of ignored, so complete it or clear it.","type":"string","default":"u1.medium","x-cozystack-options":{"source":"instancetype"}},"instanceProfile":{"description":"Virtual Machine preferences profile.","type":"string","default":"ubuntu","x-cozystack-options":{"source":"instanceprofile"}},"disks":{"description":"List of disks to attach.","type":"array","default":[],"items":{"type":"object","required":["name"],"properties":{"bootOrder":{"description":"Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.","type":"integer","minimum":1},"bus":{"description":"Disk bus type (e.g. \"sata\").","type":"string"},"name":{"description":"Disk name.","type":"string","x-cozystack-options":{"source":"vmdisk"}}}}},"networks":{"description":"Networks to attach the VM to. On the cozyplane networking variant the single entry names the VirtualPrivateCloud the VM joins (at most one; the VM keeps its one pod interface).","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"subnets":{"description":"Deprecated: use networks instead.","type":"array","default":[],"items":{"type":"object","properties":{"name":{"description":"Network attachment name.","type":"string","x-cozystack-options":{"source":"network"}}}}},"routeTable":{"description":"Name of a cozyplane RouteTable in the VM's namespace that routes the VM's traffic. Empty uses the VPC's default table. Requires the cozyplane networking variant and a VPC in `networks`.","type":"string","default":"","maxLength":253,"pattern":"^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*)?$"},"gpus":{"description":"List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).","type":"array","default":[],"items":{"type":"object","properties":{"className":{"description":"GPUClass to take GPUs of.","type":"string","x-cozystack-options":{"source":"gpuclass"}},"count":{"description":"GPUs of the class; 1 when unset.","type":"integer"},"name":{"description":"Resource name of the GPU to request directly.","type":"string","x-cozystack-options":{"source":"gpu"}}}}},"cpuModel":{"description":"Model specifies the CPU model inside the VMI. List of available models https://github.com/libvirt/libvirt/tree/master/src/cpu_map","type":"string","default":""},"firmware":{"description":"Firmware and boot configuration (UEFI/BIOS selection, Secure Boot, persistent EFI NVRAM).","type":"object","default":{},"properties":{"bootloader":{"description":"Bootloader to boot the VM with: \"uefi\" (OVMF) or \"bios\" (SeaBIOS). Empty inherits the instanceProfile default.","type":"string"},"efiPersistent":{"description":"Persist EFI NVRAM (e.g. enrolled Secure Boot keys, such as an updated Microsoft UEFI CA) across reboots. Only applies when bootloader is \"uefi\".","type":"boolean"},"secureBoot":{"description":"Enable UEFI Secure Boot. Only applies when bootloader is \"uefi\".","type":"boolean"}}},"resources":{"description":"Resource configuration for the virtual machine. Set whole-number cpu and sockets together with memory to size the VM directly, which overrides `instanceType`. Alongside an `instanceType`, a block that sizes only part of the VM is rejected; a whole-number cpu or sockets on its own sizes nothing and is ignored.","type":"object","default":{},"properties":{"cpu":{"description":Line truncated
release:
prefix: vm-instance-
labels: