Using the Class Generator (Recommended)
The class generator automatically creates resource classes from Kubernetes API schemas.Installation
The class generator is included when you install openshift-python-wrapper:Shell Completion
Add shell completion to your shell configuration (~/.bashrc or ~/.zshrc):Basic Usage
Generate a class for a specific resource kind:File Naming
The generator creates files inocp_resources/ with snake_case naming:
Pod→pod.pyCDIConfig→cdi_config.pyVirtualMachine→virtual_machine.py
Handling Duplicate Kinds
Some resources share the same kind but have different API groups. The generator handles this by including the API group in the filename:dns_config_openshift_io.py(from config.openshift.io)dns_operator_openshift_io.py(from operator.openshift.io)
Overwriting Existing Resources
When updating or regenerating resources, use the--overwrite flag with --backup to safely preserve the original:
.backups/backup-YYYYMMDD-HHMMSS/ with the original directory structure preserved.
Batch Regeneration
Regenerate all resources with backups:Discovering Missing Resources
The class generator can automatically discover resources in your cluster that don’t have wrapper classes yet.Basic Discovery
Generate a coverage report showing missing resources:Example Output
Priority Levels
- CORE: Essential Kubernetes resources (v1 API group)
- HIGH: Common workload resources (apps/v1, batch/v1)
- MEDIUM: Platform-specific resources (OpenShift, operators)
- LOW: Custom resources and less common APIs
Discovery Options
JSON output for CI/CD integration:Caching
Discovery results are cached for 24 hours in:Updating Resource Schemas
Resource schemas can be updated from a connected Kubernetes/OpenShift cluster.Prerequisites
Full Schema Update
Update all resource schemas from your cluster:Single Resource Schema Update
Update the schema for a specific resource:- Connected to an older cluster but need a specific CRD
- A new operator was installed
- You want to refresh one resource without a full update
--update-schema and --update-schema-for are mutually exclusive. Use one or the other, not both.Manual Resource Creation
If you need more control or the generator doesn’t fit your needs, you can create resources manually.Resource File Structure
Create a new file inocp_resources/ following these rules:
- File naming: Use snake_case (e.g.,
config_map.pyfor ConfigMap) - Class naming: Match the resource kind exactly
- Inheritance:
- Cluster-scoped resources: inherit from
Resource - Namespaced resources: inherit from
NamespacedResource
- Cluster-scoped resources: inherit from
Example: Namespaced Resource
ocp_resources/config_map.py
Example: Cluster-scoped Resource
ocp_resources/backup.py
Required Components
- API Group: Define under
Resource.ApiGrouporNamespacedResource.ApiGroup - API Reference: Include a link to the official API documentation
__init__method: Define required and optional parametersto_dictmethod: Convert the resource to a dictionary for API calls
Best Practices
- Add type hints to all methods and parameters
- Include docstrings explaining the resource purpose
- Link to official API documentation
- Only include required and commonly used parameters in
__init__ - Handle optional parameters with
Nonedefaults
Adding Tests
After creating a resource, add tests using the test generator:Verification
After adding a resource:-
Run pre-commit checks:
-
Run tests:
-
Test in your environment: