Skip to content

Numerous improvements - #22

Open
seanmil wants to merge 14 commits into
hashicorp:mainfrom
seanmil:improvements
Open

Numerous improvements#22
seanmil wants to merge 14 commits into
hashicorp:mainfrom
seanmil:improvements

Conversation

@seanmil

@seanmil seanmil commented Sep 23, 2025

Copy link
Copy Markdown

Description

This PR contains a series of somewhat related improvements including:

  • Aligning the communicator configuration options with the standard ones as provided in the packer-plugin-sdk
  • Leverage the packer-plugin-sdk communicator setup routines to reduce custom code and enable additional features (e.g., SSH agent forwarding)
  • Modify the port forwarding functionality to default to allocating a random ephemeral port
  • Fix the port forwarding routine to gracefully handle connection errors, to permit communicator availability polling to work properly
  • Add a way to disable port forwarding for direct communicator connections
  • Add a wait_for_ip step which waits for the VM to report a guest IP and then provides that IP for possible use in later steps
  • Add an optional wait_for_agent step which could be used to detect the phase of the installation where the Guest Agent becomes available

@seanmil
seanmil requested a review from a team as a code owner September 23, 2025 14:10
@hashicorp-cla-app

hashicorp-cla-app Bot commented Sep 23, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@seanmil

seanmil commented Sep 23, 2025

Copy link
Copy Markdown
Author

Notes:

  • This is my first foray into Go, so please keep that in mind. I welcome any feedback on things I could have done better.
  • I have tested this successfully using a RHEL 9 build with pod networking using port-forwarding, a Multus network using port forwarding, and a Multus network and a direct connection using the guest-detected IP.

@dominikholler

Copy link
Copy Markdown

Nice to see the packer-plugin-kubevirt evolving!
Are the changes worth to adjust the included examples?

@seanmil

seanmil commented Sep 25, 2025

Copy link
Copy Markdown
Author

Nice to see the packer-plugin-kubevirt evolving! Are the changes worth to adjust the included examples?

I took a quick look at the existing examples and, yes, I believe it would be worth adjusting them. I'll work on that and push an update. Thanks!

@codingben

Copy link
Copy Markdown
Contributor

/cc

@codingben codingben left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, thanks for taking care of this. I think examples should be updated as well and I want to make sure that the changes being tested against a real k8s cluster.

Refactor communicator-related options to leverage and align
with the provided functionality in the packer-plugin-sdk.

This change preserves backward compatibility with options
having different names but marks them as deprecated in favor
of the packer-plugin-sdk provided ones.
Refactor the communicator setup to remove the custom setup
code and instead use the packer-plugin-sdk provided setup.
@seanmil

seanmil commented Oct 21, 2025

Copy link
Copy Markdown
Author

I just pushed a big update versus my first go at it. In order to properly build the whole set of systems I needed in my environment I needed to add more configuration options. I did include updated and expanded examples (adding SLES and Ubuntu), as well as significantly expanded unit test coverage vs. the last push.

Modeled after the VMware plugin's IP wait, this adds a wait
on the availability of the VM's IP and then provides the IP
for use in other steps. If no forwarding is in use and
no IP/host was manually specified, the detected IP will
be used as the default.
With the addition of ip_wait_timeout the
installation_wait_timeout option should no longer be
marked as as required option.
Enable port forwarding to, by default, select and using a local
ephemeral port as the forwarding port. Also, adjust port
forwarding routines to permit connection failures gracefully
allowing for retries to support the communicator wait for
SSH/WinRM workflows.
If enabled, the builder will wait for the Guest Agent to be
reported as online prior to moving to the wait_for_ip step.
Allow the temporary VM name to be specified separately from
the desired resulting DataSource name. If not specified,
VMName will default to the final DataSource name.
Add new configuration options media_content, media_label, and
keep_media to allow better flexibility and configurability
of the built ConfigMap-based media specification.
Enable users to specify their own container pull path for the
VirtIO Container image with the required Windows drivers.
Also, update the default container URL to the latest stable
version available.
Expose the ability to set the accessMode and volumeMode used when
creating the PVC holding the temporary VM's rootdisk and the
PVC backing the resulting DataVolume built image.
Refactor the create_bootablevolume and create_virtualmachine
tests to better support easy overrides of configuration values
to test different scenarios, and using this new refactor,
expand the testing coverage.
When specifying the "name" parameter in a simple HCL configuration,
using a static block definition, it gets correctly defined.

However, when using dynamic blocks the value set for "name" does
not get propagated into the builder configuration structure.

Exposing template_name as a separate parameter, and defaulting it
to "name" if defined, preserves backwards compatibility but also
allows the resulting template volume name to be properly set
when leveraging dynamic blocks in the configuration.
@sulaweyo

Copy link
Copy Markdown

I just went through this PR as I'm currently working on s390 kubevirt images and was glad of how many cool things are there 👍
What jumped my eye though, was the sata bus for the cdrom drives which does not work on s390x as there is no sata available. Currently it's working as qemu decides which bus it uses and on s390x that is scsi.
I'd either not define the bus or allow configuration

@cskarby

cskarby commented Jan 23, 2026

Copy link
Copy Markdown

@codingben is there anything we can do to help make progress with the packer-plugin-kubevirt? E.g. split this PR into smaller pieces or anything else that makes it easier to review and make momentum?

@codingben

codingben commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

@cskarby I'm no longer active here as I'm contributing to KubeVirt projects. @tanmay-hc @anurag5sh Do you plan to review this PR?

@EugenSusurrus

Copy link
Copy Markdown

Would be really nice to see these changes merged. Especially the ssh communicator ones, since the current kubevirt source, requires the ssh host to be set, This Makes the plugin virtually impossible to use in environments where the ip of the built VM is set dynamically.

Kind of sad that it takes so long to review and make it happen

peschmae pushed a commit to peschmae/packer-plugin-kubevirt that referenced this pull request Mar 24, 2026
…, wait steps, media config, access/volume modes
@anurag5sh
anurag5sh requested review from Copilot and removed request for codingben July 14, 2026 06:18

Copilot AI left a comment

Copy link
Copy Markdown

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 modernizes the KubeVirt ISO builder by aligning communicator configuration with the packer-plugin-sdk, reducing custom communicator code, and adding new optional wait steps (guest IP + guest agent) along with improved media handling and documentation updates.

Changes:

  • Adopt packer-plugin-sdk communicator configuration and connection steps (SSH/WinRM) and update docs/examples accordingly.
  • Add wait_for_ip and wait_for_agent steps (plus config/docs) to support richer build-time readiness detection.
  • Improve media handling via media_content/media_files, add PVC access/volume mode options, and enhance port forwarding behavior/documentation.

Reviewed changes

Copilot reviewed 39 out of 44 changed files in this pull request and generated 16 comments.

Show a summary per file
File Description
examples/builder/kubevirt-iso/windows/windows.pkr.hcl Updates Windows example to template autounattend content and align WinRM config with SDK conventions.
examples/builder/kubevirt-iso/windows/autounattend.xml.tpl Makes AcceptEula templated via templatefile input.
examples/builder/kubevirt-iso/ubuntu/ubuntu.pkr.hcl Adds a new Ubuntu ISO example showcasing cloud-init media + guest agent waiting and SSH communicator settings.
examples/builder/kubevirt-iso/ubuntu/ubuntu-iso.yaml Adds a DataVolume manifest for fetching the Ubuntu installer ISO.
examples/builder/kubevirt-iso/ubuntu/autoinstall.yml Adds Ubuntu autoinstall configuration (cloud-init) for unattended install.
examples/builder/kubevirt-iso/sles/sles.pkr.hcl Adds a new SLES ISO example with SSH communicator and guest agent waiting.
examples/builder/kubevirt-iso/sles/sles-iso.yaml Adds a DataVolume manifest template for a SLES ISO.
examples/builder/kubevirt-iso/sles/autoinst.xml Adds SLES AutoYaST profile used by the SLES example.
examples/builder/kubevirt-iso/rhel/rhel.pkr.hcl Updates RHEL example to show media_content usage and new wait behavior.
examples/builder/kubevirt-iso/README.md Links new Ubuntu and SLES examples.
examples/builder/kubevirt-iso/fedora/fedora.pkr.hcl Updates Fedora example to reflect new wait/communicator conventions.
docs/builders/iso.mdx Restructures builder docs into clearer sections and includes new partials (media/wait/communicator).
docs-partials/builder/kubevirt/iso/WaitIpConfig-not-required.mdx Adds generated docs for wait_for_ip configuration.
docs-partials/builder/kubevirt/iso/WaitForAgentConfig-not-required.mdx Adds generated docs for wait_for_agent configuration.
docs-partials/builder/kubevirt/iso/PortForwardConfig-not-required.mdx Adds generated docs for new port-forwarding controls.
docs-partials/builder/kubevirt/iso/MediaConfig-not-required.mdx Adds generated docs for media_files/media_content support.
docs-partials/builder/kubevirt/iso/Config-required.mdx Updates required config docs to use template_name terminology.
docs-partials/builder/kubevirt/iso/Config-not-required.mdx Documents new optional fields (vm_name, access_mode, volume_mode, virtio_container, etc.).
builder/kubevirt/iso/step_wait_for_ip.hcl2spec.go Adds HCL2 spec generation output for WaitIpConfig.
builder/kubevirt/iso/step_wait_for_ip.go Implements wait_for_ip step and IP settling logic.
builder/kubevirt/iso/step_wait_for_ip_test.go Adds tests for wait_for_ip behavior.
builder/kubevirt/iso/step_wait_for_agent.hcl2spec.go Adds (currently incomplete) HCL2 spec generation output for wait-for-agent.
builder/kubevirt/iso/step_wait_for_agent.go Implements wait_for_agent step (Guest Agent availability).
builder/kubevirt/iso/step_wait_for_agent_test.go Adds tests for wait_for_agent behavior.
builder/kubevirt/iso/step_stop_virtualmachine.go Switches stop logic to use VMName (temporary VM) rather than image/template name.
builder/kubevirt/iso/step_stop_virtualmachine_test.go Updates tests for VMName usage.
builder/kubevirt/iso/step_start_portforward.hcl2spec.go Adds HCL2 spec generation output for PortForwardConfig.
builder/kubevirt/iso/step_start_portforward.go Refactors port-forwarding step to support ephemeral ports and forwarding disablement.
builder/kubevirt/iso/step_start_portforward_test.go Updates port-forward tests for new factory contract and communicator config struct.
builder/kubevirt/iso/step_create_virtualmachine.go Extends VM creation to include media label, virtio container, and PVC access/volume mode.
builder/kubevirt/iso/step_create_virtualmachine_test.go Expands VM creation tests for Linux/Windows disk/volume layout and PVC modes.
builder/kubevirt/iso/step_create_bootablevolume.go Updates bootable volume creation to use template/vm naming split and PVC modes.
builder/kubevirt/iso/step_create_bootablevolume_test.go Updates/expands tests for clone DV and DataSource labeling and PVC modes.
builder/kubevirt/iso/step_copy_media_files.hcl2spec.go Adds HCL2 spec generation output for MediaConfig.
builder/kubevirt/iso/step_copy_media_files.go Refactors media handling to support both file-copy and inline content and adds keep_media.
builder/kubevirt/iso/step_copy_media_files_test.go Updates tests for media file reading and inline media_content.
builder/kubevirt/iso/step_boot_command.go Switches VNC boot automation to use VMName.
builder/kubevirt/iso/resources.go Refactors resource constructors (VM disks/volumes, configmap) and adds access/volume mode conversion helpers.
builder/kubevirt/iso/config.hcl2spec.go Updates HCL2 config schema to include new communicator/media/wait/port-forward fields.
builder/kubevirt/iso/config.go Refactors config to embed SDK communicator config + new sub-configs and adds backwards-compat remapping.
builder/kubevirt/iso/builder.go Reworks build pipeline to use SDK communicator steps and adds optional wait steps + port-forward control.
builder/kubevirt/common/portforwarder.go Modifies port-forwarder to support ephemeral port allocation and more tolerant connection handling.
builder/kubevirt/common/comm_config.go Adds helper functions to select communicator host/port from state (forwarded host/port, guest IP, explicit host).
.web-docs/components/builder/iso/README.md Updates generated web docs to include new config sections and SDK communicator docs.
Files not reviewed (5)
  • builder/kubevirt/iso/config.hcl2spec.go: Generated file
  • builder/kubevirt/iso/step_copy_media_files.hcl2spec.go: Generated file
  • builder/kubevirt/iso/step_start_portforward.hcl2spec.go: Generated file
  • builder/kubevirt/iso/step_wait_for_agent.hcl2spec.go: Generated file
  • builder/kubevirt/iso/step_wait_for_ip.hcl2spec.go: Generated file

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

Comment on lines +63 to +67
localAddress = "localhost"
remotePort = s.Config.Comm.Port()

ui.Sayf("Preparing port forwarding from %s:%d to VM on port %d", localAddress, localPort, remotePort)

Comment on lines +66 to 70
ui.Sayf("Preparing port forwarding from %s:%d to VM on port %d", localAddress, localPort, remotePort)

address, _ := net.ResolveIPAddr("ip", localAddress)
vmi := s.Client.VirtualMachineInstance(namespace)

Comment on lines 84 to 88
conn, err := listener.Accept()
if err != nil {
log.Log.Errorf("error accepting connection: %v", err)
return
fmt.Printf("port forwarder: error accepting connection: %v", err)
continue
}
Comment on lines +51 to +58
if c.SettleTimeout == 0 {
c.SettleTimeout = 5 * time.Second
}
if c.WaitTimeout == 0 {
c.WaitTimeout = 30 * time.Minute
}

return errs
Comment on lines +26 to +29
// Amount of time to wait for VM's IP, similar to 'ssh_timeout'.
// Defaults to `30m` (30 minutes). Refer to the Golang
// [ParseDuration](https://golang.org/pkg/time/#ParseDuration)
// documentation for full details.
Comment on lines +87 to +89
build {
sources = ["source.kubevirt-iso.fedora"]

Comment on lines +18 to +22
source "kubevirt-iso" "fedora" {
# Kubernetes configuration
kube_config = var.kube_config
name = "sles-15-sp7-rand-85"
namespace = "images"
Comment on lines +67 to +69
build {
sources = ["source.kubevirt-iso.fedora"]

# ISO configuration
iso_volume_name = "rhel-10-x86-64-iso"

# PVC configuratoin
Comment on lines +30 to +37
// Content is a map of content to include in the generated media volume.
// The map keys are the filenames and the map values are the file
// contents. This permits the use of HCL functions such as `file` and
// `templatefile` to populate the ConfigMap.
// If a filename matches a file included in `media_files` then the
// contents specified here takes precedence.
// Defaults to an empty list.
Content map[string]string `mapstructure:"media_content" required:"false"`
@anurag5sh

anurag5sh commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

@seanmil thanks for raising this PR. I will review this but do note that I don't have any prior experience working with kubevirt.

Could you address the copilot comments (if at all they are valid). Once the conflicts are resolved we can get this PR move forward. Thanks!

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.

8 participants