Talk To Your Cluster With This Open Source Python API Wrapper

Image credits: Unsplash - Ante Hamersmit

Combine the power of an open API and the Python programming language.

Open source projects that create a wrapper around an API are becoming increasingly popular. These projects make it easier for developers to interact with APIs and use them in their applications. The openshift-python-wrapper project is a wrapper around openshift-restclient-python. What began as an internal package to help our team work with the OpenShift API became an open source project (Apache License 2.0).

This article discusses what an API wrapper is, why it’s useful, and some examples from the wrapper.

Why use an API wrapper?

An API wrapper is a layer of code that sits between an application and an API. It simplifies the API access process by abstracting away some of the complexities involved in making requests and parsing responses. A wrapper can also provide additional functionality beyond what the API itself offers, such as caching or error handling.

Using an API wrapper makes the code more modular and easier to maintain. Instead of writing custom code for every API, you can use a wrapper that provides a consistent interface for interacting with APIs. It saves time, avoids code duplications, and reduces the chance of errors.

Another benefit of using an API wrapper is that it can shield your code from changes to the API. If an API changes its interface, you can update the wrapper code without modifying your application code. This can reduce the work required to maintain your application over time.


The application is on PyPi, so install openshift-python-wrapper using the pip command:

$ python3 -m pip install openshift-python-wrapper

Python wrapper

The OpenShift REST API provides programmatic access to many of the features of the OpenShift platform. The wrapper offers a simple and intuitive interface for interacting with the API using the openshift-restclient-python library. It standardizes how to work with cluster resources and offers unified resource CRUD (Create, Read, Update, and Delete) flows. It also provides additional capabilities, such as resource-specific functionality that otherwise needs to be implemented by users. The wrapper makes code easier to read and maintain over time.

One example of simplified usage is interacting with a container. Running a command inside a container requires using Kubernetes stream, handling errors, and more. The wrapper handles it all and provides simple and intuitive functionality.

>>> from ocp_resources.pod import Pod
>>> from ocp_utilities.infra import get_client
>>> client = get_client()

ocp_utilities.infra INFO Trying to get
client via new_client_from_config

>>> pod = Pod(client=client, name="nginx-deployment-7fb96c846b-b48mv", namespace="default")
>>> pod.execute("ls")

ocp_resources Pod INFO Execute ls on
nginx-deployment-7fb96c846b-b48mv (ip-10-0-155-108.ec2.internal)


Developers or testers can use this wrapper—our team wrote the code while keeping testing in mind. Using Python capabilities, context managers can provide out-of-the-box resource creation and deletion, and inheritance can be used to extend functionality for specific use cases. Pytest fixtures can utilize the code for setup and teardown, leaving no leftovers. Resources can even be saved for debugging. Resource manifests and logs can be easily collected.

Here’s an example of a context manager:

def namespace():
    admin_client = get_client()
    with Namespace(client=admin_client, name="test-ns",) as ns:
        ns.wait_for_status(status=Namespace.Status.ACTIVE, timeout=240)
        yield ns

def test_ns(namespace):

Generators iterate over resources, as seen below:

>>> from ocp_resources.node import Node
>>> from ocp_utilities.infra import get_client
>>> admin_client = get_client()
# This returns a generator
>>> for node in Node.get(dyn_client=admin_client): 


Open source code for open source communities

To paraphrase a popular saying, “If you love your code, set it free.” The openshift-python-wrapper project started as utility modules for OpenShift Virtualization. As more and more projects benefitted from the code, we decided to extract those utilities into a separate repository and have it open sourced. To paraphrase another common saying, “If the code does not return to you, it means it was never yours.” We like saying that once that happens, it’s truly open source.

More contributors and maintainers mean that the code belongs to the community. Everyone is welcome to contribute.

By: Ruth Netser & Meni Yakove
Originally published at Open Source

Source: Cyberpogo

For enquiries, product placements, sponsorships, and collaborations, connect with us at We'd love to hear from you!