Skip to content

Commit 1127da0

Browse files
committed
Update virtual_machine example
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 <phoracek@redhat.com>
1 parent 7339e54 commit 1127da0

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

examples/virtual_machine.py

+34-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1+
from kubernetes import config
12
from ocp_resources.virtual_machine import VirtualMachine
23

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

1135
# VM operations
36+
vm.create()
37+
vm.start()
38+
vm.vmi.wait_until_running(timeout=180)
1239
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)