feat: Add harvester_pci_device resource for PCI device passthrough#157
feat: Add harvester_pci_device resource for PCI device passthrough#157jniedergang wants to merge 8 commits intoharvester:masterfrom
Conversation
6a63bb6 to
e622366
Compare
217dcf7 to
fb829a5
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds a new Terraform resource harvester_pci_device for managing PCI device passthrough to VirtualMachines in Harvester using the PCIDeviceClaim CRD. The implementation enables users to attach PCI devices (such as GPUs) directly to VMs through Terraform configuration.
Changes:
- New
harvester_pci_deviceresource for PCI device passthrough management - Helper function
PCIDeviceIDPartsfor parsing PCI device resource IDs - Constants and schema definitions for PCI device resource fields
- Documentation and examples for resource usage
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 14 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/helper/id.go | Adds PCIDeviceIDParts helper for parsing 3-part IDs (namespace/vmname/claimname) |
| pkg/constants/constants_pcidevice.go | Defines constants for PCI device resource fields and resource type name |
| internal/provider/provider.go | Registers the new harvester_pci_device resource with the provider |
| internal/provider/pcidevice/schema_pcidevice.go | Defines Terraform schema for PCI device resource with validation |
| internal/provider/pcidevice/resource_pcidevice.go | Implements CRUD operations for PCIDeviceClaim resources using dynamic client |
| examples/resources/harvester_pci_device/resource.tf | Provides usage examples for attaching PCI devices to VMs |
| docs/resources/pci_device.md | Generated documentation for the harvester_pci_device resource |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9f380af to
2a63d6a
Compare
f76e661 to
b78f623
Compare
|
This pull request is now in conflict. Could you fix it @jniedergang? 🙏 |
Add a new Terraform resource for managing PCI device passthrough to VMs using Harvester's PCIDeviceClaim CRD. This enables GPU and other PCI device passthrough directly from Terraform. Features: - Create PCIDeviceClaim resources to attach PCI devices to VMs - Validate VM existence before creating claims - Support for multiple PCI addresses per resource - Automatic claim name generation based on node/address format - Support for labels on claims The resource requires: - vm_name: Target VM in namespace/name format - node_name: Required node for scheduling (mandatory for PCI passthrough) - pci_addresses: List of PCI addresses to attach Signed-off-by: Jean-Daniel Niedergang <jniedergang@outlook.com> Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
- Do not set 'name' field in Read function to prevent drift The PCIDeviceClaim name is auto-generated and differs from the user-provided Terraform resource name - Replace TODO comment with clear documentation explaining the single-address-per-claim design Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
Extract helper functions to reduce cyclomatic complexity from 20 to ~8: - Add PCIDeviceIDParts() helper for parsing 3-part PCI device IDs - Extract setPCIDeviceResourceData() for consolidated state setting - Add getSpecField() and extractLabelsFromUnstructured() helpers - Also refactor Update and Delete to use the new ID parser Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
Remove trailing whitespace on blank lines and trailing empty lines at end of files to pass go fmt validation in CI. Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
- Remove duplicate FieldPCIDeviceLabels constant (conflicts with FieldCommonLabels added by NamespacedSchemaWrap) - Use FieldCommonLabels consistently in resource code - Fix PCIDeviceIDParts to reject IDs with too many parts (< 3 -> != 3) - Remove redundant node_name/pci_addresses empty checks (schema enforces) - Remove redundant Get before Create (IsAlreadyExists handles it) - Fix delete to remove all PCIDeviceClaims, not just the first one - Simplify example and schema descriptions Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
Read now verifies all PCIDeviceClaim resources (one per PCI address) exist and returns all addresses, preventing phantom drift when multiple addresses are configured. Update now properly handles address list changes: creates new claims for added addresses, deletes claims for removed addresses, and updates labels on all existing claims. Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
b78f623 to
ca4305d
Compare
…asource Add DiffSuppressFunc on vm_name to handle bare name vs namespace/name equivalence (e.g. "rhel8-test" == "default/rhel8-test"). Clear AtLeastOneOf in DataSourceSchemaWrap to prevent InternalValidate errors on computed datasource fields. Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
|
Functional test completed on Harvester v1.7.1 using the Intel WiFi card ( Results:
Fix in f33879f: Added |
Rename DiffSuppressFunc parameters from 'old'/'new' to 'oldVal'/'newVal' to avoid shadowing Go's builtin new() function, fixing CodeFactor maintainability warning. Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
Summary
Add a new Terraform resource
harvester_pci_devicefor managing PCI device passthrough to VMs using Harvester's PCIDeviceClaim CRD. This enables GPU and other PCI device passthrough directly from Terraform.Resolves harvester/harvester#10018
Related to harvester/harvester#6423
Features
Required Fields
vm_name: Target VM in namespace/name formatnode_name: Required node for scheduling (mandatory for PCI passthrough)pci_addresses: List of PCI addresses to attach (format:0000:XX:YY.Z)Example Usage
Test plan
go test ./pkg/importer/ -run TestPCIDevice— passgo build ./...— compilation passesgofmt -l .— no formatting issuesgo generate ./...— docs generated