Skip to content

#256, #258: quotaBelowRequested reads the pods' live use; the team label is configurable (teamKey) - #771

Merged
ephico2real2 merged 7 commits into
mainfrom
fix/256-258-approval-facts
Dec 1, 2026
Merged

ephico2real2 merged 7 commits into
mainfrom
fix/256-258-approval-facts

Conversation

@ephico2real2

Copy link
Copy Markdown
Owner

Closes #256
Closes #258

Two Medium defects in the approval facts. Both were found by the Sonnet 5.5 documentation pass of PR #254 and stated as gaps in docs/18.

#256: quotaBelowRequested was always false

What was wrong. TierCatalog.compare takes used and sets quotaBelowRequested when a new hard value is below it. Both callers, Pipeline._plan_namespace and Pipeline.restore_facts (the revert), called it without used. So the fact was always false and the shipped rule when: quotaBelowRequested (approval-rules.yaml) never fired. docs/01, "Quota changes on existing namespaces", says: "A decrease below what pods already request is refused unless approver-platform approve, because new pods would be rejected at admission."

The fix.

  • Pipeline.quota_used reads what the pods request now. This is the status.used of the live ResourceQuota that Git's quota is, read from the cluster's watch cache. The capacity check reads the same cache. A short name (cpu, memory) stands in for its long one, as in capacity_ledger. Both callers pass the result to compare.
  • When the use cannot be known, compare counts a key that the change lowers as below (fail closed, QuotaChange.unknown_lowered). The use cannot be known when there is no quota in Git or no watch cache. It also cannot be known when the Namespace or ResourceQuota watch has not listed, is stale or is refused (401 or 403, the same test as capacity_backend._read_cached_quotas), or when the live namespace has no such quota. A key that the change does not lower is never counted for want of a read. A namespace that a fresh watch does not hold has no pods, so it requests nothing. The planner logs quota_used_unknown with the reason.

