Skip to content

Commit 106e999

Browse files
authored
Update virtual_machine example (#2376)
The original example did not work. This patch replaces it with one that does, using the body attribute to provide a clear self-contained example. Signed-off-by: Petr Horacek <[email protected]>
1 parent 7339e54 commit 106e999

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

examples/virtual_machine.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1+
from kubernetes import config
12
from ocp_resources.virtual_machine import VirtualMachine
23

3-
# Create a VM
4-
with VirtualMachine(
4+
config.load_kube_config()
5+
6+
# Define a VM
7+
vm = VirtualMachine(
58
name="vm-example",
69
namespace="namespace-example",
7-
node_selector="worker-node-example",
8-
) as vm:
9-
vm.start()
10+
body={
11+
"spec": {
12+
"runStrategy": "Halted",
13+
"template": {
14+
"spec": {
15+
"domain": {
16+
"devices": {"disks": [{"name": "disk0", "disk": {"bus": "virtio"}}]},
17+
"resources": {"requests": {"memory": "64Mi"}},
18+
},
19+
"volumes": [
20+
{
21+
"name": "disk0",
22+
"containerDisk": {"image": "kubevirt/cirros-container-disk-demo"},
23+
}
24+
],
25+
},
26+
},
27+
}
28+
},
29+
)
1030

1131
# VM operations
32+
vm.create()
33+
vm.start()
34+
vm.vmi.wait_until_running(timeout=180)
1235
vm.stop()
13-
vm.restart()
14-
15-
# Get VM VMI
16-
test_vmi = vm.vmi
17-
18-
# After having a VMI, we can wait until VMI is in running state:
19-
test_vmi.wait_until_running()
20-
21-
# Then, we can get the virt launcher Pod and execute a command on it:
22-
command_output = test_vmi.virt_launcher_pod.execute(command="command-example")

0 commit comments

Comments
 (0)