{"id":6020,"library":"openshift","title":"OpenShift Python Client","description":"The `openshift` library, corresponding to `openshift-restclient-python` on GitHub, provides a Python client for interacting with the OpenShift and Kubernetes APIs. It leverages a dynamic client to generically interact with all resources on the server, including Custom Resource Definitions and those defined by aggregated API servers. Currently at version 0.13.2, the library maintains an active release cadence with several updates per year.","status":"active","version":"0.13.2","language":"en","source_language":"en","source_url":"https://github.com/openshift/openshift-restclient-python","tags":["openshift","kubernetes","client","automation","rest-api"],"install":[{"cmd":"pip install openshift","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"The OpenShift client depends on the Kubernetes Python client, which is automatically installed as part of the setup process.","package":"kubernetes"}],"imports":[{"note":"This is the modern, recommended approach for generic interaction with OpenShift and Kubernetes resources.","symbol":"DynamicClient","correct":"from openshift.dynamic import DynamicClient"},{"note":"Used for loading Kubernetes/OpenShift configuration, such as from kubeconfig files or in-cluster settings.","symbol":"config","correct":"from kubernetes import config"},{"note":"Needed for instantiating the core Kubernetes API client which the DynamicClient wraps.","symbol":"client","correct":"from kubernetes import client"}],"quickstart":{"code":"import os\nfrom kubernetes import config\nfrom openshift.dynamic import DynamicClient\n\ntry:\n    # Attempt to load kubeconfig from default locations or KUBECONFIG env var\n    kube_config_path = os.environ.get('KUBECONFIG', os.path.expanduser('~/.kube/config'))\n    k8s_client = config.new_client_from_config(config_file=kube_config_path)\nexcept Exception as e:\n    print(f\"Could not load kubeconfig: {e}. Attempting in-cluster config...\")\n    try:\n        # Fallback to in-cluster configuration if outside kubeconfig fails\n        k8s_client = config.load_incluster_config()\n    except config.ConfigException as e_incluster:\n        print(f\"Could not load in-cluster config: {e_incluster}. Please ensure KUBECONFIG is set or run inside a cluster.\")\n        exit(1)\n\n# Instantiate the DynamicClient with the Kubernetes client\ndyn_client = DynamicClient(k8s_client)\n\n# Example: List all Projects\n# In OpenShift, 'Project' is a custom resource that typically corresponds to a Kubernetes Namespace.\n# You might need 'Project' (OpenShift API) or 'Namespace' (Kubernetes API).\n# This example tries for OpenShift's Project if available, otherwise Kubernetes Namespace.\n\ntry:\n    # Try to get the 'Project' resource (OpenShift specific)\n    projects_resource = dyn_client.resources.get(api_version='project.openshift.io/v1', kind='Project')\n    print(\"Listing OpenShift Projects:\")\n    for project in projects_resource.get().items:\n        print(f\"  - {project.metadata.name}\")\nexcept Exception as e:\n    print(f\"Could not list OpenShift Projects (resource 'project.openshift.io/v1, Project' might not exist or be accessible): {e}\")\n    try:\n        # Fallback to listing Kubernetes Namespaces\n        namespaces_resource = dyn_client.resources.get(api_version='v1', kind='Namespace')\n        print(\"Listing Kubernetes Namespaces instead:\")\n        for namespace in namespaces_resource.get().items:\n            print(f\"  - {namespace.metadata.name}\")\n    except Exception as e_ns:\n        print(f\"Could not list Kubernetes Namespaces either: {e_ns}\")\n","lang":"python","description":"This quickstart demonstrates how to connect to an OpenShift or Kubernetes cluster using `kubeconfig` or in-cluster configuration and then use the `openshift.dynamic.DynamicClient` to list projects (or namespaces as a fallback). It's crucial to have a valid `kubeconfig` file (e.g., at `~/.kube/config`) or appropriate in-cluster permissions for the client to authenticate successfully."},"warnings":[{"fix":"Migrate to using `openshift.dynamic.DynamicClient` for all resource interactions.","message":"Older versions of the library used a deprecated client approach with swagger-generated models. The current recommended approach, reflected in recent versions, is to use the dynamic client for generic interaction. Users upgrading from very old versions should refactor.","severity":"breaking","affected_versions":"<= 0.10.x (pre-dynamic client focus)"},{"fix":"Utilize service account tokens or `kubeconfig` files (`~/.kube/config` or `KUBECONFIG` environment variable) for authentication. Avoid direct username/password in code where possible.","message":"Authentication with username/password has historically had issues and is generally less reliable than token-based authentication. It is highly recommended to use token-based authentication, typically through a service account token or by ensuring a valid kubeconfig file is present.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are using `openshift==0.13.1` or a later version.","message":"Version 0.13.0 was released with broken requirements, leading to installation issues. This was fixed in version 0.13.1. [changelog]","severity":"breaking","affected_versions":"0.13.0"},{"fix":"Regularly test your application against new `openshift` and `kubernetes` client versions. Consider pinning the `kubernetes` dependency in your `requirements.txt` to a specific major/minor version that is known to be compatible with your `openshift` version.","message":"Version 0.13.0 removed the upper limit on the `kubernetes` dependency. While this keeps up with Kubernetes API updates, it means that a very new `kubernetes` client library with potential breaking changes could be pulled in, causing unexpected compatibility issues with `openshift`. [changelog]","severity":"gotcha","affected_versions":"0.13.0 and later"},{"fix":"Be aware that APIs might evolve, and review changelogs carefully when upgrading. Pin to specific versions to manage stability.","message":"The PyPI project is currently classified as 'Development Status :: 3 - Alpha'. While actively maintained, this status might imply potential for API instability or significant changes in future releases.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}