A Python automation tool for auditing and enforcing Rancher quotas (Rancher 2.13) for project-level and namespace-level quotas in air-gapped on-prem environments.
Note: This project (code, docs, and structure) was generated by an AI coding assistant, not written by hand by a human developer. Use and modify as needed; feedback and contributions are welcome.
- Project-level quota enforcement: CPU and memory limits/reservations
- Namespace-level quota enforcement: CPU and memory limits/reservations
- Multi-cluster support: Manage quotas across multiple Kubernetes clusters
- Multi-instance support: Support for multiple Rancher instances
- Idempotent operations: Only updates quotas when drift is detected
- Dry-run mode: Preview changes without applying them
- Structured logging: JSON-formatted logs with context
- Air-gapped ready: All dependencies pinned for offline installation
- HTML quota report: Overview of all project and namespace quotas
- Python 3.9 or higher
- Rancher 2.13
- Kubernetes 1.33 (downstream clusters)
- Rancher API token with appropriate permissions
From the repo root:
make install # editable install; use quotactl from source
make build # single Linux binary → dist/quotactl (requires PyInstaller)
make test # run testspip install -e .For air-gapped environments, install from local wheel cache:
# Download dependencies (on internet-connected machine)
pip download -r requirements.txt -d wheels/
# Transfer wheels/ directory to air-gapped machine
# Install from local cache
pip install --no-index --find-links wheels/ -r requirements.txt
pip install -e .Each GitHub Release includes a Linux amd64 binary (built on Ubuntu 22.04; runs on Ubuntu 22.04, 24.04, and other glibc 2.35+ systems) plus a Python wheel (.whl) and source distribution (.tar.gz). To use the binary, download quotactl-<version>-linux-amd64, rename to quotactl, and run:
chmod +x quotactl
./quotactl --helpTo install from the release wheel: download the .whl from the release assets, then pip install quotactl-<version>-py3-none-any.whl. Use the wheel or install from source if your system has glibc older than 2.35 (e.g. Ubuntu 20.04 or older).
To build the binary yourself (requires PyInstaller):
make build
# or: ./scripts/build-release.sh
# Output: dist/quotactlCreate a YAML configuration file for each Rancher instance:
url: https://rancher.example.com
token_ref: RANCHER_TOKEN # Environment variable name
# insecure_skip_tls_verify: true # Set if Rancher uses self-signed cert (or use --insecure / RANCHER_INSECURE_SKIP_VERIFY=1)
clusters:
production:
cluster_id: c-abc123
projects:
my-project:
project_quota:
cpu_limit: "2000m"
memory_limit: "4Gi"
cpu_reservation: "1000m"
memory_reservation: "2Gi"
namespace_quotas:
ns1:
cpu_limit: "1000m"
memory_limit: "2Gi"
ns2:
cpu_limit: "500m"
memory_limit: "1Gi"url: Rancher instance base URLtoken: API token (not recommended - usetoken_refinstead)token_ref: Environment variable name containing the API tokeninsecure_skip_tls_verify: Set totrueto skip TLS verification (e.g. self-signed Rancher). Alternatively use CLI--insecureor envRANCHER_INSECURE_SKIP_VERIFY=1.clusters: Dictionary of cluster configurationscluster_id: Rancher cluster IDprojects: Dictionary of project configurationsproject_quota: Project-level quota specificationnamespace_quotas: Dictionary of namespace quotas (keyed by namespace name)
- CPU values: Use millicores (e.g.,
"2000m") or cores (e.g.,"2") - Memory values: Use Kubernetes memory format (e.g.,
"4Gi","2G","512Mi")
Recommended: Use environment variables for tokens:
export RANCHER_TOKEN="your-token-here"The tool will never log token values (they are masked in logs).
The tool has two main commands: apply (enforce quotas) and report (generate HTML overview).
quotactl apply --config instance.yaml --cluster c-abc123 --project my-project --dry-runquotactl apply --config instance.yaml --cluster c-abc123 --project my-project --applyquotactl apply --config instance.yaml --cluster c-abc123 --project p1 --project p2 --applyquotactl apply --config instance.yaml --cluster c-abc123 --all-projects --applyquotactl apply --config instance.yaml --cluster c-abc123 --cluster c-def456 --all-projects --applyquotactl apply --config instance.yaml --cluster c-abc123 --all-projects --apply --continue-on-errorGenerate a readable HTML report of all project and namespace quotas across clusters:
quotactl report --config instance.yaml --output quota-report.htmlTo limit to specific clusters:
quotactl report --config instance.yaml --output quota-report.html --cluster local --cluster c-abc123The report is self-contained HTML with embedded CSS, suitable for sharing or viewing in a browser.
Apply command:
--config,-c: Path to configuration file (required)--cluster: Cluster ID(s) to process (can be specified multiple times)--project: Project name(s) to process (can be specified multiple times)--all-projects: Process all projects in selected clusters--dry-run: Show planned changes without applying--apply: Apply quota changes (required for write operations)--continue-on-error: Continue processing after errors (default: fail-fast)--log-level: Logging level (DEBUG, INFO, WARNING, ERROR)--token-env-var: Environment variable name containing Rancher API token
Report command:
--config,-c: Path to configuration file (required)--output,-o: Output HTML file path (required)--cluster: Cluster ID(s) to include (default: all clusters)--log-level: Logging level (DEBUG, INFO, WARNING, ERROR)--token-env-var: Environment variable name containing Rancher API token
0: Success1: Fatal error2: Partial failure (when using--continue-on-error)
The tool requires the following Rancher API permissions:
- Read:
projects.get,namespaces.get,clusters.get - Write:
projects.update,namespaces.update
Recommended Rancher roles:
- Project Owner (for project-level quotas)
- Cluster Admin (for cluster-wide operations)
quotactl apply --config rancher-prod.yaml \
--cluster c-abc123 \
--project my-app \
--dry-runOutput:
Execution Plan: 2 change(s) needed out of 2 resource(s)
project 'my-app':
CPU Limit: 2000m → 3000m
Memory Limit: 4Gi → 8Gi
namespace 'my-app-ns':
CPU Limit: 1000m → 2000m
[DRY RUN] No changes applied.
quotactl apply --config rancher-prod.yaml \
--cluster c-abc123 \
--project my-app \
--applyquotactl apply --config rancher-prod.yaml \
--cluster c-abc123 \
--all-projects \
--apply \
--continue-on-errorquotactl report --config rancher-prod.yaml --output quota-overview.htmlpytest tests/unit/Integration tests require a real Rancher instance. Set environment variables:
export RANCHER_URL="https://rancher.example.com"
export RANCHER_TOKEN="your-token"
export CLUSTER_ID="c-abc123"
export PROJECT_NAME_TEST="test-project"
export INTEGRATION_WRITE=1 # Required for write operationsRun integration tests:
pytest tests/integration/ -m integrationNote: Integration tests will:
- Read current quota state
- Apply safe test changes
- Verify results
- Restore original values
The tool uses structured JSON logging. Logs are written to stderr and include:
- Timestamp
- Log level
- Message
- Context: instance, cluster, project, namespace
Example log entry:
{
"timestamp": "2024-01-15T10:30:00Z",
"level": "INFO",
"message": "Processing project: my-app",
"instance": "https://rancher.example.com",
"cluster": "c-abc123",
"project": "my-app"
}rancher-quota/
├── src/
│ └── quotactl/
│ ├── __init__.py
│ ├── cli.py # Command-line interface
│ ├── config.py # Configuration loading
│ ├── diff.py # Diff generation
│ ├── executor.py # Quota enforcement
│ ├── logging.py # Structured logging
│ ├── models.py # Data models
│ ├── planner.py # Execution planning
│ └── rancher_client.py # Rancher API client
├── tests/
│ ├── unit/ # Unit tests
│ └── integration/ # Integration tests
├── DESIGN.md # Design document
├── pyproject.toml # Project metadata
├── requirements.txt # Pinned dependencies
└── README.md # This file
See DESIGN.md for detailed architecture, data models, API usage, failure modes, and testing strategy.
- No Rollback: The tool does not implement rollback functionality. Each quota update is independent.
- No Locking: Concurrent modifications are not prevented (last write wins).
- Project Name Collisions: Project names must be unique within a cluster.
- API Rate Limits: The tool includes retry logic but does not implement rate limiting.
- Verify token is valid and has required permissions
- Check token is set in environment variable (if using
token_ref) - Ensure token has not expired
- Verify project name matches exactly (case-sensitive)
- Ensure project exists in the specified cluster
- Check cluster ID is correct
- Verify API token has write permissions
- Check quota values are in correct format
- Review Rancher API logs for detailed error messages
- Follow the design document for architecture decisions
- Maintain test coverage above 90%
- Use structured logging for all operations
- Document all assumptions and limitations
MIT License