@getenergy/gke-sandboxes 0.4.0 → 0.4.1
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 +1 -0
- package/dist/index.js +46 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -162,6 +162,13 @@ const StoredSnapshotSchema = z.object({
|
|
|
162
162
|
storageStatus: z.object({ gcs: z.object({ observedGCSPath: z.string().startsWith('gs://') }) }),
|
|
163
163
|
}),
|
|
164
164
|
});
|
|
165
|
+
// A sandbox's keep-alive. Reports of use renew it instead of writing to the pod: GKE checks every
|
|
166
|
+
// pod write with an admission webhook, which is several times slower and queues under load.
|
|
167
|
+
const LeaseSchema = z.object({
|
|
168
|
+
metadata: z.object({ name: z.string() }),
|
|
169
|
+
spec: z.object({ renewTime: z.string().optional() }).default({}),
|
|
170
|
+
});
|
|
171
|
+
const renewedMs = (lease) => lease ? Date.parse(LeaseSchema.parse(lease).spec.renewTime ?? '') || 0 : 0;
|
|
165
172
|
const TriggerSchema = z.object({
|
|
166
173
|
status: z
|
|
167
174
|
.object({
|
|
@@ -205,6 +212,7 @@ export class GkeSandboxes {
|
|
|
205
212
|
kubernetes;
|
|
206
213
|
options;
|
|
207
214
|
pods;
|
|
215
|
+
leases;
|
|
208
216
|
triggers;
|
|
209
217
|
snapshotsPath;
|
|
210
218
|
manager;
|
|
@@ -232,6 +240,7 @@ export class GkeSandboxes {
|
|
|
232
240
|
this.idleTimeoutMs <= 0)
|
|
233
241
|
throw new Error('A manager label and a positive idle timeout are required.');
|
|
234
242
|
this.pods = `/api/v1/namespaces/${namespace}/pods`;
|
|
243
|
+
this.leases = `/apis/coordination.k8s.io/v1/namespaces/${namespace}/leases`;
|
|
235
244
|
const api = `/apis/podsnapshot.gke.io/v1/namespaces/${namespace}`;
|
|
236
245
|
this.triggers = `${api}/podsnapshotmanualtriggers`;
|
|
237
246
|
this.snapshotsPath = `${api}/podsnapshots`;
|
|
@@ -466,13 +475,22 @@ export class GkeSandboxes {
|
|
|
466
475
|
}
|
|
467
476
|
async sleepUnused() {
|
|
468
477
|
const idleBefore = Date.now() - this.idleTimeoutMs;
|
|
478
|
+
const [sandboxes, leases] = await Promise.all([
|
|
479
|
+
this.list({}),
|
|
480
|
+
this.kubernetes.request('GET', `${this.leases}?labelSelector=${this.selector({})}`),
|
|
481
|
+
]);
|
|
482
|
+
const renewed = new Map(listOf(LeaseSchema)
|
|
483
|
+
.parse(leases)
|
|
484
|
+
.items.map((lease) => [lease.metadata.name, renewedMs(lease)]));
|
|
469
485
|
// All at once: a sleep waits on its snapshot, and one request sweeps the whole cluster, so
|
|
470
486
|
// sleeping in turn reaches only the first few of many idle sandboxes before its deadline.
|
|
471
|
-
await Promise.all(
|
|
487
|
+
await Promise.all(sandboxes.map(async ({ pod }) => {
|
|
472
488
|
if (!pod || sleeping(pod) || pod.status.phase !== 'Running')
|
|
473
489
|
return;
|
|
474
490
|
if (Number(pod.metadata.annotations[ACQUIRED_ANNOTATION]) > idleBefore)
|
|
475
491
|
return;
|
|
492
|
+
if ((renewed.get(pod.metadata.name) ?? 0) > idleBefore)
|
|
493
|
+
return;
|
|
476
494
|
// One sandbox that cannot sleep must not keep the rest awake; the next sweep retries it.
|
|
477
495
|
// A failed sleep has been reported, and one in use is no failure.
|
|
478
496
|
await this.sleepPod(pod).catch(() => undefined);
|
|
@@ -797,9 +815,29 @@ export class GkeSandboxes {
|
|
|
797
815
|
const pod = await this.livePod(sandboxId);
|
|
798
816
|
if (!pod)
|
|
799
817
|
return;
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
}
|
|
818
|
+
const { name, uid } = pod.metadata;
|
|
819
|
+
// Kubernetes reads this time only with six decimals.
|
|
820
|
+
const spec = { renewTime: new Date().toISOString().replace('Z', '000Z') };
|
|
821
|
+
if (await this.kubernetes.request('PATCH', `${this.leases}/${name}`, { spec }))
|
|
822
|
+
return;
|
|
823
|
+
// The pod owns its lease, so Kubernetes deletes the lease with the pod.
|
|
824
|
+
try {
|
|
825
|
+
await this.kubernetes.request('POST', this.leases, {
|
|
826
|
+
apiVersion: 'coordination.k8s.io/v1',
|
|
827
|
+
kind: 'Lease',
|
|
828
|
+
metadata: {
|
|
829
|
+
name,
|
|
830
|
+
labels: this.manager,
|
|
831
|
+
ownerReferences: [{ apiVersion: 'v1', kind: 'Pod', name, uid }],
|
|
832
|
+
},
|
|
833
|
+
spec,
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
catch (error) {
|
|
837
|
+
// Another report created it at the same moment, with the same time.
|
|
838
|
+
if (!(error instanceof KubernetesApiError && error.status === 409))
|
|
839
|
+
throw error;
|
|
840
|
+
}
|
|
803
841
|
}
|
|
804
842
|
async sleep(id) {
|
|
805
843
|
const pod = await this.livePod(id);
|
|
@@ -824,6 +862,10 @@ export class GkeSandboxes {
|
|
|
824
862
|
throw new SandboxInUseError(name);
|
|
825
863
|
const startedAt = Date.now();
|
|
826
864
|
try {
|
|
865
|
+
// Read after the claim, so a report of use made while the sweep was deciding still counts.
|
|
866
|
+
const renewed = renewedMs(await this.kubernetes.request('GET', `${this.leases}/${pod.metadata.name}`));
|
|
867
|
+
if (Date.now() - renewed < this.idleTimeoutMs)
|
|
868
|
+
throw new SandboxInUseError(name);
|
|
827
869
|
if (this.options.drain && !(await this.options.drain(this.environment(name, 'started'))))
|
|
828
870
|
throw new SandboxInUseError(name);
|
|
829
871
|
const snapshot = await this.snapshot(pod);
|