Why fail closed. Each source is quoted, with its URL and the date it was read:

  • Kubernetes, Resource Quotas (https://kubernetes.io/docs/concepts/policy/resource-quotas/, read 2026-12-01): "Neither contention nor changes to quota will affect already created resources." It also says: "If creating or updating a resource violates a quota constraint, the control plane rejects that request with HTTP status code 403 Forbidden." So a quota lowered below the use keeps the pods that are running and refuses new ones.
  • The ResourceQuota API reference (https://kubernetes.io/docs/reference/kubernetes-api/policy-resources/resource-quota-v1/, read 2026-12-01) defines status.used as: "Used is the current observed total usage of the resource in the namespace."
  • OWASP, Fail securely (https://community.owasp.org/Fail_securely, read 2026-12-01): checks "should all return false if there is an exception during processing", and "a failure will follow the same execution path as disallowing the operation."
  • Saltzer and Schroeder, The Protection of Information in Computer Systems (https://web.mit.edu/Saltzer/www/publications/protection/Basic.html, read 2026-12-01), on fail-safe defaults: "Base access decisions on permission rather than exclusion."
  • This follows the pipeline's own precedent. When live usage cannot be read, the plan "carries usage: unavailable and the change needs a platform approval instead of failing" (docs/01), and ApprovalRules._added adds approver-platform. A delete's workload count is "unknown is 503, never 0".
  • Option rejected: a 503 for a plan whose use cannot be read. It would block every quota change while the watch is stale. docs/01 chose approval over failure for an unknown live input of a quota change.

#258: cmapi.net/team was hard-coded

What was wrong. The pipeline read the cmapi.net/team label for placement, the fact team, the daily auto-approval allowance per team (auto_approvals_today) and change_requests.team. Placement's team count, the quota, namespace and expiration list views, and the revert job read it too. The owner's company example declares no cmapi.net/team. With it, no namespace had a team, so the allowance never applied.

The fix. metadata-keys.yaml gains the stanza teamKey, read by MetadataKeys.team_key and team_of(). Every reader above now goes through it.

  • When the stanza is left out, the key is cmapi.net/team. A file written before the stanza keeps its behaviour, declared or not.
  • A file that names a teamKey must declare it as a label in requiredLabels or extraLabels.keys, or the loader refuses the file. Otherwise the key would be read on no namespace, and the allowance would silently apply to none.
  • The company example names teamKey: company.net/mnemonic. The reference file and the chart file name cmapi.net/team explicitly.
  • The Helm values.schema.json takes teamKey as optional, as it takes liveOnlyKeys.
  • The API descriptions (the team query parameters, NamespaceView.team, PlacementRequest.team and the placement-mismatch fix text) now name the team label. The contract and the portal types were generated again (CMAPI_WRITE_CONTRACT=1, npm run api:types); scripts/api-examples-doc.py --check passes.
  • A new test holds every shipped metadata-keys file to a declared team label.

Tests: fail before, pass after

Before: the new tests copied onto origin/main 0030e34 (record: workbench/cmapi/impl-256-264/before-256/before-run.txt). After: this branch.

Test Before After
tests/service/test_quota_below_requested_256.py: below the use (plan), unknown use, stale watch, revert 4 FAILED (['dev change: 0 from approver-platform'], no rule; the revert's quotaBelowRequested False) 5 passed
tests/service/test_quota_below_requested_256.py: above the use passed (guards against a false positive) passed
tests/core/test_quota_below_requested_256.py 4 FAILED, 2 passed 6 passed
tests/service/test_team_key_258.py: company file, allowance 1, second create FAILED: assert 'approved' == 'pending_approval' passed
tests/core/test_team_key_258.py ImportError (no DEFAULT_TEAM_KEY) passed

Measured here, locally, against PostgreSQL 16 on 127.0.0.1:55432:

  • the whole core suite (tests/core): 1027 passed, 117 skipped;
  • the service files changed or added, and the two that exercise the plan's quota facts, one at a time: test_quota_below_requested_256.py 5 passed, test_team_key_258.py 1 passed, test_catalog_drift_710.py 8 passed, test_revert_sizing.py 9 passed, test_helm_metadata_values.py 83 passed, test_api_docs.py -k committed_contract 1 passed. (A broader local run was stopped at the coordinator's request because of machine load.)

The full service suite was not run locally. CI is the gate.

Not tested

  • Not run on CRC or kind. The watch cache with a real quota controller's status.used is inferred from the API reference, not measured here.
  • contract/lint.sh (Spectral) was not run locally. Only descriptions changed. CI's api-contract job runs it.
  • The portal's own test suites were not run. Only the generated schema.generated.ts comments changed.

Who wrote what

All commits are by Opus 5.5 at high, the implementer. No reviewer has seen it yet. The review goes to reviewer 1 and reviewer 2, because the change is over 50 lines and touches the approval gate.

…bel is metadata-keys.yaml teamKey

#256: TierCatalog.compare takes the use, and neither caller passed it, so quotaBelowRequested was always false.
Pipeline.quota_used reads the status.used of the live ResourceQuota that Git's quota is, from the watch cache that the
capacity check reads, and the plan and the revert pass it. A key that the change lowers and whose use cannot be read
(no live quota, a watch that has not listed, is stale or refused) counts as below: fail closed, logged
quota_used_unknown. A key that the change does not lower never does.

#258: cmapi.net/team was hard-coded for placement, the fact team, the daily allowance per team, change_requests.team
and the list views. The new stanza teamKey names the label, cmapi.net/team by default; a named key must be a declared
label. The company example names company.net/mnemonic. Contract, portal types, Helm schema, docs/02, docs/18 and the
glossary follow.
…wly added); scoped quota is unknown; default approver when no rule reads quotaBelowRequested; deletes count against the team allowance; company portal places with its team key

Reviewer 1 (Codex gpt-6.2-astra) and reviewer 2 (Fable) findings, plus one coordinator finding fixed by reviewer 2.
Record: docs/reviews/2026-12-01-issue-256-258/.
…ent for that case; docs/01, #710)

Reviewer 2 (Fable) amend2.diff, applied by the coordinator.
…ions relocated, docs/14 pipeline.py:298 corrected by hand
@ephico2real2
ephico2real2 merged commit 049077f into main Dec 1, 2026
12 checks passed
ephico2real2 added a commit that referenced this pull request Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant