Publish AI workspace source through the isolated runtime

This commit is contained in:
WebUp Cloud committed 2026-11-08 17:29:54 +08:00
1 parent 03169bd0f6
commit d1d14f0f77
21 files changed
+487 -68

No files matched your search

+29 -1
View File
@@ -1,5 +1,6 @@
"""Restricted OCI runtime controller. This module never builds or runs code on the gateway host."""
import hmac
import base64
import json
import os
import threading
@@ -12,6 +13,7 @@ from fastapi import APIRouter, Header, HTTPException
from kubernetes import client
from kubernetes.client.rest import ApiException
from pydantic import BaseModel, Field
from source_runtime import Source, attach_source, source_chunks, source_image
router = APIRouter()
NAMESPACE = os.environ.get("RUNTIME_NAMESPACE", "platform-runtimes")
@@ -44,6 +46,7 @@ class Service(BaseModel):
memory_mb: int = Field(ge=64, le=262144)
replicas: int = Field(ge=1, le=20)
health_path: str = Field(pattern=r"^/[^\s]{0,499}$|^$")
source_managed: bool = False
class Deployment(BaseModel):
@@ -51,6 +54,7 @@ class Deployment(BaseModel):
deploymentId: uuid.UUID
services: list[Service] = Field(min_length=1, max_length=100)
branding: dict[str, str] = Field(default_factory=dict)
source: Source | None = None
class ProjectState(BaseModel):
@@ -150,12 +154,24 @@ def deployment_manifest(project, deployment, service, branding=None):
return manifest
@router.get("/runtime/source-ready")
def source_ready(authorization: str | None = Header(default=None)):
auth(authorization)
return {"ready": bool(source_image())}
@router.post("/runtime/deployments/{deployment}")
def deploy(deployment: str, request: Deployment, authorization: str | None = Header(default=None)):
auth(authorization)
did, project = identifier(deployment), identifier(request.projectId)
if did != identifier(request.deploymentId):
raise HTTPException(400, "Deployment ID mismatch")
managed = [service for service in request.services if service.source_managed]
if len(managed) != int(request.source is not None):
raise HTTPException(400, "Source snapshot must belong to exactly one managed service")
if request.source and not source_image():
raise HTTPException(503, "Source runtime image is not configured")
chunks = source_chunks(request.source) if request.source else []
with guard:
lock = locks.setdefault(project, threading.Lock())
if not lock.acquire(timeout=1):
@@ -190,6 +206,18 @@ def deploy(deployment: str, request: Deployment, authorization: str | None = Hea
for service in request.services:
manifest = deployment_manifest(project, did, service, request.branding)
name = manifest["metadata"]["name"]
if service.source_managed:
attach_source(manifest, request.source, len(chunks))
for index, chunk in enumerate(chunks):
ensure(core.create_namespaced_config_map, namespace=NAMESPACE, body={
"metadata": {"name": f"{name}-source-{index}", "labels": manifest["metadata"]["labels"]},
"immutable": True, "binaryData": {f"part-{index:03}": base64.b64encode(chunk).decode()},
})
if request.source.runtime == "NODE":
ensure(core.create_namespaced_persistent_volume_claim, namespace=NAMESPACE, body={
"metadata": {"name": "data-" + identifier(service.id), "labels": {"platform/project": project}},
"spec": {"accessModes": ["ReadWriteOnce"], "resources": {"requests": {"storage": "1Gi"}}},
})
ensure(core.create_namespaced_secret, namespace=NAMESPACE, body={"metadata": {"name": name}, "type": "Opaque", "stringData": service.environment})
proxy_config = branding_config(service.port,request.branding)
if proxy_config is not None:
@@ -199,7 +227,7 @@ def deploy(deployment: str, request: Deployment, authorization: str | None = Hea
target_port = 8081 if proxy_config is not None else service.port
ensure(core.create_namespaced_service, namespace=NAMESPACE, body={"metadata": {"name": stable, "labels": {"platform/project": project}}, "spec": {"selector": manifest["metadata"]["labels"], "ports": [{"port": service.port, "targetPort": target_port}]}})
names.append((name, stable, manifest["metadata"]["labels"], service.port, service.replicas))
deadline = time.monotonic() + 90
deadline = time.monotonic() + (200 if request.source else 90)
while time.monotonic() < deadline:
if all((apps.read_namespaced_deployment(n, NAMESPACE).status.ready_replicas or 0) >= replicas for n, _, _, _, replicas in names):
break