Skip to content

feat: Add harvester_pci_device resource for PCI device passthrough#157

Open
jniedergang wants to merge 8 commits intoharvester:masterfrom
jniedergang:upstream-pci-device
Open

feat: Add harvester_pci_device resource for PCI device passthrough#157
jniedergang wants to merge 8 commits intoharvester:masterfrom
jniedergang:upstream-pci-device

Conversation

@jniedergang
Copy link

@jniedergang jniedergang commented Feb 5, 2026

Summary

Add a new Terraform resource harvester_pci_device for 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

  • 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

Required Fields

  • 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 (format: 0000:XX:YY.Z)

Example Usage

resource "harvester_pci_device" "example" {
  name      = "example-pci-device-claim"
  namespace = "default"

  vm_name   = "default/my-vm"
  node_name = "node-01"

  pci_addresses = [
    "0000:01:00.0",  # GPU
  ]

  labels = {
    environment = "production"
  }
}

Test plan

  • Unit tests: go test ./pkg/importer/ -run TestPCIDevice — pass
  • go build ./... — compilation passes
  • gofmt -l . — no formatting issues
  • go generate ./... — docs generated
  • Functional test — requires PCI device (GPU) passthrough hardware

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_device resource for PCI device passthrough management
  • Helper function PCIDeviceIDParts for 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.

@jniedergang jniedergang force-pushed the upstream-pci-device branch 4 times, most recently from f76e661 to b78f623 Compare March 6, 2026 16:49
@mergify
Copy link

mergify bot commented Mar 6, 2026

This pull request is now in conflict. Could you fix it @jniedergang? 🙏

Terraform Provider Developer added 6 commits March 6, 2026 17:52
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>
@jniedergang jniedergang force-pushed the upstream-pci-device branch from b78f623 to ca4305d Compare March 6, 2026 16:52
…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>
@jniedergang
Copy link
Author

Functional test completed on Harvester v1.7.1 using the Intel WiFi card (0000:00:14.3, Comet Lake PCH-LP CNVi WiFi) on node harv1.home.lo.

Results:

  • terraform apply: PCIDeviceClaim created successfully, VM existence validated
  • terraform plan (idempotence): 0 changes — after fixing vm_name drift (DiffSuppressFunc for bare name vs namespace/name equivalence)
  • terraform destroy: claim deletion issued, Harvester controller finalizer held (expected for a device not configured for passthrough — the controller can't unbind the in-use iwlwifi driver)

Fix in f33879f: Added DiffSuppressFunc on vm_name field to suppress false drift when user specifies "rhel8-test" but Read returns "default/rhel8-test". Also cleared AtLeastOneOf in DataSourceSchemaWrap to prevent validation errors on computed datasource fields.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add harvester_pci_device resource for PCI device passthrough

2 participants