Understanding the base Resource class and resource hierarchy
The Resource class is the foundation of the OpenShift Python Wrapper. It provides common functionality for all Kubernetes and OpenShift resources, including CRUD operations, resource management, and schema validation.
All resource classes in the wrapper follow a clear inheritance hierarchy:
Resource # Base class for all resources├── NamespacedResource # Resources that exist within a namespace│ ├── Pod│ ├── Deployment│ ├── Service│ └── ConfigMap└── Cluster Resources # Cluster-wide resources ├── Namespace ├── ClusterRole └── Node
from ocp_resources.pod import Pod# Get specific podfor pod in Pod.get( client=client, namespace="default", name="my-pod"): print(f"Found pod: {pod.name}")
# Get all pods in namespacefor pod in Pod.get(client=client, namespace="default"): print(f"Pod: {pod.name}")# Get pods with label selectorfor pod in Pod.get( client=client, label_selector="app=nginx"): print(f"Pod: {pod.name} on node {pod.node.name}")# Get pods with field selectorfor pod in Pod.get( client=client, namespace="default", field_selector="status.phase=Running"): print(f"Running pod: {pod.name}")
from ocp_resources.resource import Resource# Iterate through all resource typesfor resource in Resource.get_all_cluster_resources( client=client, label_selector="app=myapp"): print(f"Resource: {resource.kind}/{resource.metadata.name}")
from ocp_resources.namespace import Namespacens = Namespace(client=client, name="test-ns")ns.deploy()# Wait for specific statusns.wait_for_status( status=Namespace.Status.ACTIVE, timeout=120)
from ocp_resources.deployment import Deploymentdeployment = Deployment(client=client, namespace="default", name="my-app")# Get all events for this resourcefor event in deployment.events(timeout=60): print(f"Event: {event.reason} - {event.message}")# Filter events by typefor event in deployment.events( field_selector="type=Warning", timeout=30): print(f"Warning: {event.message}")
from ocp_resources.deployment import Deploymentfrom ocp_resources.namespace import Namespacefrom ocp_resources.pod import Pod# Core API group (empty string)print(Pod.api_group) # ""print(Pod.api_version) # "v1"# Apps API groupprint(Deployment.api_group) # "apps"print(Deployment.api_version) # "v1"# Resources with no group (core v1)print(Namespace.api_version) # "v1"
Common API groups available through Resource.ApiGroup:
from ocp_resources.resource import Resource# Access predefined API groupsprint(Resource.ApiGroup.APPS) # "apps"print(Resource.ApiGroup.BATCH) # "batch"print(Resource.ApiGroup.NETWORKING_K8S_IO) # "networking.k8s.io"print(Resource.ApiGroup.RBAC_AUTHORIZATION_K8S_IO) # "rbac.authorization.k8s.io"
The Resource class inherits from ResourceConstants, providing common constants:
from ocp_resources.pod import Pod# Status constantsprint(Pod.Status.RUNNING)print(Pod.Status.PENDING)print(Pod.Status.FAILED)# Condition constantsprint(Pod.Condition.READY)print(Pod.Condition.INITIALIZED)print(Pod.Condition.Status.TRUE)print(Pod.Condition.Status.FALSE)