Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .agents/flow/issues/m8-runner-tarball-internal-endpoint.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Serve the runner archive through the internal OSS endpoint to stop per-spot public egress

English | [中文](m8-runner-tarball-internal-endpoint.md)

```yaml flow
kind: issue
milestone: m8
priority: P2
status: in-progress
scope:
- .agents/flow/issues/m8-runner-tarball-internal-endpoint.*
- backend/app/api/runners.py
- tests/backend/**
- plans/2026-12-04-pr490-review.md
adr: []
github:
number: 489
url: https://github.com/Shadow-Azure/ai-eval-platform/issues/489
```

## Background

The object-storage bill for the first three days of December was about ¥42; roughly 201 GB of standard LRS storage costs only about ¥2.4 for three days, so about ¥40 was public egress. Each of the 367 spot instances created in December downloaded a roughly 228 MB actions-runner archive through the API's `/runner/v1/runner-tarball/<version>.tar.gz`; 367 × 228,071,396 B ≈ 83.7 GB, at ¥0.50/GB ≈ ¥41.9, which matches the bill. The API host and the bucket are in the same region and VPC, yet `runner_tarball()` read the private OSS object through the public endpoint, so every proxied download produced one public egress. The `dataset-images` presign already uses the internal channel.

## Goals

- `runner_tarball()` prefers the internal OSS endpoint for the runner archive; non-Aliyun deployments keep their configured endpoint.
- A regression test proves a complete streaming download through the internal endpoint while the public endpoint is unreachable.
- Spot instances keep downloading from the API's VPC callback address and never connect to the OSS internal endpoint directly.

## Acceptance

- With an internal endpoint configured, `/runner/v1/runner-tarball/<version>.tar.gz` reads through the internal endpoint and returns the complete archive.
- The test points the public endpoint at an unreachable address, proving the implementation does not fall back to public egress.
- Backend tests and repository gates pass; after deployment, runner-related public egress is near zero (operator checks the bill).
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 双语配对一致性记录(docs/i18n/README.md):两侧在最后一次确认一致状态下的
# git blob hash。两种语言同等权威;编辑任一侧后带上另一侧,然后重录:
# pnpm run record:i18n -- .agents/flow/issues/m8-runner-tarball-internal-endpoint.md
m8-runner-tarball-internal-endpoint.md: 728c0c5f1c7e6c7a4a6a7e3a8b025289f2af61cd
m8-runner-tarball-internal-endpoint.en.md: 7aca7d1e474543985cc21e6585b69d5c592d0d46
35 changes: 35 additions & 0 deletions .agents/flow/issues/m8-runner-tarball-internal-endpoint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Runner 安装包代理改走内网 OSS,消除 spot 每台的公网流出

[English](m8-runner-tarball-internal-endpoint.en.md) | 中文

```yaml flow
kind: issue
milestone: m8
priority: P2
status: in-progress
scope:
- .agents/flow/issues/m8-runner-tarball-internal-endpoint.*
- backend/app/api/runners.py
- tests/backend/**
- plans/2026-12-04-pr490-review.md
adr: []
github:
number: 489
url: https://github.com/Shadow-Azure/ai-eval-platform/issues/489
```

## 背景

12 月对象存储账单前 3 天约 ¥42;按约 201 GB 标准 LRS 用量,3 天存储费只有约 ¥2.4,差额约 ¥40 是公网流出。12 月创建的 367 台 spot 实例每台经 API 的 `/runner/v1/runner-tarball/<version>.tar.gz` 下载约 228 MB 的 actions-runner 包;367 × 228,071,396 B ≈ 83.7 GB,按 ¥0.50/GB ≈ ¥41.9,与账单吻合。API 主机与 bucket 同地域同 VPC,`runner_tarball()` 却用公网 endpoint 读取私有 OSS,于是每次代理下载都产生一次公网流出。`dataset-images` 的 presign 已经使用内网通道。

## 目标

- `runner_tarball()` 优先从内网 OSS endpoint 读取 runner 安装包;非阿里云部署保持现有 endpoint。
- 回归测试证明公共 endpoint 不可达时仍能经由内网 endpoint 完整流式下载。
- spot 实例继续从 API 的 VPC 回调地址下载,不直连 OSS 内网。

