Skip to content

Commit d62f047

Browse files
Add files via upload
1 parent 6cfa449 commit d62f047

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

docs/workflow-example.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# 🧩 Workflow example — `task: vm.create`
2+
3+
This document describes the full end-to-end workflow for the `vm.create` task in **OpenHVX**, from user request to resource persistence.
4+
5+
---
6+
7+
## 1. User → API Gateway
8+
9+
A user (tenant or admin) sends a request to create a new virtual machine:
10+
11+
```http
12+
POST /api/v1/admin/tasks
13+
Content-Type: application/json
14+
Authorization: Bearer <JWT>
15+
16+
{
17+
"tenantId": "test1",
18+
"action": "vm.create",
19+
"data": {
20+
"name": "example",
21+
"cpu": 2,
22+
"ram": "2GB",
23+
"imageId": "Rocky-10-GenericCloud-Base.latest",
24+
"cloudInit": { ... }
25+
}
26+
}
27+
```
28+
29+
### Gateway actions
30+
1. **Validate JWT** using the appropriate audience (`tenant` or `admin`) via the **Auth Service**, depending on the host (`api.` or `api-admin.`).
31+
2. **Check roles and scopes** according to the request domain.
32+
3. Run **anti-spoofing and policy checks** (e.g., tenant ID consistency).
33+
4. **Proxy the request** to the **Controller** service at `controller:[PORT]/api/admin/tasks`.
34+
35+
---
36+
37+
## 2. API Gateway → Controller
38+
39+
### Middleware & context setup
40+
- **accessMode middleware**: sets `req.isAdmin = true`.
41+
- **Envelope validation**: ensures presence of `kind`, `refId`, and `target`.
42+
- **Schema pre-validation**: uses `src/lib/schemas/task.js` (per action, e.g. `vm.create`).
43+
- **Tenant context resolution**:
44+
- For admin requests → tenantId is taken from the request body.
45+
- For tenant users → tenantId is extracted from JWT claims.
46+
- **Ownership validation**: confirms user’s access to `refId`; skipped for global admins.
47+
48+
---
49+
50+
## 3. Agent Selection & Ownership
51+
52+
The **Controller** performs **agent election** based on:
53+
- Agent heartbeat freshness.
54+
- Available resources (CPU, RAM, storage).
55+
- Advertised capabilities (`vm.create`, `network.*`, etc.).
56+
57+
If the selected agent already satisfies capability and availability requirements, additional checks are skipped.
58+
59+
---
60+
61+
## 4. Payload Enrichment
62+
63+
**Controller** enriches and normalizes the incoming payload through `src/lib/enrich.js (vm.create)`:
64+
- Sets default values for RAM, CPU, and storage.
65+
- Ensures proper image ID and generation type.
66+
- Adds default `cloud-init` configuration when missing.
67+
68+
---
69+
70+
## 5. Quota Reservation
71+
72+
1. Compute **resource deltas** (vCPU, RAM, storage).
73+
2. Perform a **quota hold** with TTL to reserve capacity temporarily.
74+
3. If limits are exceeded → respond with `ERR_QUOTA_EXCEEDED`.
75+
4. If successful → continue with persistence and dispatch.
76+
77+
---
78+
79+
## 6. Task Persistence & Dispatch
80+
81+
- Persist the new Task document in MongoDB with `status: "sent"`.
82+
- Publish the message to RabbitMQ:
83+
84+
```
85+
Exchange: jobs
86+
Routing key: agent.<agentId>
87+
Queue: agent.<agentId>.tasks (durable)
88+
```
89+
90+
- Return initial response to the client (`202 Accepted`) with task ID and status.
91+
92+
---
93+
94+
## 7. Agent Execution
95+
96+
- The **Agent** consumes messages from its queue `agent.<agentId>.tasks`.
97+
- Executes the PowerShell script `vm.create.ps1` on the Hyper-V host.
98+
- Upon completion, publishes a result message to the **results** exchange:
99+
100+
```
101+
Exchange: results
102+
Routing key: task.<taskId>
103+
```
104+
105+
Payload includes the results.
106+
107+
---
108+
109+
## 8. Result Handling
110+
111+
The **Controller** consumes results from the `results` exchange:
112+
- Updates the Task document with final status (`SUCCESS` or `FAILED`).
113+
- Stores logs and execution metadata.
114+
115+
Depending on the result:
116+
- **Success** → consume the quota hold (commit resources).
117+
- **Failure** → release the quota hold (rollback).
118+
119+
---
120+
121+
## 9. Resource Linking
122+
123+
- Link the newly created VM to the corresponding **TenantResource** entry.
124+
125+
---
126+
127+
## ✅ Summary (High-Level Flow)
128+
129+
| Step | Service | Description |
130+
|------|----------|-------------|
131+
| 1 | API Gateway | Auth, scopes, anti-spoofing, proxy |
132+
| 2 | Controller | Envelope & schema validation |
133+
| 3 | Controller | Agent election & ownership checks |
134+
| 4 | Controller | Payload enrichment |
135+
| 5 | Controller | Quota hold reservation |
136+
| 6 | Controller + MQ | Persist and publish task to agent queue |
137+
| 7 | Agent | Execute PowerShell script on Hyper-V |
138+
| 8 | Controller | Process result and adjust quota |
139+
| 9 | Controller | Link resources |
140+
141+
---
142+
143+
> **Note:**
144+
> The same workflow structure applies to other task types (e.g., `vm.delete`, `vm.edit`), with different schemas, enrichers, and capability filters per action.

0 commit comments

Comments
 (0)