Skip to content

feat(virtualmachine): Add CD-ROM eject support via tray state#162

Open
jniedergang wants to merge 2 commits intoharvester:masterfrom
jniedergang:upstream-cdrom-eject
Open

feat(virtualmachine): Add CD-ROM eject support via tray state#162
jniedergang wants to merge 2 commits intoharvester:masterfrom
jniedergang:upstream-cdrom-eject

Conversation

@jniedergang
Copy link

@jniedergang jniedergang commented Feb 12, 2026

Summary

Add an eject boolean field to the disk block that controls the CD-ROM tray state via KubeVirt's CDRomTarget.Tray API.

  • eject = false (default): tray is closed, CD-ROM media is mounted
  • eject = true: tray is open, CD-ROM media is ejected

This enables the common workflow of installing an OS from an ISO, then ejecting the CD-ROM via Terraform without manual kubectl intervention.

Only applies to type = "cd-rom" disks. Regular disks ignore this field.

Related issue: harvester/harvester#10049

Changes

File Change
pkg/constants/constants_virtualmachine.go Add FieldDiskEject constant
internal/provider/virtualmachine/schema_virtualmachine_disk.go Add eject field (TypeBool, Optional, Default: false)
internal/provider/virtualmachine/resource_virtualmachine_constructor.go After vmBuilder.Disk(), set CDRom.Tray = TrayStateOpen when eject is true
pkg/importer/resource_virtualmachine_importer.go Read CDRom.Tray state and map to eject boolean
pkg/importer/resource_virtualmachine_importer_test.go Unit tests for eject import (3 cases: tray open, tray closed, regular disk)
docs/resources/virtualmachine.md Add eject field documentation
docs/data-sources/virtualmachine.md Add eject field documentation

Example Usage

resource "harvester_virtualmachine" "example" {
  name      = "my-vm"
  namespace = "default"
  cpu       = 2
  memory    = "4Gi"

  # Root disk
  disk {
    name       = "rootdisk"
    type       = "disk"
    size       = "20Gi"
    bus        = "virtio"
    boot_order = 1
    image      = "default/ubuntu-22.04"
  }

  # CD-ROM with ISO - set eject=true after OS installation
  disk {
    name       = "cdrom-disk"
    type       = "cd-rom"
    bus        = "sata"
    boot_order = 2
    image      = "default/my-iso"
    eject      = false  # Change to true to eject after installation
  }

  network_interface {
    name = "nic-1"
  }
}

Test plan

  • Unit tests: go test ./pkg/importer/ -run TestDiskEjectImport — 3 sub-tests (tray open, tray closed, regular disk)
  • go build ./... — compilation passes
  • gofmt -l . — no formatting issues
  • go generate ./... — docs generated
  • Functional test on Harvester v1.7.1:
    • Created VM with CD-ROM disk (type = "cd-rom", eject = false) — verified tray closed
    • Updated to eject = true — verified tray opened via kubectl get vm -o json
    • Idempotence: terraform plan shows 0 changes after eject
    • terraform destroy — VM cleaned up

@mergify
Copy link

mergify bot commented Feb 13, 2026

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

@mergify
Copy link

mergify bot commented Feb 13, 2026

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

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 pull request adds support for ejecting CD-ROM disks in Harvester virtual machines by controlling the tray state through a new eject boolean field in the disk configuration. When set to true, the CD-ROM tray is opened, effectively ejecting the media. When set to false (default), the tray remains closed with media mounted. This feature enables a common workflow where users can install an OS from an ISO and then eject the CD-ROM via Terraform without manual intervention.

Changes:

  • Added eject field to disk schema with proper validation and documentation
  • Implemented tray state control in the constructor by setting CDRom.Tray = TrayStateOpen when eject is enabled
  • Updated importer to read tray state from cluster and map it to the eject boolean
  • Added comprehensive unit tests covering CD-ROM with open tray, closed tray, and regular disks
  • Updated documentation for both resource and data source

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/constants/constants_virtualmachine.go Added FieldDiskEject constant following existing naming conventions
internal/provider/virtualmachine/schema_virtualmachine_disk.go Added eject boolean field with default value false and clear description
internal/provider/virtualmachine/resource_virtualmachine_constructor.go Implemented tray control by setting TrayStateOpen when eject is true for CD-ROM disks
pkg/importer/resource_virtualmachine_importer.go Added logic to read tray state and map to eject field for both CD-ROM and regular disks
pkg/importer/resource_virtualmachine_importer_test.go Added TestDiskEjectImport with test cases for open/closed tray states and regular disks
docs/resources/virtualmachine.md Added documentation for eject field with description
docs/data-sources/virtualmachine.md Added eject field to data source schema (missing description)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jniedergang jniedergang force-pushed the upstream-cdrom-eject branch 3 times, most recently from dd2d1a4 to 60e1fd2 Compare March 5, 2026 15:00
@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 2 commits March 6, 2026 17:31
Add eject field to disk block that controls the CD-ROM tray state.
When eject=true, the tray opens (KubeVirt TrayStateOpen), ejecting
the CD-ROM media. Only applies to cd-rom type disks.

Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
- Add description to eject field in data source docs
- Validate that eject is only set on cd-rom type disks, return
  clear error instead of silently ignoring
- Add test case for CD-ROM with no Tray field set (default behavior)

Signed-off-by: Terraform Provider Developer <terraform@harvester.local>
@jniedergang jniedergang force-pushed the upstream-cdrom-eject branch from 60e1fd2 to 01b692c Compare March 6, 2026 16:32
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.

2 participants