@namzu/sandbox 13.0.0 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +309 -0
  2. package/README.md +151 -0
  3. package/dist/backends/firecracker/protocol.d.ts +22 -0
  4. package/dist/backends/firecracker/protocol.d.ts.map +1 -1
  5. package/dist/backends/firecracker/protocol.js.map +1 -1
  6. package/dist/backends/firecracker/transport.d.ts +104 -9
  7. package/dist/backends/firecracker/transport.d.ts.map +1 -1
  8. package/dist/backends/firecracker/transport.js +139 -13
  9. package/dist/backends/firecracker/transport.js.map +1 -1
  10. package/dist/backends/kubernetes/egress-policy.d.ts +219 -0
  11. package/dist/backends/kubernetes/egress-policy.d.ts.map +1 -0
  12. package/dist/backends/kubernetes/egress-policy.js +314 -0
  13. package/dist/backends/kubernetes/egress-policy.js.map +1 -0
  14. package/dist/backends/kubernetes/index.d.ts +374 -0
  15. package/dist/backends/kubernetes/index.d.ts.map +1 -0
  16. package/dist/backends/kubernetes/index.js +671 -0
  17. package/dist/backends/kubernetes/index.js.map +1 -0
  18. package/dist/backends/kubernetes/k8s-client.d.ts +125 -0
  19. package/dist/backends/kubernetes/k8s-client.d.ts.map +1 -0
  20. package/dist/backends/kubernetes/k8s-client.js +246 -0
  21. package/dist/backends/kubernetes/k8s-client.js.map +1 -0
  22. package/dist/backends/kubernetes/lease.d.ts +119 -0
  23. package/dist/backends/kubernetes/lease.d.ts.map +1 -0
  24. package/dist/backends/kubernetes/lease.js +151 -0
  25. package/dist/backends/kubernetes/lease.js.map +1 -0
  26. package/dist/backends/kubernetes/objects.d.ts +282 -0
  27. package/dist/backends/kubernetes/objects.d.ts.map +1 -0
  28. package/dist/backends/kubernetes/objects.js +156 -0
  29. package/dist/backends/kubernetes/objects.js.map +1 -0
  30. package/dist/backends/kubernetes/privilege-probe.d.ts +136 -0
  31. package/dist/backends/kubernetes/privilege-probe.d.ts.map +1 -0
  32. package/dist/backends/kubernetes/privilege-probe.js +185 -0
  33. package/dist/backends/kubernetes/privilege-probe.js.map +1 -0
  34. package/dist/backends/kubernetes/sandbox.d.ts +123 -0
  35. package/dist/backends/kubernetes/sandbox.d.ts.map +1 -0
  36. package/dist/backends/kubernetes/sandbox.js +299 -0
  37. package/dist/backends/kubernetes/sandbox.js.map +1 -0
  38. package/dist/backends/kubernetes/transport.d.ts +122 -0
  39. package/dist/backends/kubernetes/transport.d.ts.map +1 -0
  40. package/dist/backends/kubernetes/transport.js +197 -0
  41. package/dist/backends/kubernetes/transport.js.map +1 -0
  42. package/dist/backends/kubernetes/workspace.d.ts +381 -0
  43. package/dist/backends/kubernetes/workspace.d.ts.map +1 -0
  44. package/dist/backends/kubernetes/workspace.js +1064 -0
  45. package/dist/backends/kubernetes/workspace.js.map +1 -0
  46. package/dist/index.d.ts +132 -2
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +102 -34
  49. package/dist/index.js.map +1 -1
  50. package/dist/testing/sandbox-conformance.d.ts +193 -0
  51. package/dist/testing/sandbox-conformance.d.ts.map +1 -0
  52. package/dist/testing/sandbox-conformance.js +465 -0
  53. package/dist/testing/sandbox-conformance.js.map +1 -0
  54. package/package.json +5 -4
  55. package/src/backends/firecracker/protocol.ts +27 -0
  56. package/src/backends/firecracker/transport.ts +199 -28
  57. package/src/backends/kubernetes/egress-policy.ts +437 -0
  58. package/src/backends/kubernetes/index.ts +1012 -0
  59. package/src/backends/kubernetes/k8s-client.ts +352 -0
  60. package/src/backends/kubernetes/lease.ts +198 -0
  61. package/src/backends/kubernetes/objects.ts +363 -0
  62. package/src/backends/kubernetes/privilege-probe.ts +261 -0
  63. package/src/backends/kubernetes/sandbox.ts +395 -0
  64. package/src/backends/kubernetes/transport.ts +286 -0
  65. package/src/backends/kubernetes/workspace.ts +1386 -0
  66. package/src/index.ts +257 -35
  67. package/src/testing/sandbox-conformance.ts +667 -0
