Skip to content

fix: Update virtual_machine.py example #2376

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions examples/virtual_machine.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
from kubernetes import config
from ocp_resources.virtual_machine import VirtualMachine

# Create a VM
with VirtualMachine(
config.load_kube_config()

# Define a VM
vm = VirtualMachine(
name="vm-example",
namespace="namespace-example",
node_selector="worker-node-example",
) as vm:
vm.start()
body={
"spec": {
"runStrategy": "Halted",
"template": {
"spec": {
"domain": {
"devices": {"disks": [{"name": "disk0", "disk": {"bus": "virtio"}}]},
"resources": {"requests": {"memory": "64Mi"}},
},
"volumes": [
{
"name": "disk0",
"containerDisk": {"image": "kubevirt/cirros-container-disk-demo"},
}
],
},
},
}
},
)

# VM operations
vm.create()
vm.start()
vm.vmi.wait_until_running(timeout=180)
vm.stop()
vm.restart()

# Get VM VMI
test_vmi = vm.vmi

# After having a VMI, we can wait until VMI is in running state:
test_vmi.wait_until_running()

# Then, we can get the virt launcher Pod and execute a command on it:
command_output = test_vmi.virt_launcher_pod.execute(command="command-example")