A python wrapper for https://github.com/openshift/openshift-restclient-python.
With support for RedHat Container Virtualization. (Openshift Virtualization) https://www.openshift.com/learn/topics/virtualization/
From source:
git clone https://github.com/RedHatQE/openshift-python-wrapper.git
cd openshift-python-wrapper
python setup.py install --user
client = DynamicClient(client=kubernetes.config.new_client_from_config())
The examples given below are relevant to all resources. For simplicity we will use the resource - Namespace.
Import Namespace:
from resources.namespace import Namespace
Create a Namespace:
ns = Namespace(name="namespace-example-1")
ns.create()
Will return True
if creation succeeded.
We can also use the with
statement which ensures automatic clean-up of the code executed:
with Namespace(name="namespace-example-2") as ns:
yield ns
teardown=False
- Disables clean-up after execution.
Wait for Namespace to be in status Active
:
ns.wait_for_status(status=Namespace.Status.ACTIVE, timeout=120)
Will raise a TimeoutExpiredError
if Namespace is not in the desired status.
Delete the Namespace
ns.delete()
Will return False
if not found.
Checks if Namespace exists on the server:
ns.exists
Will return None
if not found.
Query to get Pods (resource) in the connected cluster with label of label_example=example
. Returns a generator
of the resource - pod
for pod in Pod.get(dyn_client=client, label_selector="label_example=example")):
pod.log()
We can also get the name of the Node that the pod
is running on:
pod.node.name
Start:
with VirtualMachine(
name="vm-example",
namespace="namespace-example",
node_selector="worker-node-example",
) as vm:
vm.start()
Stop:
vm.stop()
Restart:
vm.restart()
Get VMI:
test_vmi = vm.vmi
After having a VMI, we can wait until VMI is in running state:
test_vmi.wait_until_running()
Will raise TimeoutExpiredError
if VMI failed to run.
Then, we can get the Pod that is in Running state and execute a command on it:
command_output = test_vmi.virt_launcher_pod.execute(command="command-example")
If no Pod was found, will raise ResourceNotFoundError
.
We use pre-commit for code check.
pre-commit install