@getenergy/gke-sandboxes 0.3.1 → 0.4.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
@@ -36,6 +36,7 @@ export type SandboxResources = {
36
36
  limits: ResourceList;
37
37
  };
38
38
  export declare const DEFAULT_RESOURCES: SandboxResources;
39
+ export declare const DEFAULT_DISK = "20Gi";
39
40
  export type GkeSandboxOptions = {
40
41
  namespace: string;
41
42
  image: string;
@@ -48,6 +49,13 @@ export type GkeSandboxOptions = {
48
49
  };
49
50
  /** What a sandbox reserves on its node and may use at most. Defaults to `DEFAULT_RESOURCES`. */
50
51
  resources?: SandboxResources;
52
+ /**
53
+ * The size of a sandbox's root filesystem, `DEFAULT_DISK` unless given. `df` reports it and a
54
+ * write past it fails. It must be below the `ephemeral-storage` limit: the difference is what a
55
+ * full sandbox may still hold in /tmp before it is evicted. A sleeping sandbox holding more than
56
+ * this does not wake.
57
+ */
58
+ disk?: string;
51
59
  /**
52
60
  * A sandbox that reaches its memory limit is killed whole. By default each sandbox watches its
53
61
  * own memory and ends its largest processes first. `protect` lists command-line fragments of
@@ -115,6 +123,7 @@ export declare class GkeSandboxes {
115
123
  private readonly snapshotsPath;
116
124
  private readonly manager;
117
125
  private readonly idleTimeoutMs;
126
+ private readonly diskBytes;
118
127
  constructor(kubernetes: KubernetesApi, options: GkeSandboxOptions);
119
128
  private selector;
120
129
  acquire(id: string, signal: AbortSignal, initialLabels?: Labels): Promise<{
package/dist/index.js CHANGED
@@ -7,13 +7,14 @@ import { z } from 'zod';
7
7
  import { KubernetesApiError } from './kubernetes-api.js';
8
8
  import { MEMORY_WATCHDOG } from './memory-watchdog.js';
9
9
  export { KubernetesApi, KubernetesApiError } from './kubernetes-api.js';
10
- // Sandboxes are mostly idle, so nodes are packed by the request, not by the limit. The disk limit
11
- // keeps one sandbox from filling its node. A pod that exceeds it is evicted, not refused the
12
- // write, and comes back from its last snapshot.
10
+ // Sandboxes are mostly idle, so nodes are packed by the request, not by the limit. The kubelet
11
+ // counts a sandbox's root filesystem, scratch /tmp and logs against the disk limit and evicts a
12
+ // pod that exceeds it, so the limit is what one sandbox may take of its node's disk.
13
13
  export const DEFAULT_RESOURCES = {
14
14
  requests: { cpu: '250m', memory: '1Gi', 'ephemeral-storage': '4Gi' },
15
- limits: { cpu: '2', memory: '4Gi', 'ephemeral-storage': '20Gi' },
15
+ limits: { cpu: '2', memory: '4Gi', 'ephemeral-storage': '40Gi' },
16
16
  };
17
+ export const DEFAULT_DISK = '20Gi';
17
18
  const SANDBOX_LABEL = 'energy-sandbox';
18
19
  const CREATED_LABEL = 'energy-created-ms';
19
20
  const POOL_LABEL = 'getenergy.com/warm-pool';
@@ -29,6 +30,23 @@ const RESTORE_ANNOTATION = 'podsnapshot.gke.io/ps-name';
29
30
  // this rate, about a 16 Gbps node's share for one of 64 sandboxes. No sandbox crowds out its
30
31
  // neighbours, and one turned against a host can send it no more than this.
31
32
  const EGRESS_BANDWIDTH = '250M';
33
+ const QUANTITY_UNITS = {
34
+ Ki: 2 ** 10,
35
+ Mi: 2 ** 20,
36
+ Gi: 2 ** 30,
37
+ Ti: 2 ** 40,
38
+ k: 1e3,
39
+ M: 1e6,
40
+ G: 1e9,
41
+ T: 1e12,
42
+ };
43
+ // gVisor takes a size in bytes, not a Kubernetes quantity.
44
+ const quantityBytes = (quantity) => {
45
+ const [, amount, unit] = /^(\d+(?:\.\d+)?)(Ki|Mi|Gi|Ti|k|M|G|T)?$/.exec(quantity) ?? [];
46
+ if (!amount)
47
+ throw new Error(`Unsupported disk size: ${quantity}`);
48
+ return Math.floor(Number(amount) * (unit ? QUANTITY_UNITS[unit] : 1));
49
+ };
32
50
  // Written by a sandbox's first pod as it starts. A woken pod without it was not restored from its
33
51
  // snapshot.
34
52
  const SANDBOX_ID_FILE = '/var/tmp/.energy-sandbox-id';
@@ -191,6 +209,7 @@ export class GkeSandboxes {
191
209
  snapshotsPath;
192
210
  manager;
193
211
  idleTimeoutMs;
212
+ diskBytes;
194
213
  constructor(kubernetes, options) {
195
214
  this.kubernetes = kubernetes;
196
215
  this.options = options;
@@ -203,6 +222,11 @@ export class GkeSandboxes {
203
222
  if ([SANDBOX_LABEL, CREATED_LABEL, POOL_LABEL, TEMPLATE_LABEL].some((key) => key in this.manager))
204
223
  throw new Error('A manager selector cannot use reserved workspace labels.');
205
224
  this.idleTimeoutMs = options.idleTimeoutMs ?? 300_000;
225
+ const disk = options.disk ?? DEFAULT_DISK;
226
+ const limit = (options.resources ?? DEFAULT_RESOURCES).limits['ephemeral-storage'];
227
+ this.diskBytes = quantityBytes(disk);
228
+ if (this.diskBytes >= quantityBytes(limit))
229
+ throw new Error(`The disk (${disk}) must be below the ephemeral-storage limit (${limit}), which also covers /tmp and logs.`);
206
230
  if (!Object.keys(this.manager).length ||
207
231
  !Number.isFinite(this.idleTimeoutMs) ||
208
232
  this.idleTimeoutMs <= 0)
@@ -694,6 +718,10 @@ export class GkeSandboxes {
694
718
  [ACQUIRED_ANNOTATION]: String(Date.now()),
695
719
  [SLEEPING_ANNOTATION]: '0',
696
720
  'kubernetes.io/egress-bandwidth': EGRESS_BANDWIDTH,
721
+ // gVisor gives the root filesystem this size. Without it the filesystem has none: `df`
722
+ // reports 8 EiB free, and the only limit is the kubelet's, which evicts the pod. gVisor
723
+ // cannot size the scratch volume at /tmp, so the kubelet's limit still bounds that.
724
+ 'dev.gvisor.flag.overlay2': `root:self,size=${this.diskBytes}`,
697
725
  ...(snapshot ? { [RESTORE_ANNOTATION]: snapshot } : {}),
698
726
  },
699
727
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getenergy/gke-sandboxes",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {