Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,6 @@ argument_specs:
type: dict
required: true
description: ComputeInstance configuration
gpu_devices:
type: list
required: false
default: []
elements: dict
description: |
List of GPU devices to pass through to the virtual machine. Each entry
specifies a PCI device selector and a resource name. When set, the role
will configure the VM with GPU passthrough via the `hostDevices` field
in the KubeVirt domain devices spec, and will also ensure that the
`HyperConverged` CR (`kubevirt-hyperconverged` in the `openshift-cnv`
namespace) permits these host devices under
`spec.virtualization.permittedHostDevices.pciHostDevices` (v1) or
`spec.permittedHostDevices.pciHostDevices` (v1beta1).

The PCI vendor and product ID for a given GPU can be found by running
`lspci -nn | grep -i nvidia` on a node that has the device.
options:
pci_device_selector:
type: str
required: true
description: >
PCI vendor and product ID in "VVVV:PPPP" format, for example
"10DE:1DB6" for an NVIDIA Tesla V100.
resource_name:
type: str
required: true
description: >
The resource name for this device, for example
"nvidia.com/GV100GL_Tesla_V100". Used both in the VM
`hostDevices` spec and in the `HyperConverged` CR
`permittedHostDevices` configuration.
template_parameters:
type: dict
required: false
Expand Down
Comment thread
ygalblum marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -1,103 +1,96 @@
---

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] naming-convention

The task name uses singular 'entry' while the fact name is desired_pci_host_devices (plural). The new code always builds exactly one entry from the single GPU spec, so 'entry' is semantically accurate but inconsistent with the variable name.

- name: Build desired pciHostDevices entries from gpu_devices
ansible.builtin.set_fact:
desired_pci_host_devices: >-
[{% for dev in gpu_devices %}
{"pciDeviceSelector": "{{ dev.pci_device_selector }}",
"resourceName": "{{ dev.resource_name }}"}
{{ "," if not loop.last }}
{% endfor %}]
when: gpu_devices | default([]) | length > 0
- name: Configure permitted host devices for GPU passthrough
when: compute_instance.spec.gpu is defined
block:
- name: Build desired pciHostDevices entry from compute_instance.spec.gpu
ansible.builtin.set_fact:
desired_pci_host_devices:
- pciDeviceSelector: "{{ compute_instance.spec.gpu.pciDeviceSelector }}"
resourceName: "{{ compute_instance.spec.gpu.resourceName }}"

- name: Read current HyperConverged CR (v1)
kubernetes.core.k8s_info:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: hco.kubevirt.io/v1
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
register: hyperconverged_cr_v1
ignore_errors: true
when: gpu_devices | default([]) | length > 0
- name: Read current HyperConverged CR (v1)
kubernetes.core.k8s_info:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: hco.kubevirt.io/v1
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
register: hyperconverged_cr_v1
ignore_errors: true

- name: Read current HyperConverged CR (v1beta1 fallback)
kubernetes.core.k8s_info:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: hco.kubevirt.io/v1beta1
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
register: hyperconverged_cr_v1beta1
when:
- gpu_devices | default([]) | length > 0
- hyperconverged_cr_v1 is failed or (hyperconverged_cr_v1.resources | default([]) | length == 0)
- name: Read current HyperConverged CR (v1beta1 fallback)
kubernetes.core.k8s_info:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: hco.kubevirt.io/v1beta1
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
register: hyperconverged_cr_v1beta1
when:
- hyperconverged_cr_v1 is failed or (hyperconverged_cr_v1.resources | default([]) | length == 0)

- name: Select HyperConverged CR read result
ansible.builtin.set_fact:
hyperconverged_cr: >-
{{ hyperconverged_cr_v1
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else hyperconverged_cr_v1beta1 }}
hyperconverged_api_version: >-
{{ 'hco.kubevirt.io/v1'
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else 'hco.kubevirt.io/v1beta1' }}
hyperconverged_pci_devices_use_virtualization: >-
{{ (hyperconverged_cr_v1.resources[0].spec.virtualization is defined)
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else (hyperconverged_cr_v1beta1.resources[0].spec.virtualization is defined) }}
when: gpu_devices | default([]) | length > 0
- name: Select HyperConverged CR read result
ansible.builtin.set_fact:
hyperconverged_cr: >-
{{ hyperconverged_cr_v1
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else hyperconverged_cr_v1beta1 }}
hyperconverged_api_version: >-
{{ 'hco.kubevirt.io/v1'
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else 'hco.kubevirt.io/v1beta1' }}
hyperconverged_pci_devices_use_virtualization: >-
{{ (hyperconverged_cr_v1.resources[0].spec.virtualization is defined)
if not (hyperconverged_cr_v1 is failed)
and (hyperconverged_cr_v1.resources | default([]) | length > 0)
else (hyperconverged_cr_v1beta1.resources[0].spec.virtualization is defined) }}

