OpenShift Client for Python
The `openshift-client` library provides a Pythonic interface for interacting with OpenShift clusters, built on top of the official Kubernetes Python client. It simplifies common OpenShift operations like managing projects, applications, and resources. The current version is 2.0.5, and it has an active development cadence with parallel maintenance of 1.x and 2.x branches, though 2.x is the primary focus for new features.
Common errors
-
ModuleNotFoundError: No module named 'openshift'
cause The package `openshift-client` is installed, but the top-level import for the main client module is `openshift.client`, not just `openshift`.fixChange your import statement from `import openshift` or `from openshift import OC` to `from openshift.client import OC`. -
kubernetes.client.rest.ApiException: (404) Not Found
cause This typically means the requested resource (e.g., pod, deployment) does not exist in the specified project (namespace), or the project itself does not exist or is inaccessible.fixVerify the resource name and kind are correct. Ensure the `OC().project()` method has been called with the correct project name, or that the default context points to the desired project. -
TypeError: can't compare offset-naive and offset-aware datetimes
cause You are attempting to compare a timezone-naive datetime object in your code with a timezone-aware datetime object returned by the `openshift-client` library (which became timezone-aware in recent versions).fixEnsure all datetime objects involved in comparisons or operations are consistently timezone-aware. Use `datetime.now(timezone.utc)` or `datetime.fromtimestamp(ts, tz=timezone.utc)` to create timezone-aware datetimes.
Warnings
- breaking Python 2 support has been officially deprecated starting with versions 2.0.5 and 1.0.24. Future releases will drop Python 2 compatibility entirely.
- gotcha Starting with versions 2.0.2 and 1.0.22, the library internally uses timezone-aware datetime objects instead of timezone-naive `datetime.utcnow()`. This can lead to `TypeError: can't compare offset-naive and offset-aware datetimes` if your application code mixes naive and aware datetimes when interacting with API objects or their properties.
- gotcha The `OC` client defaults to using the currently active context in your `kubeconfig` file. If you need to interact with a specific context or a different `kubeconfig` file, you must explicitly specify it.
Install
-
pip install openshift-client
Imports
- OC
import openshift
from openshift.client import OC
Quickstart
import os
from openshift.client import OC
# Instantiate the OC client. It automatically uses your default kubeconfig context.
# Or specify a kubeconfig path:
# oc_client = OC(kubeconfig=os.environ.get('KUBECONFIG_PATH', '~/.kube/config'))
oc_client = OC()
# Change to a specific project (namespace)
project_name = os.environ.get('OPENSHIFT_PROJECT', 'default')
oc_client.project(project_name)
print(f"Current project: {oc_client.get_project_name()}")
# List all pods in the current project
try:
pods = oc_client.selector('pods').objects()
if pods:
print(f"Found {len(pods)} pods in project '{project_name}':")
for pod in pods:
print(f" - {pod.metadata.name} (Status: {pod.status.phase})")
else:
print(f"No pods found in project '{project_name}'.")
except Exception as e:
print(f"Error listing pods: {e}")
# Example: Get a specific resource (if it exists)
# try:
# deployment = oc_client.get('deployment', 'my-app-deployment')
# print(f"Found deployment: {deployment.metadata.name}")
# except Exception as e:
# print(f"Deployment not found or error: {e}")