@@ -0,0 +1,671 @@
1
+ /**
2
+ * Kubernetes / agent-sandbox backend — acquire, readiness and teardown.
3
+ *
4
+ * Sibling of `firecracker/` and `aci-standby-pool/`: same
5
+ * {@link SandboxBackend} surface, same "the host supplies the credential
6
+ * callback and this package carries no cloud SDK" boundary, a different
7
+ * control plane. Here the control plane is the Kubernetes API server and the
8
+ * warm pool is a `SandboxWarmPool` reconciled by the agent-sandbox controller
9
+ * (kubernetes-sigs/agent-sandbox v1.0.2).
10
+ *
11
+ * Registered as `microvm` because {@link SandboxTier} names the strength of
12
+ * the boundary, not the orchestrator that starts it: a pod scheduled onto a
13
+ * Kata RuntimeClass runs in a hardware-virtualized guest. The tier also keeps
14
+ * the backend out of the container tier's mandatory
15
+ * `ContainerSandboxLayout`, which a remote-copy backend has no use for —
16
+ * Firecracker took the same exemption.
17
+ *
18
+ * ## Two acquire paths, and why they create different kinds
19
+ *
20
+ * - `warmPoolName` set → POST a `SandboxClaim` at the named pool. The
21
+ * controller binds an already-running Sandbox out of the pool, which is
22
+ * what makes a sub-second acquire possible at all.
23
+ * - `warmPoolName` unset → POST a `Sandbox` directly. This is necessitated
24
+ * rather than chosen: `SandboxClaimSpec.warmPoolRef` is a REQUIRED field,
25
+ * so a pool-less claim does not exist in the API.
26
+ *
27
+ * The claim this backend POSTs is PRISTINE: `warmPoolRef` and a lifecycle
28
+ * bound, nothing else. `spec.env` and `spec.volumeClaimTemplates` are
29
+ * available on the claim and are never set, because a claim carrying either
30
+ * is forced to cold-start instead of adopting a pool sandbox — the single
31
+ * most expensive mistake available on this path, and a silent one, since such
32
+ * a claim still works and only the latency shows it. Per-sandbox controls
33
+ * that would need those fields are refused by {@link assertEnforceable}
34
+ * rather than accepted and dropped.
35
+ *
36
+ * ## The bound sandbox is not named after the claim
37
+ *
38
+ * A pool sandbox keeps the generated name the pool gave it when the claim
39
+ * adopts it. The backend therefore reads the bound identity back out of
40
+ * `status.sandbox` and never derives it from the claim's own name. A test
41
+ * covers exactly that asymmetry.
42
+ *
43
+ * ## Credential
44
+ *
45
+ * The per-instance agent bind token is the backing pod's own
46
+ * `metadata.uid`: the host learns it from the API server after readiness, the
47
+ * guest learns it through the downward API (`NAMZU_AGENT_BIND_TOKEN`), and
48
+ * nothing has to be minted, stored or injected at claim time. One GET, no
49
+ * claim mutation, so the warm path stays pristine. A resumed pod is a new pod
50
+ * with a new uid, which is correct: it is a new instance.
51
+ *
52
+ * ## The lease
53
+ *
54
+ * The `shutdownTime` acquire stamps is the leak guard AND, unrenewed, a
55
+ * deadline on the run. The Sandbox handle owns a renewal loop that PATCHes
56
+ * it forward every half-TTL for as long as the handle is alive, and
57
+ * `destroy()` stops it — so a live run keeps its pod and a dead host still
58
+ * costs the cluster exactly one expiry. See `lease.ts`.
59
+ *
60
+ * ## The privilege probe
61
+ *
62
+ * `create()` does not resolve until the guest has reported — and this
63
+ * backend has checked — that it is deprivileged: all four capability masks
64
+ * zero, `NoNewPrivs: 1`, read out of `/proc/self/status` over the agent's
65
+ * `execute` op, on a clock of its own so a guest that goes quiet is refused
66
+ * rather than waited on. A refusal destroys the instance and rejects, so no
67
+ * handle to an under-hardened sandbox escapes. There is no off switch. See
68
+ * `privilege-probe.ts`.
69
+ *
70
+ * ## Not watch
71
+ *
72
+ * Readiness is polled against the shared {@link OperationDeadline}, exactly as
73
+ * ACI polls `provisioningState`. A watch would buy nothing on a path whose
74
+ * whole budget is under a second, and would cost resourceVersion tracking,
75
+ * bookmarks, 410-relist and reconnect backoff.
76
+ *
77
+ * ## Egress
78
+ *
79
+ * `config.egress` is optional and, when set, translated and VERIFIED — never
80
+ * created — by `egress-policy.ts`. Verification happens once, lazily, on the
81
+ * first `create()`, so `buildKubernetesBackend` itself still contacts
82
+ * nothing. Every Sandbox this file creates directly (`buildSandboxBody`)
83
+ * carries {@link sandboxTemplateLabel} on its podTemplate specifically so
84
+ * that translated policy's `podSelector` has something stable to match —
85
+ * see `objects.ts`'s doc comment on that label for why agent-sandbox's own
86
+ * controller-owned label does not cover this path.
87
+ */
88
+ import { generateSandboxId } from '@namzu/sdk';
89
+ import { OperationDeadline, OperationDeadlineExpired, resolveReadinessOptions, runFailureCleanup, } from '../readiness.js';
90
+ import { assertEgressPolicyIsEnforceable, defaultEgressPolicyName, translateEgressPolicy, verifyEgressPolicyApplied, } from './egress-policy.js';
91
+ import { KubernetesAlreadyGoneError, createKubernetesClient, } from './k8s-client.js';
92
+ import { READY_CONDITION, SANDBOX_API_GROUP, SANDBOX_API_VERSION, SANDBOX_EXTENSIONS_API_GROUP, claimCollectionPath, claimPath, isConditionTrue, isPodLive, podListPath, podPath, sandboxCollectionPath, sandboxPath, sandboxTemplateLabel, sandboxTemplatePath, } from './objects.js';
93
+ import { privilegeProbeTimedOut, runPrivilegeProbe } from './privilege-probe.js';
94
+ import { buildKubernetesSandbox } from './sandbox.js';
95
+ import { KubernetesAgentTransport } from './transport.js';
96
+ /**
97
+ * The same number the Firecracker guest agent listens on over vsock
98
+ * (`DEFAULT_AGENT_VSOCK_PORT`), so one agent has one port across both tiers
99
+ * and a manifest, a NetworkPolicy and a transport can all name it from
100
+ * memory. Unprivileged, and the sandbox pod is not sharing it with anything.
101
+ */
102
+ export const DEFAULT_AGENT_PORT = 1024;
103
+ /**
104
+ * Deliberately far below ACI's 500 ms. A pool bind lands in ~120 ms on a warm
105
+ * cluster, so a half-second poll would spend most of the sub-second acquire
106
+ * budget asleep; 50 ms costs a handful of cheap GETs and gives the measurement
107
+ * somewhere to land.
108
+ */
109
+ const DEFAULT_READY_POLL_MS = 50;
110
+ const DEFAULT_READY_TIMEOUT_MS = 60_000;
111
+ const DEFAULT_CLAIM_TTL_SECONDS = 3_600;
112
+ /**
113
+ * The ceiling on the privilege probe's own clock — see
114
+ * {@link resolveProbeTimeoutMs} for where the rest of the number comes from.
115
+ *
116
+ * The probe is one `cat` of a pseudo-file over an already-established path,
117
+ * so half a minute would already be generous and a quarter of one is plenty.
118
+ * The number matters because the alternative is not "a bit longer": with no
119
+ * clock of its own the probe falls back on the execution controller's generic
120
+ * defaults — a five-minute execution observation, then a cancel-confirm and a
121
+ * drain — so a guest that accepts the TCP connection and then stops answering
122
+ * would keep a 60 s `create()` pending for over six minutes.
123
+ */
124
+ const PRIVILEGE_PROBE_TIMEOUT_CAP_MS = 15_000;
125
+ /**
126
+ * How long the privilege probe may take, given the caller's readiness budget.
127
+ *
128
+ * `readyTimeoutMs` bounds the CONTROL plane and has usually expired by the
129
+ * time the probe starts, so the probe cannot share it — but it is still the
130
+ * number the caller chose to describe how long an acquire may take, so the
131
+ * probe is allowed exactly that much again and no more, capped. A caller who
132
+ * asked for a 500 ms acquire gets a 500 ms probe; one who asked for two
133
+ * minutes of cold start still gets {@link PRIVILEGE_PROBE_TIMEOUT_CAP_MS}.
134
+ * Deliberately not a separate config key: a knob whose only correct value is
135
+ * "long enough for one `cat`" is a knob that only ever gets set wrong.
136
+ */
137
+ export function resolveProbeTimeoutMs(readyTimeoutMs) {
138
+ return Math.min(readyTimeoutMs, PRIVILEGE_PROBE_TIMEOUT_CAP_MS);
139
+ }
140
+ /**
141
+ * The readiness bounds every path in this backend polls against — acquire,
142
+ * and `workspace.ts`'s create/suspend/resume. One function so the two cannot
143
+ * drift apart on defaults.
144
+ */
145
+ export function resolveKubernetesReadiness(config) {
146
+ return resolveReadinessOptions('kubernetes', config.readyTimeoutMs, config.readyPollIntervalMs, {
147
+ timeoutMs: DEFAULT_READY_TIMEOUT_MS,
148
+ pollIntervalMs: DEFAULT_READY_POLL_MS,
149
+ });
150
+ }
151
+ /**
152
+ * Per-sandbox controls this backend cannot apply, and therefore refuses.
153
+ *
154
+ * `env` is the load-bearing one. A SandboxClaim CAN carry `spec.env`, so this
155
+ * looks at first like a control that fits — but a claim that sets it is forced
156
+ * to cold-start instead of adopting a pool sandbox, which turns a 120 ms
157
+ * acquire into a full pod start. Accepting env here would buy a caller a
158
+ * feature and silently take the warm pool away, and the only symptom would be
159
+ * latency. The limits belong on the SandboxTemplate the pool is built from.
160
+ *
161
+ * `egress` is refused because this backend applies egress at the network
162
+ * layer, on the template's NetworkPolicy, which cannot be rewritten per
163
+ * running sandbox. The container backend's habit of emitting proxy
164
+ * environment variables as a substitute is not repeated here: a policy
165
+ * accepted and quietly not enforced is worse than one that is refused.
166
+ */
167
+ const UNSUPPORTED_PER_SANDBOX_CONTROLS = [
168
+ ['egress', 'network egress policy'],
169
+ ['memoryLimitMb', 'memory limit'],
170
+ ['maxProcesses', 'process limit'],
171
+ ['env', 'environment variables'],
172
+ ];
173
+ export function assertEnforceable(options) {
174
+ const unenforceable = UNSUPPORTED_PER_SANDBOX_CONTROLS.filter(([key]) => {
175
+ const value = options[key];
176
+ return value !== undefined && (key !== 'env' || Object.keys(value).length > 0);
177
+ });
178
+ if (unenforceable.length === 0)
179
+ return;
180
+ throw new Error(`The kubernetes sandbox backend cannot enforce per-sandbox ${unenforceable
181
+ .map(([, label]) => label)
182
+ .join(', ')}: a SandboxClaim carrying env or volumes is forced to cold-start instead of adopting a warm pool sandbox, and egress is a NetworkPolicy on the pool's SandboxTemplate rather than a per-sandbox setting. Set them on the SandboxTemplate the SandboxWarmPool is built from, or use a backend that applies them per sandbox. Refusing rather than accepting a control that would be silently dropped.`);
183
+ }
184
+ /**
185
+ * Refuse a runtime class the pool path cannot honour.
186
+ *
187
+ * A pooled sandbox is already running by the time a claim reaches it, under
188
+ * whatever RuntimeClass its SandboxTemplate named. `runtimeClassName` in this
189
+ * config would therefore be read, accepted and ignored — and the thing it
190
+ * selects is the VM boundary, which is the last control to lose quietly.
191
+ */
192
+ export function assertRuntimeClassIsApplicable(config) {
193
+ if (config.runtimeClassName === undefined || config.warmPoolName === undefined)
194
+ return;
195
+ throw new Error(`The kubernetes sandbox backend cannot apply runtimeClassName ${JSON.stringify(config.runtimeClassName)} to sandboxes claimed from warm pool ${JSON.stringify(config.warmPoolName)}: a pooled sandbox is already running under the RuntimeClass its SandboxTemplate named, and a claim cannot change it. Set runtimeClassName on that SandboxTemplate's podTemplate, or drop warmPoolName to have this backend create each Sandbox itself.`);
196
+ }
197
+ /**
198
+ * Build a {@link SandboxBackend} against a cluster running the agent-sandbox
199
+ * controller. Construction is synchronous and contacts nothing: readiness
200
+ * bounds and the config refusals are validated here so a misconfiguration
201
+ * surfaces during host wiring rather than mid-run, and the first API call
202
+ * happens on the first `create()`.
203
+ */
204
+ export function buildKubernetesBackend(config) {
205
+ const readiness = resolveKubernetesReadiness(config);
206
+ assertRuntimeClassIsApplicable(config);
207
+ // A hostname allowlist with no FQDN-capable engine declared is a
208
+ // configuration error, not a runtime one — it can be decided from
209
+ // `config.egress.policy.kind` alone, with no API call, so it is refused
210
+ // here, synchronously, the same moment the two checks above are.
211
+ if (config.egress) {
212
+ assertEgressPolicyIsEnforceable(config.egress.policy, config.egress.engine ?? 'core');
213
+ }
214
+ const client = createKubernetesClient(clientAccess(config));
215
+ // Verify-not-trust runs once, lazily, on the first `create()` — never here,
216
+ // because `buildKubernetesBackend` is documented to contact nothing. A
217
+ // failed attempt is not cached: a transient API error should not wedge
218
+ // every later create() behind the same stale rejection forever.
219
+ let egressVerification;
220
+ return {
221
+ tier: 'microvm',
222
+ name: 'kubernetes',
223
+ async create(options) {
224
+ if (config.egress) {
225
+ egressVerification ??= verifyEgressPolicyConfigured(client, config.namespace, config.sandboxTemplateName, config.egress, options.signal).catch((err) => {
226
+ egressVerification = undefined;
227
+ throw err;
228
+ });
229
+ await egressVerification;
230
+ }
231
+ const acquisition = await acquireKubernetesSandbox(client, config, options, readiness);
232
+ return await admitProbedSandbox(acquisition, config, options, resolveProbeTimeoutMs(readiness.timeoutMs));
233
+ },
234
+ };
235
+ }
236
+ /**
237
+ * Translate `egress.policy` and confirm an operator applied a matching
238
+ * object — the whole verify-not-trust step, isolated so `create()` above
239
+ * stays about ONE thing (memoize-once-per-backend) rather than two.
240
+ *
241
+ * Exported because `workspace.ts` runs the identical step: a workspace does
242
+ * not go through `buildKubernetesBackend`, and a config `egress` honoured on
243
+ * one entry point and ignored on the other would be a silent downgrade of the
244
+ * boundary this backend calls primary. `sandboxTemplateName` is the template
245
+ * the caller is actually building from — it decides both the default policy
246
+ * name and the pod label the policy's selector has to match, and a workspace
247
+ * may be built from a different template than the task path's.
248
+ */
249
+ export async function verifyEgressPolicyConfigured(client, namespace, sandboxTemplateName, egress, signal) {
250
+ const engine = egress.engine ?? 'core';
251
+ const translated = await translateEgressPolicy(egress.policy, engine, {
252
+ namespace,
253
+ name: egress.networkPolicyName ?? defaultEgressPolicyName(sandboxTemplateName),
254
+ sandboxTemplateName,
255
+ });
256
+ await verifyEgressPolicyApplied(client, translated, signal);
257
+ }
258
+ /** Config → the client's own access shape. Shared with `workspace.ts`. */
259
+ export function clientAccess(config) {
260
+ const access = config.access;
261
+ if (access.inCluster === true)
262
+ return { inCluster: true };
263
+ return {
264
+ server: access.server,
265
+ namespace: config.namespace,
266
+ getToken: access.getToken,
267
+ ...(access.ca !== undefined ? { ca: access.ca } : {}),
268
+ };
269
+ }
270
+ /**
271
+ * Claim or create, wait for Ready, read the bound identity back, resolve the
272
+ * address and learn the pod's uid — or leave nothing behind trying.
273
+ *
274
+ * Exported because the sandbox surface is built on top of this record rather
275
+ * than beside it: one acquire path, one cleanup path, whatever ends up
276
+ * wrapping them.
277
+ */
278
+ export async function acquireKubernetesSandbox(client, config, options, readiness) {
279
+ options.signal?.throwIfAborted();
280
+ assertEnforceable(options);
281
+ assertRuntimeClassIsApplicable(config);
282
+ const namespace = config.namespace;
283
+ const ttlSeconds = config.claimTtlSeconds ?? DEFAULT_CLAIM_TTL_SECONDS;
284
+ const shutdownTime = new Date(Date.now() + ttlSeconds * 1_000).toISOString();
285
+ // Client-owned name, as on ACI: it lets failure cleanup DELETE the object
286
+ // even when the create response never arrived. `generateSandboxId` returns
287
+ // a lowercase UUID, which is already a legal DNS-1123 name suffix.
288
+ const objectName = `namzu-task-${generateSandboxId()}`;
289
+ const ownedPath = config.warmPoolName !== undefined
290
+ ? claimPath(namespace, objectName)
291
+ : sandboxPath(namespace, objectName);
292
+ const release = async (signal) => {
293
+ try {
294
+ await client.request('DELETE', ownedPath, undefined, signal);
295
+ }
296
+ catch (err) {
297
+ // The object is gone, which is the state DELETE was asking for.
298
+ if (!(err instanceof KubernetesAlreadyGoneError))
299
+ throw err;
300
+ }
301
+ };
302
+ // Where the expiry lives differs by KIND, and only this function knows
303
+ // which kind it created: a claim keeps it under `spec.lifecycle`, a
304
+ // directly created Sandbox at the top of `spec` (v1beta1 as served has
305
+ // not moved it under `lifecycle` yet). Merge-patch semantics (RFC 7386,
306
+ // the only content type this client's PATCH sends) merge the nested
307
+ // object, so `shutdownPolicy: Delete` survives every renewal.
308
+ // The parameter is deliberately NOT named `shutdownTime`: the stamp above
309
+ // is the one the create body carries, and no renewal ever re-sends it.
310
+ const renew = async (nextShutdownTime, signal) => {
311
+ const patch = config.warmPoolName !== undefined
312
+ ? { spec: { lifecycle: { shutdownTime: nextShutdownTime } } }
313
+ : { spec: { shutdownTime: nextShutdownTime } };
314
+ await client.request('PATCH', ownedPath, patch, signal);
315
+ };
316
+ // One clock over the whole path — the create POST included, so a hung API
317
+ // server cannot leave `create()` pending past the caller's timeout.
318
+ const deadline = new OperationDeadline(readiness.timeoutMs, 'kubernetes readiness', options.signal);
319
+ // The pool-less path reads its pod template BEFORE anything is created, so
320
+ // a missing or malformed template fails with nothing to clean up — hence
321
+ // this sits outside the cleanup block below. It is read per create rather
322
+ // than cached: an operator editing the template expects the next sandbox to
323
+ // use it, and this path is not the sub-second one.
324
+ const createPath = config.warmPoolName !== undefined
325
+ ? claimCollectionPath(namespace)
326
+ : sandboxCollectionPath(namespace);
327
+ const createBody = config.warmPoolName !== undefined
328
+ ? buildClaimBody(namespace, objectName, config.warmPoolName, shutdownTime)
329
+ : buildSandboxBody({
330
+ namespace,
331
+ name: objectName,
332
+ template: await deadline.run((signal) => readSandboxTemplate(client, namespace, config.sandboxTemplateName, signal)),
333
+ sandboxTemplateName: config.sandboxTemplateName,
334
+ shutdownTime,
335
+ ...(config.runtimeClassName !== undefined
336
+ ? { runtimeClassName: config.runtimeClassName }
337
+ : {}),
338
+ });
339
+ try {
340
+ // Inside the cleanup block: a POST that fails client-side may still have
341
+ // committed, so the only safe assumption is that the object exists.
342
+ await deadline.run((signal) => client.request('POST', createPath, createBody, signal));
343
+ const binding = config.warmPoolName !== undefined
344
+ ? await pollForBinding(async (signal) => bindingFromClaim(await client.request('GET', claimPath(namespace, objectName), undefined, signal), objectName), deadline, readiness, `claim ${objectName}`)
345
+ : await pollForBinding(async (signal) => bindingFromSandbox(await client.request('GET', sandboxPath(namespace, objectName), undefined, signal)), deadline, readiness, `sandbox ${objectName}`);
346
+ const token = await deadline.run((signal) => readPodBindToken(client, namespace, binding, signal));
347
+ return {
348
+ binding,
349
+ agent: resolveAgentAddress(binding, config.agentPort ?? DEFAULT_AGENT_PORT, token),
350
+ ownedPath,
351
+ ttlSeconds,
352
+ release,
353
+ renew,
354
+ };
355
+ }
356
+ catch (err) {
357
+ // One cleanup for every way out of the block above, on its own short
358
+ // budget: the readiness clock has already expired in the common case,
359
+ // so spending it again would either skip cleanup or leave `create()`
360
+ // pending without a bound. An object that is already gone is success.
361
+ await runFailureCleanup(async (signal) => {
362
+ await release(signal);
363
+ });
364
+ throw err;
365
+ }
366
+ }
367
+ /**
368
+ * The claim body, in full. Everything absent from it is absent on purpose:
369
+ * no `env`, no `volumeClaimTemplates`, no `additionalPodMetadata`. Either of
370
+ * the first two forces a cold start upstream and takes the warm pool away.
371
+ *
372
+ * `shutdownTime` + `shutdownPolicy: 'Delete'` is the leak guard: it bounds the
373
+ * object by the wall clock whatever the host does, so a host that dies
374
+ * mid-acquire costs the cluster one TTL rather than one leaked sandbox
375
+ * forever. `ttlSecondsAfterFinished` deliberately does NOT appear — its timer
376
+ * starts from the Finished condition, which a crashed host never reaches.
377
+ */
378
+ function buildClaimBody(namespace, name, warmPoolName, shutdownTime) {
379
+ return {
380
+ apiVersion: `${SANDBOX_EXTENSIONS_API_GROUP}/${SANDBOX_API_VERSION}`,
381
+ kind: 'SandboxClaim',
382
+ metadata: { name, namespace },
383
+ spec: {
384
+ warmPoolRef: { name: warmPoolName },
385
+ lifecycle: { shutdownTime, shutdownPolicy: 'Delete' },
386
+ },
387
+ };
388
+ }
389
+ /**
390
+ * The pool-less body. `Sandbox.spec` has no `templateRef` — only a
391
+ * SandboxWarmPool consumes a SandboxTemplate — so the template's podTemplate
392
+ * is copied in here by the client.
393
+ *
394
+ * `service: true` is forced rather than inherited: a Sandbox without a Service
395
+ * has no `status.serviceFQDN`, and then the only address left is a pod IP that
396
+ * changes on every resume.
397
+ *
398
+ * `volumeClaimTemplates` is copied VERBATIM when the template declares any.
399
+ * Dropping it would be silent: the Sandbox would come up healthy with no disk,
400
+ * the container's `volumeDevices`/`volumeMounts` entry would fail to resolve
401
+ * (or, worse, resolve to an empty emptyDir on some paths), and the only
402
+ * symptom of a workspace that lost its disk would be that yesterday's files
403
+ * are gone. The controller wires the mount by the entry's own NAME,
404
+ * StatefulSet style, so the copy needs no matching `volumes:` entry and this
405
+ * function adds none.
406
+ *
407
+ * The podTemplate's metadata gains {@link sandboxTemplateLabel}: this Sandbox
408
+ * is created DIRECTLY, never adopted out of a pool, so it never gets
409
+ * agent-sandbox's own controller-owned
410
+ * `agents.x-k8s.io/sandbox-template-ref-hash` label (that is written only on
411
+ * bind). Without a label of its own a direct Sandbox's pod would carry
412
+ * nothing `egress-policy.ts`'s translated `NetworkPolicy` could select it
413
+ * by. Existing labels on the copied template are preserved — this ADDS to
414
+ * them rather than replacing the object outright — but this backend's own
415
+ * key always wins if the template happened to set it too, since this is the
416
+ * label the translated policy is built to match.
417
+ */
418
+ export function buildSandboxBody(options) {
419
+ const podTemplate = options.template.podTemplate;
420
+ const spec = options.runtimeClassName !== undefined
421
+ ? { ...podTemplate.spec, runtimeClassName: options.runtimeClassName }
422
+ : { ...podTemplate.spec };
423
+ const metadata = {
424
+ ...podTemplate.metadata,
425
+ labels: {
426
+ ...podTemplate.metadata?.labels,
427
+ ...sandboxTemplateLabel(options.sandboxTemplateName),
428
+ },
429
+ };
430
+ return {
431
+ apiVersion: `${SANDBOX_API_GROUP}/${SANDBOX_API_VERSION}`,
432
+ kind: 'Sandbox',
433
+ metadata: { name: options.name, namespace: options.namespace },
434
+ spec: {
435
+ operatingMode: 'Running',
436
+ service: true,
437
+ ...(options.shutdownTime !== undefined
438
+ ? { shutdownTime: options.shutdownTime, shutdownPolicy: 'Delete' }
439
+ : {}),
440
+ ...(options.template.volumeClaimTemplates !== undefined
441
+ ? { volumeClaimTemplates: options.template.volumeClaimTemplates }
442
+ : {}),
443
+ podTemplate: { ...podTemplate, metadata, spec },
444
+ },
445
+ };
446
+ }
447
+ export async function readSandboxTemplate(client, namespace, sandboxTemplateName, signal) {
448
+ const template = await client.request('GET', sandboxTemplatePath(namespace, sandboxTemplateName), undefined, signal);
449
+ const podTemplate = template?.spec?.podTemplate;
450
+ if (!podTemplate || typeof podTemplate.spec !== 'object' || podTemplate.spec === null) {
451
+ throw new Error(`kubernetes: SandboxTemplate ${sandboxTemplateName} in namespace ${namespace} carries no spec.podTemplate.spec, so there is nothing to create a pool-less Sandbox from. Sandbox.spec has no templateRef — the podTemplate has to be copied in.`);
452
+ }
453
+ const volumeClaimTemplates = template?.spec?.volumeClaimTemplates;
454
+ return {
455
+ podTemplate,
456
+ ...(volumeClaimTemplates !== undefined ? { volumeClaimTemplates } : {}),
457
+ };
458
+ }
459
+ /**
460
+ * Where the agent answers.
461
+ *
462
+ * Its own function, and the Service FQDN wins over a pod IP, because the
463
+ * address outlives the pod: a suspended-then-resumed workspace comes back as a
464
+ * new pod with a new IP behind the same name, and the transport re-resolves
465
+ * the name on every dial. A literal IP baked into a long-lived handle is the
466
+ * bug that would produce.
467
+ */
468
+ export function resolveAgentAddress(binding, agentPort, token) {
469
+ const host = binding.serviceFQDN ?? binding.podIPs?.[0];
470
+ if (host === undefined || host === '') {
471
+ throw new Error(`kubernetes: sandbox ${binding.name} reported Ready with neither a serviceFQDN nor a pod IP, so its agent has no address to dial. Set 'service: true' on the SandboxTemplate the pool is built from.`);
472
+ }
473
+ return { kind: 'tcp', host, port: agentPort, token };
474
+ }
475
+ function bindingFromClaim(claim, claimName) {
476
+ if (!isConditionTrue(claim?.status?.conditions, READY_CONDITION))
477
+ return undefined;
478
+ const bound = claim?.status?.sandbox;
479
+ // Ready with no bound name is the controller contradicting itself; polling
480
+ // on would just burn the deadline waiting for a field that is finished.
481
+ if (!bound?.name) {
482
+ throw new Error(`kubernetes: SandboxClaim ${claimName} reported Ready but named no sandbox in status.sandbox.name, so there is nothing to address.`);
483
+ }
484
+ return {
485
+ name: bound.name,
486
+ ...(bound.podIPs !== undefined ? { podIPs: bound.podIPs } : {}),
487
+ ...(bound.serviceFQDN !== undefined ? { serviceFQDN: bound.serviceFQDN } : {}),
488
+ };
489
+ }
490
+ /** Ready-or-not-yet, read off a `Sandbox`'s own status. Shared with `workspace.ts`. */
491
+ export function bindingFromSandbox(sandbox) {
492
+ if (!isConditionTrue(sandbox?.status?.conditions, READY_CONDITION))
493
+ return undefined;
494
+ const name = sandbox?.metadata?.name;
495
+ if (!name) {
496
+ throw new Error('kubernetes: Sandbox reported Ready with no metadata.name');
497
+ }
498
+ const status = sandbox?.status;
499
+ return {
500
+ name,
501
+ ...(status?.podIPs !== undefined ? { podIPs: status.podIPs } : {}),
502
+ ...(status?.serviceFQDN !== undefined ? { serviceFQDN: status.serviceFQDN } : {}),
503
+ ...(status?.selector !== undefined ? { podSelector: status.selector } : {}),
504
+ };
505
+ }
506
+ /**
507
+ * Poll until `read` reports a binding. `read` returns `undefined` for "not
508
+ * yet" and throws for a failure worth surfacing; the deadline owns every wait,
509
+ * including the sleep between attempts, so an expired clock cannot be extended
510
+ * by one more round trip. Shaped after ACI's `pollForRunningIp`.
511
+ */
512
+ export async function pollForBinding(read, deadline, readiness, label) {
513
+ while (deadline.remainingMs() > 0) {
514
+ try {
515
+ const binding = await deadline.run(read);
516
+ if (binding)
517
+ return binding;
518
+ }
519
+ catch (err) {
520
+ if (err instanceof OperationDeadlineExpired)
521
+ break;
522
+ throw err;
523
+ }
524
+ try {
525
+ await deadline.delay(readiness.pollIntervalMs);
526
+ }
527
+ catch (err) {
528
+ if (err instanceof OperationDeadlineExpired)
529
+ break;
530
+ throw err;
531
+ }
532
+ }
533
+ throw new Error(`kubernetes: ${label} never became Ready (${readiness.timeoutMs}ms)`);
534
+ }
535
+ /**
536
+ * The per-instance bind token: the backing pod's `metadata.uid`.
537
+ *
538
+ * The pod is named after its Sandbox in agent-sandbox v1.0.2 — verified
539
+ * against a running cluster — but that is an observation, not a documented
540
+ * guarantee, and `Sandbox.status` exposes no pod name to fall back on. So the
541
+ * fast path is one GET by that name, and the only cost of the name convention
542
+ * changing upstream is a second round trip through `status.selector`, which is
543
+ * exactly what the controller publishes the selector for.
544
+ */
545
+ export async function readPodBindToken(client, namespace, binding, signal) {
546
+ try {
547
+ const pod = await client.request('GET', podPath(namespace, binding.name), undefined, signal);
548
+ const uid = pod?.metadata?.uid;
549
+ // `isPodLive` matters on the RESUME path in `workspace.ts`: a resumed
550
+ // pod keeps its name, so for as long as the outgoing one is
551
+ // terminating this GET can answer with the pod that is leaving and a
552
+ // uid the new agent will refuse. On the acquire path nothing is
553
+ // terminating and the filter never fires.
554
+ if (uid && isPodLive(pod))
555
+ return uid;
556
+ }
557
+ catch (err) {
558
+ if (!(err instanceof KubernetesAlreadyGoneError))
559
+ throw err;
560
+ }
561
+ const selector = binding.podSelector ?? (await readSandboxSelector(client, namespace, binding, signal));
562
+ if (selector !== undefined && selector !== '') {
563
+ const list = await client.request('GET', podListPath(namespace, selector), undefined, signal);
564
+ for (const pod of list?.items ?? []) {
565
+ const uid = pod.metadata?.uid;
566
+ if (uid && isPodLive(pod))
567
+ return uid;
568
+ }
569
+ }
570
+ throw new Error(`kubernetes: could not read a pod uid for sandbox ${binding.name} in namespace ${namespace} — no live pod of that name, and its status.selector matched no live pod either (a pod carrying a deletionTimestamp, or in phase Succeeded/Failed, is never bound to). The pod uid is the agent's bind token, so the sandbox is refused rather than returned unauthenticated.`);
571
+ }
572
+ async function readSandboxSelector(client, namespace, binding, signal) {
573
+ try {
574
+ const sandbox = await client.request('GET', sandboxPath(namespace, binding.name), undefined, signal);
575
+ return sandbox?.status?.selector;
576
+ }
577
+ catch (err) {
578
+ if (err instanceof KubernetesAlreadyGoneError)
579
+ return undefined;
580
+ throw err;
581
+ }
582
+ }
583
+ /**
584
+ * Build the Sandbox, prove it is deprivileged, and only then hand it back.
585
+ *
586
+ * The probe runs BEFORE `create()` resolves, so a caller never holds a
587
+ * reference to an under-hardened sandbox — a probe that refuses destroys the
588
+ * instance on a bounded cleanup budget and rethrows, exactly as a readiness
589
+ * failure does. There is no configuration that skips it: the whole value of
590
+ * checking the deprivileging on every acquire rather than once by hand is
591
+ * that it cannot be forgotten, and an off switch is a way to forget it.
592
+ *
593
+ * The probe goes through the Sandbox's own `exec`, not the raw transport, so
594
+ * it traverses the same reserve/admit/stream/confirm path every later call
595
+ * will. A sandbox that cannot answer the probe is one a caller could not use
596
+ * either.
597
+ *
598
+ * ## And it runs on a clock
599
+ *
600
+ * This is the first thing on the acquire path that talks to the GUEST, and
601
+ * the readiness deadline that bounded everything before it has already
602
+ * expired. Left unbounded the probe would inherit the execution controller's
603
+ * generic defaults instead — a five-minute observation, then a cancel-confirm
604
+ * and a drain — so a pod whose agent has wedged (out of memory, an event loop
605
+ * the workload blocked) would keep `create()` pending for minutes past the
606
+ * caller's `readyTimeoutMs` with nothing reported. It gets its own deadline,
607
+ * whose expiry takes the same cleanup-and-reject path every other refusal
608
+ * does, in words that name the hang rather than blame a missing `cat`.
609
+ */
610
+ async function admitProbedSandbox(acquisition, config, options, probeTimeoutMs) {
611
+ const sandbox = buildKubernetesSandbox({
612
+ name: acquisition.binding.name,
613
+ rootDir: options.workingDirectory,
614
+ transport: new KubernetesAgentTransport(acquisition.agent),
615
+ release: acquisition.release,
616
+ renew: acquisition.renew,
617
+ ttlSeconds: acquisition.ttlSeconds,
618
+ ...(config.onLeaseRenewalError !== undefined
619
+ ? { onRenewalError: config.onLeaseRenewalError }
620
+ : {}),
621
+ });
622
+ try {
623
+ await probeSandboxPrivileges(sandbox, acquisition.binding.name, probeTimeoutMs, options.signal);
624
+ }
625
+ catch (err) {
626
+ await runFailureCleanup(async (signal) => {
627
+ await sandbox.destroy({ signal });
628
+ });
629
+ throw err;
630
+ }
631
+ return sandbox;
632
+ }
633
+ /**
634
+ * Run the probe against a built Sandbox, on its own clock, and throw if it
635
+ * refuses. Cleanup is the CALLER's, and the two callers want opposite things:
636
+ * a task acquire destroys the instance, while `workspace.ts` suspends it,
637
+ * because deleting a workspace deletes its disk and a probe refusal is not a
638
+ * reason to lose a caller's files.
639
+ */
640
+ export async function probeSandboxPrivileges(sandbox, sandboxName, probeTimeoutMs, signal) {
641
+ // Labelled with the sandbox, so an expiry read off a log line says which
642
+ // acquire stopped answering — and so the catch below can tell THIS
643
+ // deadline from any other that might surface through the same exec.
644
+ const probeLabel = `kubernetes privilege probe ${sandboxName}`;
645
+ try {
646
+ signal?.throwIfAborted();
647
+ // The deadline's signal covers both ways this should stop early: it
648
+ // aborts on expiry, and it aborts with the caller's own reason when
649
+ // `signal` does. Handing it to `exec` is what releases the guest-side
650
+ // execution rather than merely abandoning the wait.
651
+ await new OperationDeadline(probeTimeoutMs, probeLabel, signal).run(async (execSignal) => await runPrivilegeProbe(async (command, args) => await sandbox.exec(command, args, { signal: execSignal }), sandboxName));
652
+ // An abort that lands WHILE the probe is in flight must not leave a
653
+ // live sandbox behind: the probe itself may well have finished, and
654
+ // the caller who cancelled is about to stop holding the reference
655
+ // that could destroy it. Same cleanup, one branch later.
656
+ signal?.throwIfAborted();
657
+ }
658
+ catch (err) {
659
+ // A caller who cancelled mid-probe gets THEIR reason, not the probe's
660
+ // account of a command that was cancelled out from under it.
661
+ signal?.throwIfAborted();
662
+ // A probe that ran out of time is a probe that could not run, and is
663
+ // refused in those words: `OperationDeadlineExpired` on its own would
664
+ // leave a reader guessing which half of the acquire went quiet.
665
+ if (err instanceof OperationDeadlineExpired && err.label === probeLabel) {
666
+ throw privilegeProbeTimedOut(sandboxName, probeTimeoutMs, err);
667
+ }
668
+ throw err;
669
+ }
670
+ }
671
+ //# sourceMappingURL=index.js.map