Skip to main content
OpenShift Python Wrapper provides tools to easily add support for new Kubernetes and OpenShift resources. You can use the automated class generator or add resources manually. The class generator automatically creates resource classes from Kubernetes API schemas.

Installation

The class generator is included when you install openshift-python-wrapper:
Or with pip:

Shell Completion

Add shell completion to your shell configuration (~/.bashrc or ~/.zshrc):

Basic Usage

Generate a class for a specific resource kind:
Generate multiple resources at once:
Preview the generated code without writing files:

File Naming

The generator creates files in ocp_resources/ with snake_case naming:
  • Podpod.py
  • CDIConfigcdi_config.py
  • VirtualMachinevirtual_machine.py
Important: Review the generated filename to ensure it follows Python naming conventions.

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:
This creates two files:
  • 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 are stored in .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:
Force fresh discovery (bypass cache):

Caching

Discovery results are cached for 24 hours in:
This improves performance for repeated discoveries.

Updating Resource Schemas

Resource schemas can be updated from a connected Kubernetes/OpenShift cluster.

Prerequisites

  • Connected to a Kubernetes/OpenShift cluster
  • oc or kubectl
  • Admin access to the cluster

Full Schema Update

Update all resource schemas from your cluster:
This fetches all resource schemas and updates the local cache. When connected to an older cluster, existing schemas are preserved and only missing resources are added.

Single Resource Schema Update

Update the schema for a specific resource:
This is useful when:
  • 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
After updating the schema, regenerate the class:
--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 in ocp_resources/ following these rules:
  1. File naming: Use snake_case (e.g., config_map.py for ConfigMap)
  2. Class naming: Match the resource kind exactly
  3. Inheritance:
    • Cluster-scoped resources: inherit from Resource
    • Namespaced resources: inherit from NamespacedResource

Example: Namespaced Resource

ocp_resources/config_map.py

Example: Cluster-scoped Resource

ocp_resources/backup.py

Required Components

  1. API Group: Define under Resource.ApiGroup or NamespacedResource.ApiGroup
  2. API Reference: Include a link to the official API documentation
  3. __init__ method: Define required and optional parameters
  4. to_dict method: 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 None defaults

Adding Tests

After creating a resource, add tests using the test generator:
Or use the standalone test generator:
See Testing for more details.

Verification

After adding a resource:
  1. Run pre-commit checks:
  2. Run tests:
  3. Test in your environment:

Getting Help

For help with the class generator:
For questions or issues, open an issue on GitHub.