## 验收

- 配置内网 endpoint 后,`/runner/v1/runner-tarball/<version>.tar.gz` 走内网读取并返回完整包。
- 测试把公共 endpoint 指向不可达地址,确保实现没有回退公网。
- 后端测试与仓库门禁通过;部署后 runner 相关公网流出接近 0(运维核对账单)。
6 changes: 5 additions & 1 deletion backend/app/api/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ def runner_tarball(
status_code=404,
detail=error_detail("runner_tarball_not_found", "Runner 安装包不存在"),
)
store = object_store_from_settings(settings)
# Issue #489: spot instances all share the API's region/VPC, so read the
# private object through the internal Aliyun endpoint; the public endpoint
# would bill one egress copy per spot download. Non-Aliyun deployments keep
# their configured endpoint because the internal one cannot be derived.
store = object_store_from_settings(settings, internal=True)
if store is None:
raise HTTPException(
status_code=503,
Expand Down
168 changes: 168 additions & 0 deletions plans/2026-12-04-pr490-review.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# PR #490 检视:runner-tarball 代理改走内网 OSS

## 元信息

- 检视对象:PR #490 `fix(m8): Runner 安装包代理改走内网 OSS endpoint,消除 spot 公网流出 (#489)`,`agent/issue-489-dev-18bed0bf5aac` → `main`
- 对应 issue:#489(12 月 OSS 公网流出约 ¥40,367 台 spot 每台经 API 代理重复下载约 228 MB runner 包)
- owning 流程条目:`.agents/flow/issues/m8-runner-tarball-internal-endpoint.md`(status: in-progress)
- 实现计划:无独立 plans 文档(issue 即计划)
- 检视日期:2026-12-04
- 变更规模:**5 文件 = 新增 3 + 修改 2**,`+119 / -3`(`git diff origin/main...HEAD`,HEAD = `e879861`)。单提交 PR。
- 变更性质:后端 1 个源文件 + 1 个测试 + 流程条目三件套;**无新依赖、无迁移、无前端改动**。
- 本检视纪律:只产出本检视文档;不改源代码。结论与建议分流见 §7。

## §0 变更文件树与职责

```text
.agents/flow/issues/ A 3
m8-runner-tarball-internal-endpoint.md / .en.md / .i18n.yaml
#489 流程条目(背景/目标/验收,m8 P2 in-progress)
backend/app/api/runners.py M +6/-1
runner_tarball() 读取 OSS 改为 internal=True
tests/backend/test_runner_tarball.py M +43/-3
store fixture fake store 接受 internal kwarg
test_streams_through_the_internal_object_store_endpoint 公共 endpoint 不可达 + 内网 stub 的端到端回归
plans/2026-12-04-pr490-review.md A 本检视记录(随 PR 提交)
```

## §1 一页速览

**一句话**:把 runner 安装包的「取货路线」从收费的公共道路改走园区内部通道——spot 实例还是来 API 这个代收点取货,代收点去仓库拿货时改走内部通道,不再为每次取货付公网流量费。

**一个比喻**:API 是小区门口的代收点,OSS 是仓库。以前每来一个快递员(spot 实例),代收点都要跑到园区外的收费公路(公网 endpoint)去仓库取一次货;其实仓库就在同一园区(同地域同 VPC),有一条免费的内部通道(`-internal` endpoint)。这个 PR 只改代收点去仓库的路线,快递员仍然只到代收点,不需要知道仓库在哪。

**交付块**

| 块 | 内容 |
|---|---|
| 读取路径 | `backend/app/api/runners.py:164` 改为 `object_store_from_settings(settings, internal=True)` |
| endpoint 选择 | `backend/app/services/object_store.py:375` 的既有逻辑:`S3_INTERNAL_ENDPOINT` 显式覆盖 → 从 `s3_endpoint` 派生 `-internal` → 原 endpoint 兜底 |
| 回归测试 | `tests/backend/test_runner_tarball.py:81` 公共 endpoint `127.0.0.1:1` 不可达,内网 stub 完整返回 3.67 MB 载荷 |
| 流程条目 | `.agents/flow/issues/m8-runner-tarball-internal-endpoint.*`(m8 / P2 / in-progress) |

**验证基线**:`pytest tests/backend/test_runner_tarball.py` → 8 passed;本地全量 `pytest` → 通过;`npm test` → 130 passed;`npm run verify:docs` → 全绿(8 叶);`npm run verify:flow` → ok;`PR_TITLE=... node scripts/verify-flow.mjs --diff origin/main` → ok;本地 uvicorn + S3 stub API 冒烟 → 4/4 passed。

## §2 架构导览

### 2.1 旧路径为什么会重复计费

`runner_tarball()` 是 spot cloud-init 的固定下载地址(`backend/app/api/runners.py:141`)。它先用平台凭据 `head_object` 拿到大小,再用 `iter_object` 流式转发(`:181`)。旧代码 `object_store_from_settings(settings)`(无 `internal`)使用 `S3_ENDPOINT` 的公网地址,所以每台 spot 下载 228 MB,API 就从 OSS 公网读 228 MB;367 台就是约 83.7 GB 公网流出,与 issue 的账单对账一致。

### 2.2 endpoint 选择逻辑本身是既有能力

`object_store_from_settings(settings, internal=True)`(`backend/app/services/object_store.py:375`)先看 `S3_INTERNAL_ENDPOINT`,否则用 `internal_aliyun_endpoint()`(`:354`)把 `oss-cn-hangzhou.aliyuncs.com` 改成 `oss-cn-hangzhou-internal.aliyuncs.com`;非阿里云或无法派生时回退到原 endpoint。因此本次改动不需要新增配置,`dataset-images` 的内网 presign 已经在使用同一条通道。

`internal=True` 返回 `None` 只在完全未配置对象存储时发生,所以 `if store is None` 的 503 分支保持不变;没有额外加 `or object_store_from_settings(settings)`,因为该函数内部已经完成「无内网则回退公网」。

### 2.3 测试为什么能证明「没有回退公网」

`test_streams_through_the_internal_object_store_endpoint`(`tests/backend/test_runner_tarball.py:81`)把 `S3_ENDPOINT` 指向 `http://127.0.0.1:1`(必然连接失败),把 `S3_INTERNAL_ENDPOINT` 指向本地 `StubObjectStore`,然后走真实 FastAPI 路由。若实现回退公网,请求会 502;实际返回 200 且内容逐字节一致,证明读取只走了内网。

## §3 数据流走查

1. spot cloud-init 带 `RUNNER_HEARTBEAT_TOKEN` 请求 `GET /runner/v1/runner-tarball/2.338.2.tar.gz`(`backend/app/api/runners.py:137`)。
2. `require_runner_token` 校验 Bearer;未配置 token 时开发环境放行(`:80`)。
3. 正则只接受 `<major>.<minor>.<patch>.tar.gz`;非法文件名 404(`:154`)。
4. `object_store_from_settings(settings, internal=True)` 选择内网 endpoint(`:164`)。
5. `head_object` 拿 `Content-Length`;`ObjectMissing` → 404,其余 `ObjectStoreError` → 502(`:170`)。
6. `StreamingResponse(store.iter_object(key))` 以 `application/gzip` 流式返回(`:181`);spot 实例始终只访问 API 的 VPC 回调地址。

## §4 导读地图

| 文件 | 状态 | 说明 |
|---|---|---|
| `backend/app/api/runners.py` | ✅ 已通读变更段与上下游 120 行 | 路由、鉴权、错误映射 |
| `backend/app/services/object_store.py` | ✅ 已通读 | endpoint 派生、presign、head/iter |
| `tests/backend/test_runner_tarball.py` | ✅ 已通读 | fixture 与 8 个场景 |
| `tests/backend/object_stub.py` | ✅ 已通读 | HEAD/GET/PUT stub 行为 |
| `.agents/flow/issues/m8-runner-tarball-internal-endpoint.*` | ✅ 已通读 | 与 issue #489 对照 |

## §5 人工验收清单

- [x] 配置内网 endpoint 后,`/runner/v1/runner-tarball/<version>.tar.gz` 走内网读取并返回完整包(单测 + 本地 uvicorn 冒烟)。
- [x] 测试把公共 endpoint 指向不可达地址,确保实现没有回退公网。
- [x] 后端测试与仓库门禁通过。
- [ ] 部署后 runner 相关公网流出接近 0(需部署后核对 OSS 账单)。
- [ ] 当前 spot 池仍能正常下载 runner 包并注册(需部署后真实 spot 验证)。

## §6 讨论点

### 6.1 内网 endpoint 故障时是否要回退公网

- **位置**:`backend/app/api/runners.py:164`
- **现象**:现在只使用内网 endpoint;若内网 DNS/链路故障,下载会 502,而不会自动回退公网。
- **问题**:可用性与成本的取舍。issue 将「失败回退公网」列为可选优化,并明确跨地域/跨 VPC 池是另一个 issue 的前置条件。
- **预判**:本次不加回退。静默回退会重新产生公网流量,且流式响应一旦开始就无法中途换源;故障时应显式报错,由运维发现链路问题。
- **结论**:暂不修复;部署后若内网链路出现稳定性问题,另开 issue 设计回退或镜像烘焙。

### 6.2 测试使用显式 `S3_INTERNAL_ENDPOINT`,未覆盖「从公网 endpoint 派生」

- **位置**:`tests/backend/test_runner_tarball.py:91`
- **现象**:回归测试显式设置内网地址;生产可能只配置 `S3_ENDPOINT`,由代码派生 `-internal`。
- **问题**:派生逻辑是否回归不会被这条路由测试直接捕获。
- **预判**:派生逻辑已有单测覆盖(`test_dataset_image_tarballs.py::test_internal_endpoint_derivation`、`test_object_store_internal_uses_derived_endpoint`),路由层只需证明传入了 `internal=True`;显式配置测试更稳定,避免依赖公网域名形状。
- **结论**:不额外加测试;已有单测与路由测试组合覆盖。

### 6.3 冒烟是本地 stub,不是真实 OSS 内网

- **位置**:PR 验证段
- **现象**:本地无法访问生产内网 endpoint,冒烟用 stub 证明代码路径,未证明阿里云内网 DNS/证书。
- **问题**:部署后仍需真实 spot 下载验证。
- **预判**:issue 已在 API 主机上用内网 endpoint HEAD 成功,且 `dataset-images` 内网通道在跑;残余风险集中在容器 DNS/网络策略,部署后第一台 spot 即可验证。
- **结论**:PR 已写明部署后验证项,不阻塞合入。

## §7 分流表

| 发现 | 定性 | 去处 | 状态 |
|---|---|---|---|
| 内网故障无公网回退 | 取舍,非缺陷 | 本 PR 说明;必要时另开 issue | ✅ 已决 2026-12-04:不加回退,避免静默公网流量 |
| 派生 endpoint 未在路由测试覆盖 | 覆盖建议 | 既有单测已覆盖 | ✅ 已决 2026-12-04:不需要新测试 |
| 真实内网链路未本地验证 | 发布后验收 | PR「部署后验证」 | ⏳ 待部署 |
| issue 账单验收未完成 | 发布后验收 | issue #489 保持 open | ⏳ 待部署 |

## §8 链接

- PR:https://github.com/Shadow-Azure/ai-eval-platform/pull/490
- Issue:#489
- 流程条目:`.agents/flow/issues/m8-runner-tarball-internal-endpoint.md`
- 相关既有能力:`backend/app/services/dataset_image_tarballs.py`(#273 内网 presign)

## §9 文件级代码分析

### backend/app/api/runners.py

**职责**:Runner 平面 API,包括固定 runner 安装包代理。**变更形态**:M(+6/-1)。

**关键依赖**:`object_store_from_settings` 是平台自研的对象存储工厂(`backend/app/services/object_store.py:375`),不引入第三方 SDK;`StreamingResponse` 来自 FastAPI/Starlette,负责把 `iter_object` 的 64 KB 分块直接写给客户端,避免把 228 MB 包读进内存。

**旧代码 vs 新代码**

```python
# 旧:公网 endpoint
store = object_store_from_settings(settings)

# 新:优先内网 endpoint
store = object_store_from_settings(settings, internal=True)
```

**这在做什么、为什么这么写**:只改 endpoint 选择,不改鉴权、错误码、流式响应和 Content-Length。`internal=True` 是仓库里 `dataset_image_tarballs`、`asset_artifacts` 已在用的既有约定;它自己处理「显式内网 → 派生内网 → 公网兜底」,所以这里不需要重复写 fallback。

**实现思路总结**:spot 与 API 同 VPC,API 读 OSS 走内网;spot 仍然只连 API,不直连 OSS。

**检视发现**:无代码缺陷;唯一取舍是内网故障时不回退公网(§6.1)。

### tests/backend/test_runner_tarball.py

**职责**:M5-11 runner 安装包代理的回归测试。**变更形态**:M(+43/-3)。

**关键依赖**:`StubObjectStore`(`tests/backend/object_stub.py`)是进程内 S3-ish HTTP stub,支持 HEAD/GET/PUT;它让测试经过真实的 presign + urllib 网络路径,而不需要 OSS 凭据。

**变更分析**

- fixture 的 fake store 从 `lambda _settings:` 改为接受 `**_kwargs`,因为实现现在会传 `internal=True`。
- 新增端到端场景:公共 endpoint 不可达、内网指向 stub、断言 200/内容/Content-Length。这是「没有回退公网」的直接证据。
- 其余 401/404/502 场景保持不变,说明错误契约未受影响。

**实现思路总结**:用不可达的公网地址做「负向控制」,用可用的内网 stub 做「正向验证」,两者同时成立才证明修复生效。

**检视发现**:无;测试覆盖足够,且没有暴露 token(沿用既有断言)。
43 changes: 41 additions & 2 deletions tests/backend/test_runner_tarball.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from app.api import runners as runners_api
from app.services.object_store import ObjectMissing, ObjectStoreError
from fastapi.testclient import TestClient
from object_stub import StubObjectStore

PAYLOAD = b"\x1f\x8b" + b"runner-archive-bytes" * 16
TARBALL_URL = "/runner/v1/runner-tarball/2.338.2.tar.gz"
Expand Down Expand Up @@ -48,7 +49,11 @@ def chunks():
@pytest.fixture
def store(monkeypatch: pytest.MonkeyPatch):
def _install(fake: FakeStore) -> FakeStore:
monkeypatch.setattr(runners_api, "object_store_from_settings", lambda _settings: fake)
monkeypatch.setattr(
runners_api,
"object_store_from_settings",
lambda _settings, **_kwargs: fake,
)
return fake

return _install
Expand All @@ -73,6 +78,36 @@ def test_streams_the_cached_archive_with_the_runner_token(
assert fake.streamed == fake.headed


def test_streams_through_the_internal_object_store_endpoint(
client: TestClient, configure_settings
) -> None:
"""Issue #489: the proxy must read OSS internally, not over public egress."""

key = "runner/actions-runner-linux-x64-2.338.2.tar.gz"
with StubObjectStore() as stub:
stub.add_bytes(key, PAYLOAD)
# The public endpoint is unreachable. A regression to the public store
# fails the request instead of silently passing.
configure_settings(
S3_ENDPOINT="http://127.0.0.1:1",
S3_INTERNAL_ENDPOINT=stub.endpoint,
S3_BUCKET=stub.bucket,
S3_ACCESS_KEY="test-access",
S3_SECRET_KEY="test-secret",
S3_ADDRESSING_STYLE="path",
S3_REGION="oss-cn-hangzhou",
RUNNER_HEARTBEAT_TOKEN="platform-side-token",
)

response = client.get(
TARBALL_URL, headers={"Authorization": "Bearer platform-side-token"}
)

assert response.status_code == 200
assert response.content == PAYLOAD
assert response.headers["content-length"] == str(len(PAYLOAD))


def test_requires_the_runner_token_when_configured(
client: TestClient, configure_settings, store
) -> None:
Expand Down Expand Up @@ -106,7 +141,11 @@ def test_unknown_version_returns_not_found(client: TestClient, store) -> None:


def test_unconfigured_object_store_returns_unavailable(client: TestClient, monkeypatch) -> None:
monkeypatch.setattr(runners_api, "object_store_from_settings", lambda _settings: None)
monkeypatch.setattr(
runners_api,
"object_store_from_settings",
lambda _settings, **_kwargs: None,
)

response = client.get(TARBALL_URL)

Expand Down
Loading