Create Agent Instance#
Description#
This API is used to create a resident agent instance in the openYuanrong cluster. An agent instance carries user business logic, stays alive after creation until explicitly killed.
Container metadata supports two sources:
Mode |
Metadata source |
Use case |
|---|---|---|
inline |
Carried directly by |
Single-step creation, bypasses meta_service |
registered |
Registered funcMeta, associated via |
Metadata reuse, multiple creates of the same function |
Both modes coexist: a request with runtime_spec uses inline, with urn uses registered; when both are present, inline takes precedence.
Prerequisites#
The openYuanrong cluster is deployed and healthy; start the required components per mode:
inline mode (no function registration, bypasses meta_service):
yr start --master -s 'mode.master.frontend=true'registered mode (requires function registration first):
yr start --master -s 'mode.master.frontend=true' -s 'mode.master.meta_service=true'
docker / supervisor services are prepared by the user: openYuanrong does not manage the docker daemon or supervisor process. For
sandbox_type=dockerthe host must have a working docker daemon (image pulled or pullable); forsandbox_type=supervisorthe host must have a supervisor process. Unavailable services cause create to fail (e.g.no Docker image specified, container start failure).Image requirements: the docker executor inserts
yr_runtime_main.pyas the startup command for python runtime; the image must install theopenyuanrongsdk whl package (which bringsyr_runtime_main.py, faas_executor, yr runtime) into the image’s default python site-packages.workspace and UID alignment (when
workspaceis provided):workspaceis a directory on the host; the system automatically bind mounts it to/home/<rootfs.user>inside the container (/workspacewhenrootfs.useris empty) — the user only provides the host path, no need to specify the in-container mount point. Bind mount validates permissions by numeric UID (not by username); the UID ofrootfs.userinside the image must match the owner UID of the host workspace directory, otherwise the container process getsPermission deniedreading the workspace. For example,rootfs.user=agentos:# Host workspace owner UID (assume /home/snuser/workspaceA owned by snuser, UID=1002) stat -c '%u' /home/snuser/workspaceA # 1002 # agentos UID inside the image (must match) docker run --rm yr-docker-runtime:v0 -c 'id -u agentos' # must be 1002; otherwise align in the image Dockerfile
If the image UID differs from the host, options: â‘ image Dockerfile
useradd -u <hostUID> agentos(recommended, once for all); ②chown -R <containerUID> /home/snuser/workspaceAto change host dir owner for testing; ③chmod -R 755to relax permissions (testing only).
Constraints#
namespace,nameare required.inline mode requires
runtime_spec, with bothruntimeandrootfs.imageurlnon-empty.registered mode requires registering the function first, then create: first call Register Function (
POST /serverless/v1/functions,kindset toagent) to register the agent function, confirm the registration succeeded (response containsfunctionVersionUrn), then call create with thatfunctionVersionUrnasurn. registered mode must carryurnpointing to that registeredkind=agentfunction; anurnpointing to an unregistered or non-existent function returns 500failed to create agent.workspaceis optional: when non-empty it is bind mounted to/home/<rootfs.user>(/workspacewhen user is empty); when empty no mount is added. bind mountsource(bothworkspaceandmounts[].source) must be an absolute host path;/,/etc,/proc,/sys,/dev,/boot,docker.sock, and paths containing..are rejected.mounts[].target(in-container path) is not validated; the caller must ensure it does not overwrite sensitive in-container paths (e.g./etc/passwd,/proc,/sys,/dev).Auth:
/api/agentgoes through frontend’s globalGlobalJWTAuthMiddleware, consistent with other cluster REST APIs. Whenenable_func_token_authis off it is allowed by default (caller trusted); when on a valid JWT must be carried.
URI#
POST /api/agent
Request Parameters#
Request Header Parameters#
Parameter |
Required |
Type |
Description |
|---|---|---|---|
Content-Type |
Yes |
string |
Message body type. |
tenantId |
No |
string |
Tenant ID. In inline mode funcKey is composed from tenantID, default |
Request Body Parameters#
Common Parameters (both modes)#
Name |
Type |
Required |
Description |
|---|---|---|---|
namespace |
String |
Yes |
Instance namespace. |
name |
String |
Yes |
Instance name (must be unique within a tenant + namespace). The |
workspace |
String |
No |
Absolute host path, bind mounted to |
env_vars |
map |
No |
Environment variables injected into the container. Sunk via |
mounts |
array |
No |
Extra bind mounts. Each item see Mount. |
inline Mode Parameters (runtime_spec)#
Name |
Type |
Required |
Description |
|---|---|---|---|
runtime_spec |
Object |
inline required |
inline container config. |
runtime_spec.runtime |
String |
inline required |
Real language, mapped to faasExecutor. Values see Runtime Types. |
runtime_spec.sandbox_type |
String |
No |
executor dispatch. Values: |
runtime_spec.rootfs |
Object |
inline required |
Container rootfs config. |
runtime_spec.rootfs.imageurl |
String |
inline required |
docker image reference (e.g. |
runtime_spec.rootfs.user |
String |
No |
Container run-as user (must exist in image). Empty runs as root — high security risk (in-container process has maximum privileges); production use should explicitly specify a non-root user. |
runtime_spec.rootfs.ports |
array |
No |
Container port forwarding. Format |
runtime_spec.cpu |
int |
No |
CPU size, unit |
runtime_spec.memory |
int |
No |
Memory size, unit |
registered Mode Parameters#
Name |
Type |
Required |
Description |
|---|---|---|---|
urn |
String |
registered required |
Function URN (e.g. |
Runtime Types#
Value |
Mapped faasExecutor |
|---|---|
python3.6 / python3.7 / python3.8 / python3.9 / python3.10 / python3.11 |
Python3.x |
go / http / custom image |
Go1.x |
gois the Go language runtime;httpmeans invocation via the HTTP channel (runtime still the Go executor);custom imagemeans a user-provided custom image (startup command comes from the image, executor still maps to Go1.x). All three share the same Go executor. | java8 / java11 / java17 / java21 | Java8 / Java11 / Java17 / Java21 | | posix-custom-runtime | PosixCustom | | others | PosixCustom (fallback) |
Mount#
Name |
Type |
Required |
Description |
|---|---|---|---|
source |
String |
Yes |
Absolute host path. |
target |
String |
Yes |
In-container path. |
readonly |
boolean |
No |
Read-only. Default |
Response Parameters#
Name |
Type |
Description |
|---|---|---|
code |
int |
Status code; |
instance_id |
String |
Instance ID (UUID). |
Examples#
inline mode#
curl -X POST http://{frontend}:8888/api/agent -H "Content-Type: application/json" -d '{
"name": "agent-001", "namespace": "dev",
"runtime_spec": {
"runtime": "python3.11", "sandbox_type": "docker",
"rootfs": {"imageurl": "yr-docker-runtime:v0", "user": "agentos", "ports": ["tcp:22"]},
"cpu": 600, "memory": 512
},
"workspace": "/home/snuser/workspaceA",
"env_vars": {"AGENT_MODE": "prod", "userid": "u-9f3a"},
"mounts": [{"source": "/home/snuser/workspaceB", "target": "/mnt/workspaceB", "readonly": false}]
}'
Response:
{"code":200,"instance_id":"0b6c6322-6533-4901-8000-00000000bb0b"}
registered mode#
registered mode is two steps: register the kind=agent function first, confirm the registration succeeded (response code=0 and contains functionVersionUrn), then call create with that URN.
# 1. Register the agent function (one-time; confirm code=0 and response contains functionVersionUrn)
curl -H "Content-type: application/json" -X POST http://{meta_service}:31182/serverless/v1/functions -d '{
"name": "0@myService@python-agent", "kind": "agent", "runtime": "python3.11",
"cpu": 600, "memory": 512, "timeout": 60,
"storageType": "local", "codePath": "/opt/mycode/service",
"environment": {"AGENT_MODE": "prod"},
"sandboxType": "docker",
"rootfs": {"type": "image", "imageurl": "yr-docker-runtime:v0", "user": "agentos", "ports": ["tcp:22"]}
}'
# After confirming registration succeeded, take functionVersionUrn as urn
export FUNCTION_VERSION_URN='sn:cn:yrk:default:function:0@myService@python-agent:$latest'
# 2. Create the agent instance (with urn; ensure docker/supervisor service is ready)
curl -X POST http://{frontend}:8888/api/agent -H "Content-Type: application/json" -d '{
"name": "agent-001", "namespace": "dev", "urn": "'"${FUNCTION_VERSION_URN}"'",
"workspace": "/home/snuser/workspaceA",
"env_vars": {"userid": "u-9f3a"},
"mounts": [{"source": "/home/snuser/workspaceB", "target": "/mnt/workspaceB", "readonly": false}]
}'
Response:
{"code":200,"instance_id":"0b6c6322-6533-4901-8000-00000000bb0b"}
Register Function Parameters#
registered mode first calls POST /serverless/v1/functions (see Register Function) to register a kind=agent function. Parameters:
Name |
Type |
Required |
Description |
|---|---|---|---|
name |
String |
Yes |
Function name, format |
kind |
String |
Yes |
Function category; agent must be |
runtime |
String |
Yes |
Real language, mapped to faasExecutor; values see Runtime Types. |
cpu |
int |
Yes |
CPU size, unit |
memory |
int |
Yes |
Memory size, unit |
timeout |
int |
No |
Function invocation timeout in seconds, max |
storageType |
String |
No |
Code package storage type. |
codePath |
String |
No |
Code package local path. Effective when |
environment |
map |
No |
Static environment variables (key-value, all string). Written to funcMeta.Environment, merged with dynamic env_vars at create and sunk; agent kind has no |
sandboxType |
String |
No |
executor dispatch. Values |
rootfs.type |
String |
No |
rootfs type; for docker image use |
rootfs.imageurl |
String |
No |
docker image reference. Written to funcMeta.rootfs.imageurl, merged into createOptions[“rootfs”] JSON at create. |
rootfs.user |
String |
No |
Container run-as user (must exist in image). Passed through at create to createOptions[“host_user”]. |
rootfs.ports |
array |
No |
Port forwarding, format |
The registered funcMeta is written to etcd
/sn/functionsand loaded into funcSpecMap by the frontend watcher. At create,applyAgentFuncMetapasses through runtime/sandboxType/rootfs/cpu/memory/environment; no need to repeat them in the create request body.
Error Codes#
Response body code field: 200 for success, 500 for failure (the message field carries the specific error).
HTTP status |
Description |
|---|---|
400 |
Bad Request. |
500 |
Internal Server Error. |