- name: Compute merged pciHostDevices preserving existing entries
ansible.builtin.set_fact:
merged_pci_host_devices: "{{ _existing_pci_host_devices + _new_only }}"
vars:
_hc_spec: "{{ hyperconverged_cr.resources[0].spec }}"
_existing_pci_host_devices: >-
{{
(_hc_spec.virtualization.permittedHostDevices.pciHostDevices | default([]))
if hyperconverged_pci_devices_use_virtualization | bool
else (_hc_spec.permittedHostDevices.pciHostDevices | default([]))
}}
_existing_selectors: "{{ _existing_pci_host_devices | map(attribute='pciDeviceSelector') | list }}"
_new_only: >-
{{ desired_pci_host_devices
| rejectattr('pciDeviceSelector', 'in', _existing_selectors)
| list }}
when: gpu_devices | default([]) | length > 0
- name: Compute merged pciHostDevices preserving existing entries
ansible.builtin.set_fact:
merged_pci_host_devices: "{{ _existing_pci_host_devices + _new_only }}"
vars:
_hc_spec: "{{ hyperconverged_cr.resources[0].spec }}"
_existing_pci_host_devices: >-
{{
(_hc_spec.virtualization.permittedHostDevices.pciHostDevices | default([]))
if hyperconverged_pci_devices_use_virtualization | bool
else (_hc_spec.permittedHostDevices.pciHostDevices | default([]))
}}
_existing_selectors: "{{ _existing_pci_host_devices | map(attribute='pciDeviceSelector') | list }}"
_new_only: >-
{{ desired_pci_host_devices
| rejectattr('pciDeviceSelector', 'in', _existing_selectors)
| list }}

- name: Patch HyperConverged CR with merged pciHostDevices (virtualization path)
kubernetes.core.k8s:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: "{{ hyperconverged_api_version }}"
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
state: patched
definition:
spec:
virtualization:
permittedHostDevices:
pciHostDevices: "{{ merged_pci_host_devices }}"
when:
- gpu_devices | default([]) | length > 0
- hyperconverged_pci_devices_use_virtualization | bool
- name: Patch HyperConverged CR with merged pciHostDevices (virtualization path)
kubernetes.core.k8s:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: "{{ hyperconverged_api_version }}"
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
state: patched
definition:
spec:
virtualization:
permittedHostDevices:
pciHostDevices: "{{ merged_pci_host_devices }}"
when:
- hyperconverged_pci_devices_use_virtualization | bool

- name: Patch HyperConverged CR with merged pciHostDevices (legacy path)
kubernetes.core.k8s:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: "{{ hyperconverged_api_version }}"
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
state: patched
definition:
spec:
permittedHostDevices:
pciHostDevices: "{{ merged_pci_host_devices }}"
when:
- gpu_devices | default([]) | length > 0
- not hyperconverged_pci_devices_use_virtualization | bool
- name: Patch HyperConverged CR with merged pciHostDevices (legacy path)
kubernetes.core.k8s:
kubeconfig: "{{ remote_cluster_kubeconfig | default(omit) }}"
api_version: "{{ hyperconverged_api_version }}"
kind: HyperConverged
name: kubevirt-hyperconverged
namespace: openshift-cnv
state: patched
definition:
spec:
permittedHostDevices:
pciHostDevices: "{{ merged_pci_host_devices }}"
when:
- not hyperconverged_pci_devices_use_virtualization | bool
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
ansible.builtin.include_role:
name: osac.templates.ocp_virt_vm
tasks_from: configure_permitted_host_devices.yaml
when: gpu_devices | default([]) | length > 0
when: compute_instance.spec.gpu is defined

- name: Step - Create secrets (user-data, SSH) and add to spec
ansible.builtin.include_role:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@
domain:
devices:
hostDevices:
- name: "gpu-{{ idx }}"
deviceName: "{{ item.resource_name }}"
loop: "{{ gpu_devices | default([]) }}"
loop_control:
index_var: idx
when: gpu_devices | default([]) | length > 0
- name: "gpu-{{ item }}"
deviceName: "{{ compute_instance.spec.gpu.resourceName }}"
loop: "{{ range(compute_instance.spec.gpu.count | int) | list }}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[high] logic-error

When compute_instance.spec.gpu is not defined (no GPU requested), the loop expression range(compute_instance.spec.gpu.count | int) | list will raise an UndefinedError at template evaluation time. In Ansible, the loop directive is evaluated before the per-item when check, so when: compute_instance.spec.gpu is defined does not prevent the loop from being templated. The previous code was safe because it used loop: gpu_devices | default([]), which always evaluates to a valid (possibly empty) list. This will cause every non-GPU ComputeInstance creation to fail with an Ansible undefined variable error.

Suggested fix: Guard the loop expression with a default: loop: "{{ range((compute_instance.spec.gpu.count | default(0)) | int) | list }}"

when: compute_instance.spec.gpu is defined
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: osac.openshift.io/v1alpha1
kind: ComputeInstance
metadata:
name: test-vm-gpu
namespace: osac-system
spec:
templateID: osac.templates.ocp_virt_vm
networkAttachments:
- subnetRef: test-subnet
cores: 4
memoryGiB: 8
gpu:
pciDeviceSelector: "10DE:2901"
resourceName: "nvidia.com/GB100"
count: 1
status:
desiredConfigVersion: "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: osac.openshift.io/v1alpha1
kind: ComputeInstance
metadata:
name: test-vm-gpu-many
namespace: osac-system
spec:
templateID: osac.templates.ocp_virt_vm
networkAttachments:
- subnetRef: test-subnet
cores: 8
memoryGiB: 16
gpu:
pciDeviceSelector: "10DE:2901"
resourceName: "nvidia.com/GB100"
count: 3
status:
desiredConfigVersion: "1"
Loading
Loading