@getenergy/gke-sandboxes 0.4.1 → 0.6.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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { type ExecOptions, type KubernetesApi } from './kubernetes-api.js';
3
+ export { CommandServerUnreachableError } from './command-channel.js';
3
4
  export { KubernetesApi, KubernetesApiError, type ExecOptions } from './kubernetes-api.js';
4
5
  export type SandboxEnvironment = {
5
6
  id: string;
@@ -18,7 +19,7 @@ export type SandboxEnvironment = {
18
19
  };
19
20
  type Labels = Record<string, string>;
20
21
  export type SandboxEvent = {
21
- operation: 'create' | 'wake' | 'sleep' | 'checkpoint';
22
+ operation: 'create' | 'wake' | 'sleep' | 'checkpoint' | 'snapshot';
22
23
  outcome: 'succeeded' | 'aborted' | 'failed';
23
24
  labels: Labels;
24
25
  pod?: string;
@@ -64,13 +65,40 @@ export type GkeSandboxOptions = {
64
65
  memoryWatchdog?: false | {
65
66
  protect?: string[];
66
67
  };
68
+ /**
69
+ * Starts a service once the filesystem is ready: as a sandbox's first pod starts, as a woken pod
70
+ * starts, and when a warm pool hands out a sandbox, whose pod started before its labels were
71
+ * known.
72
+ */
67
73
  boot?: (labels: Labels) => {
68
74
  shell: string;
69
75
  args: string[];
70
76
  };
77
+ /**
78
+ * Linux capabilities to add to the sandbox's container, such as `['NET_ADMIN', 'SYS_ADMIN']`
79
+ * to run Docker inside it. They apply inside gVisor's own kernel, not the node's. The
80
+ * quickstart's namespace enforces the `baseline` Pod Security level, which refuses most of them:
81
+ * deploy with `TF_VAR_pod_security_level=privileged` to use them.
82
+ */
83
+ capabilities?: string[];
84
+ /**
85
+ * Runs commands and moves files over a connection straight to the sandbox, on this port,
86
+ * instead of through the Kubernetes API. A command starts several times sooner and does not
87
+ * queue behind other API calls on a busy cluster. The caller must be able to reach pod
88
+ * addresses: on the cluster's network, and allowed in on this port by the sandboxes' network
89
+ * policy. Each sandbox started with this set runs a small command server that only accepts
90
+ * its own random token. Sandboxes started without it are still reached through the API.
91
+ */
92
+ directCommands?: {
93
+ port: number;
94
+ };
71
95
  drain?: (sandbox: SandboxEnvironment) => Promise<boolean>;
72
96
  onOperation?: (event: SandboxEvent) => void;
73
- onPodLost?: (labels: Labels, phase: string, reason: string, hasSnapshot: boolean) => void;
97
+ /** The recovery point is empty if the selected snapshot has no usable timestamp. */
98
+ onPodLost?: (labels: Labels, phase: string, reason: string, hasSnapshot: boolean, recoveryPoint: {
99
+ recoveryPointAt?: string;
100
+ recoveryPointAgeMs?: number;
101
+ }) => void;
74
102
  };
75
103
  declare const PodSchema: z.ZodObject<{
76
104
  metadata: z.ZodObject<{
@@ -125,23 +153,80 @@ export declare class GkeSandboxes {
125
153
  private readonly manager;
126
154
  private readonly idleTimeoutMs;
127
155
  private readonly diskBytes;
156
+ private readonly configMaps;
157
+ private commandServer;
158
+ private readonly adopting;
128
159
  constructor(kubernetes: KubernetesApi, options: GkeSandboxOptions);
129
160
  private selector;
130
- acquire(id: string, signal: AbortSignal, initialLabels?: Labels): Promise<{
161
+ /**
162
+ * Returns the sandbox's running pod, waking it from its newest snapshot or creating it. With
163
+ * `from`, a sandbox that does not exist yet starts from that snapshot, which may be another
164
+ * sandbox's (see `snapshot`).
165
+ */
166
+ acquire(id: string, signal: AbortSignal, initialLabels?: Labels, { from }?: {
167
+ from?: string;
168
+ }): Promise<{
131
169
  sandbox: SandboxEnvironment;
132
170
  startState: string;
133
- labels: Record<string, string>;
171
+ labels: {
172
+ [x: string]: string;
173
+ };
134
174
  podName: string;
135
175
  createdMs: number;
136
176
  touch: () => Promise<void>;
137
177
  }>;
138
- /** Creates single-use sandboxes with generated IDs. Reopen a saved ID with acquire(). */
178
+ private acquireFound;
179
+ private forkSource;
180
+ /**
181
+ * Running sandboxes kept ready, so that a new one starts without waiting for a pod. A sandbox is
182
+ * handed out by creating a Lease named after its pod, which GKE's admission webhooks do not
183
+ * check: claims stay fast when many arrive at once. The pod is labelled as the sandbox it became
184
+ * afterwards; until then the Lease carries its labels, and lookups read them from there.
185
+ */
139
186
  warmPool(name: string): {
140
- create: (signal: AbortSignal, labels?: Labels) => Promise<{
187
+ /**
188
+ * Returns the sandbox `id`, from the pool if it does not exist yet. An existing sandbox is
189
+ * acquired as `GkeSandboxes.acquire` does; an empty pool starts a new pod. Use the same pool
190
+ * for an id every time.
191
+ */
192
+ acquire(id: string, signal: AbortSignal, labels?: Labels): Promise<{
193
+ sandbox: SandboxEnvironment;
194
+ labels: {
195
+ "energy-sandbox": string;
196
+ "energy-created-ms": string;
197
+ };
198
+ podName: string;
199
+ createdMs: number;
200
+ touch: () => Promise<void>;
201
+ startState: 'missing';
202
+ fromPool: boolean;
203
+ } | {
204
+ sandbox: SandboxEnvironment;
205
+ startState: string;
206
+ labels: {
207
+ [x: string]: string;
208
+ };
209
+ podName: string;
210
+ createdMs: number;
211
+ touch: () => Promise<void>;
212
+ fromPool: boolean;
213
+ }>;
214
+ /** Hands out a sandbox with a generated ID. Reopen it later with `acquire`. */
215
+ create(signal: AbortSignal, labels?: Labels): Promise<{
141
216
  sandbox: SandboxEnvironment;
217
+ labels: {
218
+ "energy-sandbox": string;
219
+ "energy-created-ms": string;
220
+ };
142
221
  podName: string;
222
+ createdMs: number;
223
+ touch: () => Promise<void>;
143
224
  fromPool: boolean;
225
+ } | {
226
+ sandbox: SandboxEnvironment;
227
+ podName: string;
144
228
  touch: () => Promise<void>;
229
+ fromPool: boolean;
145
230
  }>;
146
231
  reconcile: (size: number) => Promise<void>;
147
232
  maintain(size: number, signal: AbortSignal): Promise<void>;
@@ -163,6 +248,13 @@ export declare class GkeSandboxes {
163
248
  * written since. The sandbox is paused while the snapshot is taken.
164
249
  */
165
250
  checkpoint(sandboxId: string): Promise<void>;
251
+ /**
252
+ * Snapshots a running sandbox and keeps the snapshot until `deleteSnapshot`; no later checkpoint
253
+ * or sleep removes it. The sandbox runs on. Returns the snapshot's name: pass it as `from` to
254
+ * `acquire` to start new sandboxes with these files, as many as needed.
255
+ */
256
+ snapshot(sandboxId: string): Promise<string>;
257
+ deleteSnapshot(name: string): Promise<void>;
166
258
  sleepUnused(): Promise<void>;
167
259
  /**
168
260
  * Every sandbox that exists, awake or asleep, with whose it is and where its snapshots are
@@ -182,22 +274,56 @@ export declare class GkeSandboxes {
182
274
  /** Every sandbox with these labels, awake or asleep, newest first. */
183
275
  list(labels: Record<string, string>): Promise<SandboxRecord[]>;
184
276
  private livePods;
277
+ /**
278
+ * The pod a sandbox's id record names, with the sandbox's labels from the record if it is a pool
279
+ * pod that does not carry them yet. It keeps its pool label until it does, which is how a
280
+ * snapshot knows to label it first.
281
+ */
282
+ private claimedPod;
283
+ /**
284
+ * Records that a pool pod was handed out as the sandbox `id`. Returns false when another caller
285
+ * created `id` first. The record goes with the pod.
286
+ */
287
+ private recordHandOut;
288
+ /**
289
+ * Records that the new sandbox `id` is the pod `podName`'s, before that pod is created. Returns
290
+ * null when it is, or the pool pod another caller already handed out as `id`, which is then this
291
+ * caller's too. A record whose pod is gone or has failed is replaced.
292
+ */
293
+ private takeIdentity;
294
+ /**
295
+ * Labels a pool pod as the sandbox it was handed out as. Only labels the pod lacks are added, so
296
+ * labels set on it since it was handed out stay. Callers in this process take turns.
297
+ */
298
+ private adopt;
299
+ /** Runs a command in a pod, through its command server if it has one. */
300
+ private runIn;
185
301
  /** The sandbox's pod to use: the one that is not shutting down, or null. */
186
302
  private livePod;
187
303
  /** Returns the sandbox's running pod, waking or creating it first. */
188
304
  private start;
189
305
  private report;
190
306
  private confirmRestored;
307
+ /** Where the pod's command server is, or null for a pod that is reached through the API. */
308
+ private commandAddress;
309
+ /** Creates a pod, after the command server it mounts, once per process. */
310
+ private createPod;
191
311
  private manifest;
192
312
  private claim;
193
313
  /** Records the dispatch. Returns false when the sweep claimed the pod first. */
194
314
  private markAcquired;
195
315
  /** Reports that the sandbox is in use, which restarts its idle period. */
196
316
  private touch;
197
- sleep(id: string): Promise<void>;
317
+ /**
318
+ * Puts a sandbox to sleep once nothing has used it for the idle period. With `now`, it sleeps
319
+ * at once whenever it was last used, which is how a caller pauses it; `drain` is still asked.
320
+ */
321
+ sleep(id: string, { now }?: {
322
+ now?: boolean;
323
+ }): Promise<void>;
198
324
  private sleepPod;
199
325
  private dropOlderSnapshots;
200
- private snapshot;
326
+ private takeSnapshot;
201
327
  delete(id: string): Promise<void>;
202
328
  environment(name: string, state: 'started' | 'stopped'): SandboxEnvironment;
203
329
  private makeEnvironment;