Skip to main content

Overview

The Resource class is the base class for all API resources in the OpenShift Python Wrapper. It provides common functionality for CRUD operations, resource management, schema validation, and interaction with Kubernetes/OpenShift clusters. All resource classes inherit from this base class, either directly or through NamespacedResource.

Class Definition

Constructor

DynamicClient | None
required
Dynamic client for connecting to a remote cluster. Will become mandatory in the next major release.
str | None
Resource name
bool
default:"True"
Indicates if this resource should be deleted on cleanup
str | None
Path to YAML file for the resource
int
default:"TIMEOUT_4MINUTES"
Timeout in seconds for delete operations
bool
default:"False"
If True, perform a dry run without creating the resource
dict[str, Any] | None
Node selector for pod scheduling
dict[str, str] | None
Node selector labels for pod scheduling
str | None
Path to kubeconfig file for connecting to remote cluster (deprecated, use client instead)
dict[str, Any] | None
Dictionary with kubeconfig configuration
str | None
Context name for connecting to remote cluster (deprecated, use client instead)
dict[str, str] | None
Resource labels to add to metadata
dict[str, str] | None
Resource annotations to add to metadata
str
default:"''"
Resource API group; will overwrite API group definition in resource class
bool
default:"True"
Hash resource content based on resource keys_to_hash property (e.g., Secret data)
bool
default:"False"
Whether to check if the resource exists when initializing, raise if not
dict[Any, Any] | None
Dictionary representing the resource object
bool
default:"False"
Waits for the resource to be created when using deploy()
bool
default:"False"
Enable automatic schema validation for this instance. Set to True to validate on create/update operations.

Class Attributes

str
API group for the resource (e.g., “apps”, “batch”)
str
API version (e.g., “v1”, “v1beta1”)
str
Singular resource name for API calls
int
default:"TIMEOUT_1MINUTE"
Default timeout for API operations

Methods

create

Create the resource in the cluster.
bool
default:"False"
True to wait for resource status after creation
dict[type[Exception], list[str]]
Dictionary of exceptions to retry on
Returns: Created resource instance or None if create failed

delete

Delete the resource from the cluster.
bool
default:"False"
Wait for resource deletion to complete
int
default:"TIMEOUT_4MINUTES"
Timeout in seconds to wait for resource deletion
dict[str, Any] | None
Optional request body for deletion
Returns: True if resource was deleted, False otherwise

update

Update the resource with a resource dictionary (partial update/patch).
dict[str, Any]
required
Resource dictionary with fields to update

update_replace

Replace the resource metadata completely. Use this to remove existing fields.
dict[str, Any]
required
Complete resource dictionary to replace with

deploy

Deploy the resource using context manager pattern.
bool
default:"False"
Wait for resource to be ready after deployment
Returns: Self for method chaining

clean_up

Clean up (delete) the resource.
bool
default:"True"
Wait for resource deletion
int | None
Timeout in seconds, defaults to delete_timeout
Returns: True if resource was deleted, False otherwise

wait

Wait for the resource to exist.
int
default:"TIMEOUT_4MINUTES"
Time to wait for the resource
int
default:"1"
Time to wait between retries
Raises: TimeoutExpiredError if resource does not exist within timeout

wait_deleted

Wait until the resource is deleted.
int
default:"TIMEOUT_4MINUTES"
Time to wait for the resource
Returns: True if deleted, False if timeout expired

wait_for_status

Wait for the resource to reach a specific status.
str
required
Expected status (e.g., “Running”, “Succeeded”)
int
default:"TIMEOUT_4MINUTES"
Time to wait for the resource
str | None
Status which should stop the wait and fail (defaults to “Failed”)
int
default:"1"
Time between status checks

wait_for_condition

Wait for a resource condition to reach the desired status.
str
required
Condition type to query (e.g., “Ready”, “Available”)
str
required
Expected condition status (e.g., “True”, “False”)
int
default:"300"
Time to wait in seconds
int
default:"1"
Interval between each retry
str | None
Expected condition reason
str
default:"''"
Expected text in condition message
str | None
Condition which should stop the wait and fail
str
default:"'True'"
Status of the stop condition

validate

Validate the resource against its OpenAPI schema.
Raises: ValidationError if the resource is invalid according to the schema

get (class method)

Get resources from the cluster.
DynamicClient | None
Kubernetes client
str
default:"''"
Resource kind in lowercase for disambiguation
bool
default:"False"
If True, return raw ResourceInstance objects
Returns: Generator of resource objects

Properties

exists

Returns the resource instance if it exists on the server, None otherwise.

instance

Get the current resource instance from the cluster.

status

Get resource status phase (e.g., “Running”, “Pending”, “Failed”).

labels

Get resource labels.

api

Get the API resource instance for making API calls.

Context Manager Usage

The Resource class can be used as a context manager for automatic cleanup:

Examples

Basic Resource Creation

Using YAML File

Waiting for Conditions

Schema Validation

Listing Resources

See Also