Repository navigation
OSAC-3183: update ocp_virt_vm role to read GPU from CR payload #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
da44edd
d933a45
9bd63c1
9de0f6d
5b2eec5
95601e3
e7c46bf
e42201d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,103 +1,96 @@ | ||
| --- | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
|---|---|---|
|
|
@@ -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 }}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
Uh oh!
There was an error while loading. Please reload this page.