OpenShift Client for Python

2.0.5 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `OC` client, switch to a specific OpenShift project (namespace), and list pods within that project. It assumes a `kubeconfig` file is available in the default location or specified via `KUBECONFIG_PATH` environment variable. It also shows how to retrieve the current project name and handle potential errors.

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}")

view raw JSON →