Skip to content

feat(e2b): add dimension-aware API key quota#565

Open
AiRanthem wants to merge 24 commits into
openkruise:masterfrom
AiRanthem:feature/e2b-api-quota-260622
Open

feat(e2b): add dimension-aware API key quota#565
AiRanthem wants to merge 24 commits into
openkruise:masterfrom
AiRanthem:feature/e2b-api-quota-260622

Conversation

@AiRanthem

Copy link
Copy Markdown
Member

Summary

  • add API-key quota modeling for sandbox count plus CPU and memory dimensions
  • enforce quota admission and release in the E2B create/clone flows with Redis-backed live-set tracking
  • add cache health, leader lifecycle, anti-drift reconciliation, storage, config, and documentation updates

Impact

This lets sandbox-manager reject over-quota API key usage before creating new sandbox resources, while keeping the quota implementation scoped to the E2B API and sandbox-manager layers.

Validation

  • GOCACHE=/private/tmp/go-build-cache /Users/sophon/Bin/go test ./pkg/cache ./pkg/sandbox-manager ./pkg/sandbox-manager/infra/sandboxcr ./pkg/servers/e2b ./pkg/servers/e2b/keys ./pkg/servers/e2b/models ./pkg/servers/e2b/quota

@kruise-bot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign furykerry for approval by writing /assign @furykerry in a comment. For more information see:The Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@AiRanthem AiRanthem changed the title [codex] Add dimension-aware API key quota enforcement feat(e2b): add dimension-aware API key quota Jun 23, 2026
@AiRanthem AiRanthem marked this pull request as ready for review June 23, 2026 03:42
@AiRanthem AiRanthem changed the title feat(e2b): add dimension-aware API key quota [WIP] feat(e2b): add dimension-aware API key quota Jun 23, 2026
@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.25118% with 384 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.83%. Comparing base (f67661d) to head (4aa48fe).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
pkg/sandbox-manager/quota/antidrift.go 82.45% 39 Missing and 24 partials ⚠️
pkg/sandbox-manager/leader.go 61.15% 46 Missing and 8 partials ⚠️
pkg/cache/cache.go 46.66% 37 Missing and 3 partials ⚠️
pkg/sandbox-manager/core.go 54.54% 32 Missing and 8 partials ⚠️
pkg/sandbox-manager/quota/redis.go 71.53% 19 Missing and 18 partials ⚠️
...kg/sandbox-manager/infra/sandboxcr/quota_source.go 57.89% 28 Missing and 4 partials ⚠️
pkg/servers/e2b/core.go 37.03% 15 Missing and 2 partials ⚠️
pkg/servers/e2b/keys/mysql.go 79.22% 8 Missing and 8 partials ⚠️
pkg/sandbox-manager/quota/antidrift_events.go 65.85% 7 Missing and 7 partials ⚠️
pkg/sandbox-manager/api.go 80.00% 8 Missing and 4 partials ⚠️
... and 12 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #565      +/-   ##
==========================================
+ Coverage   79.71%   79.83%   +0.12%     
==========================================
  Files         205      225      +20     
  Lines       15287    17518    +2231     
==========================================
+ Hits        12186    13986    +1800     
- Misses       2658     2957     +299     
- Partials      443      575     +132     
Flag Coverage Δ
unittests 79.83% <77.25%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AiRanthem AiRanthem force-pushed the feature/e2b-api-quota-260622 branch 3 times, most recently from 806b23a to 11bdec7 Compare June 29, 2026 03:38
@AiRanthem AiRanthem force-pushed the feature/e2b-api-quota-260622 branch from 0126ba5 to f196f94 Compare June 30, 2026 10:37
@AiRanthem AiRanthem force-pushed the feature/e2b-api-quota-260622 branch from 1688630 to 859dce8 Compare July 1, 2026 09:50
@AiRanthem AiRanthem changed the title [WIP] feat(e2b): add dimension-aware API key quota feat(e2b): add dimension-aware API key quota Jul 1, 2026
AiRanthem and others added 8 commits July 2, 2026 10:32
Add the final Redis-backed quota architecture for E2B API keys with sandbox-manager-owned admission, accounting, anti-drift reconciliation, breaker handling, and metrics.

Wire quota subject resolution through the E2B key stores and update sandbox-manager configuration, manifests, docs, unit tests, and e2e coverage for the final design.

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
… flag and runID mechanism

The running flag guarded against concurrent Run calls that never happen
(Run is called exactly once). The runID mechanism defended against stale
OnStartedLeading callbacks, but it is redundant: K8s LeaderElector passes
each OnStartedLeading an iteration-scoped context that is canceled when
that iteration's Run returns, and the next iteration cannot start until
the current Run returns. Any stale callback therefore sees a canceled
context and is already rejected by the ctx.Err() check in startLeading.

Removed:
- running field and guard logic
- runID, nextRunID fields
- primaryRunIDContextKey type
- runID context value injection in Run
- runID extraction and check in startLeading
- runID reset in stopLeading and Stop

Kept:
- Retry loop (LeaderElector.Run returns on leadership loss)
- stopped flag (parent ctx is canceled after Stop)
- done channel (graceful shutdown wait)
- ctx.Err() check in startLeading (guards against Stop race)

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
AiRanthem added 12 commits July 2, 2026 10:32
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
@AiRanthem AiRanthem force-pushed the feature/e2b-api-quota-260622 branch from 22076e0 to a596cfa Compare July 2, 2026 02:32
AiRanthem and others added 4 commits July 2, 2026 17:26
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
The no-redis quota job flakes when 12 code-interpreter pods contend on a
single kind worker and readiness exceeds 180s. Use 300s to match other
pool waits in the same test file.

Signed-off-by: AiRanthem <zhongtianyun.zty@alibaba-inc.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants