Overview
TheVirtualMachine 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
Name of the virtual machine resource.
Namespace where the virtual machine will be created.
Kubernetes client instance for API communication.
Dictionary containing the VM specification including template, domain, devices, and volumes configuration.
Whether to delete the resource during cleanup operations.
Path to a YAML file containing the VM definition.
Timeout in seconds for deletion operations (default: 240 seconds).
Run Strategies
TheVirtualMachine.RunStrategy class defines how the VM lifecycle should be managed:
MANUAL: VM must be manually started and stoppedHALTED: VM should remain stoppedALWAYS: VM should always be runningRERUNONFAILURE: VM should restart on failure
Status Values
TheVirtualMachine.Status class provides constants for VM states:
MIGRATING: VM is being migrated to another nodePAUSED: VM is pausedPROVISIONING: VM resources are being provisionedSTARTING: VM is starting upSTOPPED: VM is stoppedSTOPPING: VM is shutting downWAITING_FOR_VOLUME_BINDING: Waiting for volumes to be boundERROR_UNSCHEDULABLE: VM cannot be scheduledDATAVOLUME_ERROR: Error with associated DataVolumeERROR_PVC_NOT_FOUND: PVC not foundIMAGE_PULL_BACK_OFF: Image pull failuresERR_IMAGE_PULL: Image pull errorCRASH_LOOPBACK_OFF: VM is crash looping
Methods
start()
Starts the virtual machine.Maximum time to wait for the VM to start (in seconds).
Whether to wait for the VM to reach running status.
stop()
Stops the virtual machine.Maximum time to wait for the VM to stop (in seconds).
Maximum time to wait for the VMI to be deleted (in seconds).
Whether to wait for the VM to stop and VMI to be deleted.
restart()
Restarts the virtual machine.Maximum time to wait for the VM to restart (in seconds).
Whether to wait for the VM to complete restart.
wait_for_ready_status()
Waits for the VM to reach a specific ready status.Target status:
True for running VM, None for stopped VM.Maximum time to wait (in seconds).
Sleep interval between status checks (in seconds).
get_interfaces()
Returns the network interfaces configured for the VM.Properties
vmi
Returns the associatedVirtualMachineInstance object.
ready
Returns the VM ready status. Returns:True if VM is running, None otherwise.
printable_status
Returns the human-readable status string fromstatus.printableStatus.
Usage Example
Related Resources
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.