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
Thedeploy() method creates a resource in the cluster:
Create Method
Alternatively, use thecreate() method:
Using Context Managers
Context managers provide automatic cleanup:Disable Automatic Teardown
Read Operations
Check if Resource Exists
Get Resource Instance
Access the underlying Kubernetes object:Query Resources with Get
Theget() 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
Theclean_up() method deletes a resource:
Wait for Deletion
Wait until a resource is fully deleted: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: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
- Use Context Managers: Ensure automatic cleanup with context managers
- Check Exists: Always check if a resource exists before operations
- Wait for Readiness: Use wait methods after create/update operations
- Handle Errors: Wrap operations in try-except blocks
- Use Label Selectors: Query resources efficiently with labels
- Validate Before Deploy: Enable validation to catch errors early
- Clean Up Resources: Always clean up test resources
- Use Fake Client for Tests: Test without a real cluster using
fake=True