Skip to main content

Overview

The VirtualMachine class provides a comprehensive interface for managing KubeVirt virtual machines in OpenShift. It supports lifecycle operations including start, stop, restart, and status monitoring.

Class Definition

Constructor

string
Name of the virtual machine resource.
string
Namespace where the virtual machine will be created.
DynamicClient
Kubernetes client instance for API communication.
dict
Dictionary containing the VM specification including template, domain, devices, and volumes configuration.
boolean
default:"true"
Whether to delete the resource during cleanup operations.
string
Path to a YAML file containing the VM definition.
int
default:"TIMEOUT_4MINUTES"
Timeout in seconds for deletion operations (default: 240 seconds).

Run Strategies

The VirtualMachine.RunStrategy class defines how the VM lifecycle should be managed:
  • MANUAL: VM must be manually started and stopped
  • HALTED: VM should remain stopped
  • ALWAYS: VM should always be running
  • RERUNONFAILURE: VM should restart on failure

Status Values

The VirtualMachine.Status class provides constants for VM states:
  • MIGRATING: VM is being migrated to another node
  • PAUSED: VM is paused
  • PROVISIONING: VM resources are being provisioned
  • STARTING: VM is starting up
  • STOPPED: VM is stopped
  • STOPPING: VM is shutting down
  • WAITING_FOR_VOLUME_BINDING: Waiting for volumes to be bound
  • ERROR_UNSCHEDULABLE: VM cannot be scheduled
  • DATAVOLUME_ERROR: Error with associated DataVolume
  • ERROR_PVC_NOT_FOUND: PVC not found
  • IMAGE_PULL_BACK_OFF: Image pull failures
  • ERR_IMAGE_PULL: Image pull error
  • CRASH_LOOPBACK_OFF: VM is crash looping

Methods

start()

Starts the virtual machine.
int
default:"TIMEOUT_4MINUTES"
Maximum time to wait for the VM to start (in seconds).
boolean
default:"false"
Whether to wait for the VM to reach running status.

stop()

Stops the virtual machine.
int
default:"TIMEOUT_4MINUTES"
Maximum time to wait for the VM to stop (in seconds).
int
default:"TIMEOUT_4MINUTES"
Maximum time to wait for the VMI to be deleted (in seconds).
boolean
default:"false"
Whether to wait for the VM to stop and VMI to be deleted.

restart()

Restarts the virtual machine.
int
default:"TIMEOUT_4MINUTES"
Maximum time to wait for the VM to restart (in seconds).
boolean
default:"false"
Whether to wait for the VM to complete restart.

wait_for_ready_status()

Waits for the VM to reach a specific ready status.
boolean | None
required
Target status: True for running VM, None for stopped VM.
int
default:"TIMEOUT_4MINUTES"
Maximum time to wait (in seconds).
int
default:"1"
Sleep interval between status checks (in seconds).

get_interfaces()

Returns the network interfaces configured for the VM.

Properties

vmi

Returns the associated VirtualMachineInstance object.

ready

Returns the VM ready status. Returns: True if VM is running, None otherwise.

printable_status

Returns the human-readable status string from status.printableStatus.

Usage Example

Virtual Machines Guide

Complete guide to working with virtual machines

DataVolume

Manages persistent storage for virtual machines

Notes

Virtual machines with runStrategy do not have a spec.running attribute. The VM status should be determined from status.ready.
When stopping a VM, ensure you wait for both the VM ready status to be None and the VMI to be deleted to confirm complete shutdown.