Skip to content
Wsh7AshPublic

About

Offline Kubernetes LimitRange and ResourceQuota admission simulator

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

admitlab

Simulate Kubernetes LimitRange defaulting and ResourceQuota accounting on a snapshot — and see which pods would be rejected, with the arithmetic, before you apply anything.

$ admitlab simulate fixtures/team.json
batch-1   deny     cpu 1.8/2, memory 2Gi/2Gi
          quota-exceeded: requests.cpu: hard 4, used 2.4, needs 1.8, remaining 1.6
overflow  deny     cpu 100m/200m, memory 64Mi/128Mi
          quota-exceeded: pods: hard 4, used 4, needs 1, remaining 0

The problem

Admission is all-or-nothing and only observable by submitting objects. The rules are easy to state and easy to misapply: a LimitRange can fill both limits and requests, a missing request falls back to the limit, cpu is an alias for requests.cpu, the pod count is itself a quota key, and denied pods must not consume quota. kubectl describe quota shows today's usage, not what your manifest set would do. So a new Deployment is applied, the rollout stalls, and the ReplicaFailure says exceeded quota without saying which pod was too large or how much room was left.

admitlab simulates the decision offline:

  1. Load a snapshot — a kubectl get pods,limitranges,resourcequotas -o json export or a plain JSON object.
  2. Apply Container LimitRange defaults, then copy limits into missing requests.
  3. Validate per-container max/min and maxLimitRequestRatio, and Pod-type min/max against pod sums.
  4. Charge each pod against the quota in creationTimestamp order, using exact decimal arithmetic; the first pod that does not fit is denied and not charged.
  5. Report per-pod verdicts with reasons, the defaults that were applied, and the remaining headroom per quota key.

Features

  • Offline and credential-free: no cluster, no API server, no submission.
  • Deterministic admission order (creationTimestamp, then name), so the same snapshot always produces the same verdicts.
  • Exact Decimal quantities: 500m, 1.5, 2Ki, 1Gi, decimal and binary SI suffixes.
  • LimitRange defaulting and validation, including request-from-limit fallback.
  • Quota keys pods, count/pods, requests.cpu, requests.memory, limits.cpu, limits.memory, plus the cpu/memory aliases.
  • Denied pods never change quota state; headroom is computed from admitted pods.
  • Reasons carry the numbers: hard, used, needed, remaining — or the exact bound that failed.
  • Applied defaults are listed per pod, so a sparse pod is not mistaken for a fully specified one.
  • Text and JSON output, --fail-on deny|violation|none.
  • Standard library only.

Quick start

git clone https://github.com/Wsh7Ash/admitlab.git
cd admitlab
PYTHONPATH=src python -m admitlab simulate fixtures/team.json

Observed output (see results/demo.txt):

admitlab - simulate namespace admission before you apply
snapshot: team.json (1 LimitRange item(s), 4 quota key(s), 7 pod(s))

POD ADMISSION (creationTimestamp order)
pod       verdict  effective requests / limits
api-1     admit    cpu 200m/500m, memory 128Mi/256Mi
          default: api-1/api: limits.cpu from LimitRange default
          default: api-1/api: requests.cpu from defaultRequest
          default: api-1/api: limits.memory from LimitRange default
          default: api-1/api: requests.memory from defaultRequest
api-2     admit    cpu 200m/500m, memory 128Mi/256Mi
          ...
worker-1  admit    cpu 2/2, memory 1Gi/1Gi
batch-1   deny     cpu 1.8/2, memory 2Gi/2Gi
          quota-exceeded: requests.cpu: hard 4, used 2.4, needs 1.8, remaining 1.6
too-big   deny     cpu 1/4, memory 1Gi/1Gi
          limit-range-violation: big: limits cpu=4 exceeds per-container max 2
          quota-exceeded: limits.cpu: hard 6, used 3, needs 4, remaining 3
no-mem    admit    cpu 200m/500m, memory 128Mi/256Mi
          default: no-mem/sidecar: limits.cpu from LimitRange default
          default: no-mem/sidecar: limits.memory from LimitRange default
          default: no-mem/sidecar: requests.memory from defaultRequest
overflow  deny     cpu 100m/200m, memory 64Mi/128Mi
          quota-exceeded: pods: hard 4, used 4, needs 1, remaining 0

QUOTA HEADROOM (after admitted pods)
key                      hard       used  remaining
limits.cpu                  6        3.5        2.5
pods                        4          4          0
requests.cpu                4        2.6        1.4
requests.memory           4Gi    1.375Gi    2.625Gi

SUMMARY  4 admitted, 3 denied
GATE     fail-on=deny -> FAIL

The fourth pod (no-mem) is admitted exactly at the pods boundary, and overflow is the first pod that would exceed it. batch-1 is denied on requests.cpu, too-big on the per-container maximum, and neither consumes quota.

Install

Python 3.12+, no runtime dependencies.

python -m venv .venv
source .venv/bin/activate      # Windows: .venv/Scripts/activate
pip install -e .
admitlab simulate team.json

Or without installing:

PYTHONPATH=src python -m admitlab simulate team.json

Usage

admitlab simulate SNAPSHOT [--json] [--fail-on deny|violation|none]
Flag Meaning
SNAPSHOT kubectl List export or plain snapshot JSON
--json machine-readable report
--fail-on deny (default), violation, none

Exit codes: 0 gate passed, 1 gate failed, 2 usage or input error.

Reason codes

Code Meaning
quota-exceeded the pod would push a quota key past hard
limit-range-violation a per-container or pod-sum bound was exceeded
ratio-exceeded limit/request above maxLimitRequestRatio
missing-resource the quota requires a resource the pod does not set
invalid-quantity a container resource value could not be parsed

In CI

- name: Admission gate for the workload set
  run: |
    pip install -e .
    admitlab simulate policy/team-snapshot.json --fail-on deny

Architecture

snapshot.json -> snapshot.py (load, validate, sort, notes)
              -> simulate.py (defaulting, bounds, ratio, quota charging)
              -> render.py (text | JSON) -> cli.py (gate)

See docs/ARCHITECTURE.md for the rule order and invariants.

Testing

pip install -e ".[dev]"
python -m pytest tests -q     # 55 tests
python scripts/demo.py

Limitations

  • One namespace at a time; mixed snapshots are noted and simulated as one.
  • count/<resource> keys, PVC/storage quotas, scope selectors and priority classes are not simulated.
  • Init containers, sidecars and pod overhead are summed like ordinary containers; effective pod requests for init containers are not modelled.
  • LimitRange items of type PersistentVolumeClaim are ignored.
  • Scheduling is not modelled: an admitted pod can still be Pending.
  • Quantity parsing covers the documented suffixes but not every legal spelling.

Roadmap

  • --namespace selection for multi-namespace snapshots.
  • Diff mode: two snapshots, or before/after a policy change.
  • Init-container and pod-overhead semantics.
  • JUnit output for CI dashboards.

License

MIT. See LICENSE.

About

Offline Kubernetes LimitRange and ResourceQuota admission simulator

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages