Skip to main content

Overview

All resources in the OpenShift Python Wrapper follow a unified CRUD (Create, Read, Update, Delete) pattern. This guide demonstrates common operations that apply to all resource types.

Getting a Client

All resource operations require a client connection:
The get_client() function automatically handles authentication and cluster connection. For testing, use fake=True to create resources without a real cluster.

Create Operations

Deploy a Resource

The deploy() method creates a resource in the cluster:

Create Method

Alternatively, use the create() method:

Using Context Managers

Context managers provide automatic cleanup:
Always prefer context managers when possible. They ensure resources are cleaned up even if exceptions occur.

Disable Automatic Teardown

Read Operations

Check if Resource Exists

Get Resource Instance

Access the underlying Kubernetes object:

Query Resources with Get

The get() class method returns a generator of resources:

Get Single Resource

Get a specific resource by name:

Update Operations

Update Resource

Modify and update a resource:

Patch Resource

Apply partial updates:

Update Labels

Delete Operations

Clean Up Resource

The clean_up() method deletes a resource:

Wait for Deletion

Wait until a resource is fully deleted:
Some resources have finalizers that prevent immediate deletion. Always use wait_deleted() to ensure complete removal.

Delete with Grace Period

Wait Operations

Wait for Status

Wait for a resource to reach a specific status:

Wait for Condition

Wait for specific conditions:

Custom Wait Conditions

Working with Multiple Resources

ResourceList

Create multiple similar resources:

NamespacedResourceList

Create resources across multiple namespaces:
Use ResourceList and NamespacedResourceList when you need to create multiple similar resources efficiently.

Working with YAML Files

Create from YAML

Create from Dictionary

Validation

Schema Validation

Validate resources before deployment:

Auto-validation on Create

Error Handling

Common Exceptions

Retry on Errors

Best Practices

  1. Use Context Managers: Ensure automatic cleanup with context managers
  2. Check Exists: Always check if a resource exists before operations
  3. Wait for Readiness: Use wait methods after create/update operations
  4. Handle Errors: Wrap operations in try-except blocks
  5. Use Label Selectors: Query resources efficiently with labels
  6. Validate Before Deploy: Enable validation to catch errors early
  7. Clean Up Resources: Always clean up test resources
  8. Use Fake Client for Tests: Test without a real cluster using fake=True