hammerkit 2.1.0-next.12 → 2.1.0-next.14
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/harness/connect.js +10 -7
- package/dist/harness/reconnect.js +137 -0
- package/dist/harness/sandbox-client.js +110 -25
- package/dist/remote/remote-work-commands.js +19 -8
- package/dist/server/executor.js +22 -5
- package/dist/server/http.js +4 -1
- package/dist/server/kubernetes-backend.js +155 -52
- package/dist/server/run-logs.js +25 -1
- package/dist/server/runs.js +339 -96
- package/dist/server/server.js +9 -4
- package/package.json +1 -1
|
@@ -19,6 +19,7 @@ const version_1 = require("../version");
|
|
|
19
19
|
const executor_1 = require("./executor");
|
|
20
20
|
const kept_claims_1 = require("../kubernetes/kept-claims");
|
|
21
21
|
const units_1 = require("../utils/units");
|
|
22
|
+
const runs_1 = require("./runs");
|
|
22
23
|
const server_config_1 = require("./server-config");
|
|
23
24
|
// the name of the quota and limit range the server keeps in a caller's namespace
|
|
24
25
|
const POLICY_NAME = 'hammerkit';
|
|
@@ -270,8 +271,11 @@ class KubernetesExecutor {
|
|
|
270
271
|
// per caller namespace, what changes objects of the whole namespace: its setup, the
|
|
271
272
|
// removal of what a stopped server left, the sweeps of kept claims. The last one waiting.
|
|
272
273
|
this.queues = new Map();
|
|
273
|
-
// per caller namespace, the runs that execute in it
|
|
274
|
-
//
|
|
274
|
+
// per caller namespace, the runs that execute in it: by run label, the volume scope of the
|
|
275
|
+
// kept claims they mount, if any. Every run that executes is here, not only those that mount
|
|
276
|
+
// kept claims: a tidy of the namespace (after a release that failed, or the first after a
|
|
277
|
+
// restart) leaves the objects of all of them, a resumed run's too once it no longer counts
|
|
278
|
+
// as resuming
|
|
275
279
|
this.active = new Map();
|
|
276
280
|
// per caller namespace, when its kept claims are next looked at
|
|
277
281
|
this.sweeps = new Map();
|
|
@@ -285,6 +289,9 @@ class KubernetesExecutor {
|
|
|
285
289
|
this.serviceRuns = new Map();
|
|
286
290
|
this.serviceRunsSwept = false;
|
|
287
291
|
this.asleep = new Set();
|
|
292
|
+
// the runs whose execution got its place on the cluster, until it ended: their release
|
|
293
|
+
// removes what they made
|
|
294
|
+
this.placed = new Set();
|
|
288
295
|
this.access = getClusterAccess(config.backend, processEnvs);
|
|
289
296
|
this.instance = (0, kubernetes_instance_1.createKubernetesInstances)({
|
|
290
297
|
type: 'kubernetes',
|
|
@@ -316,6 +323,7 @@ class KubernetesExecutor {
|
|
|
316
323
|
log: options.log,
|
|
317
324
|
});
|
|
318
325
|
}
|
|
326
|
+
this.resuming = new ResumingRuns(config.backend.namespacePrefix, (run) => this.namespaceOf(run));
|
|
319
327
|
this.host = new executor_1.HostExecutor({ ...options, placement: this.placement() });
|
|
320
328
|
}
|
|
321
329
|
// what the namespaces of the server's callers and service runs request and use
|
|
@@ -345,6 +353,38 @@ class KubernetesExecutor {
|
|
|
345
353
|
});
|
|
346
354
|
}
|
|
347
355
|
async execute(run, context) {
|
|
356
|
+
// what a run that resumes left is this execution's from now: the end of a run that got its
|
|
357
|
+
// place on the cluster (release) removes it, the end of one that did not removes it here
|
|
358
|
+
const left = this.resuming.has(run);
|
|
359
|
+
try {
|
|
360
|
+
await this.executeOn(run, context);
|
|
361
|
+
}
|
|
362
|
+
finally {
|
|
363
|
+
const placed = this.placed.delete(run.id);
|
|
364
|
+
if (left && !placed && !(0, runs_1.isDetached)(context)) {
|
|
365
|
+
await this.removeLeft(run);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// What a run left on the cluster, that no release removes
|
|
370
|
+
async removeLeft(run) {
|
|
371
|
+
const say = (text) => this.log?.(`run=${run.id} ${text}`);
|
|
372
|
+
this.resuming.forget(run);
|
|
373
|
+
try {
|
|
374
|
+
if (!run.up) {
|
|
375
|
+
await this.removeRun(run, getCallerNamespace(this.config.backend.namespacePrefix, run.owner), say);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
// a namespace is kept only for a run that has one from start to end
|
|
379
|
+
const namespace = this.namespaceOf(run);
|
|
380
|
+
this.asleep.delete(namespace);
|
|
381
|
+
await this.enqueue(namespace, () => this.removeServiceRun(namespace, say));
|
|
382
|
+
}
|
|
383
|
+
catch (e) {
|
|
384
|
+
say(`what the run left could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async executeOn(run, context) {
|
|
348
388
|
if (run.up) {
|
|
349
389
|
if (run.source.type === 'sandbox') {
|
|
350
390
|
throw new Error('on a cluster a service run starts from a branch, push the sandbox and start it from there');
|
|
@@ -352,6 +392,8 @@ class KubernetesExecutor {
|
|
|
352
392
|
// in a namespace of its own, apart from the caller's runs and their claims
|
|
353
393
|
return this.host.execute(run, context);
|
|
354
394
|
}
|
|
395
|
+
// it executes: its objects are kept from a tidy as those of any run that executes
|
|
396
|
+
this.resuming.forget(run);
|
|
355
397
|
const namespace = getCallerNamespace(this.config.backend.namespacePrefix, run.owner);
|
|
356
398
|
const label = (0, job_placement_1.getRunLabelValue)(run.id);
|
|
357
399
|
let runs = this.active.get(namespace);
|
|
@@ -359,9 +401,7 @@ class KubernetesExecutor {
|
|
|
359
401
|
runs = new Map();
|
|
360
402
|
this.active.set(namespace, runs);
|
|
361
403
|
}
|
|
362
|
-
|
|
363
|
-
runs.set(label, getVolumeScope(run));
|
|
364
|
-
}
|
|
404
|
+
runs.set(label, this.holds(run) ? getVolumeScope(run) : undefined);
|
|
365
405
|
// the caller is promised its share while the run executes
|
|
366
406
|
this.quotas?.enter(namespace, run.id);
|
|
367
407
|
try {
|
|
@@ -372,9 +412,12 @@ class KubernetesExecutor {
|
|
|
372
412
|
if (runs.size === 0 && this.active.get(namespace) === runs) {
|
|
373
413
|
this.active.delete(namespace);
|
|
374
414
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
415
|
+
// a run the next server resumes still has its Jobs, under the quota it had
|
|
416
|
+
if (!(0, runs_1.isDetached)(context)) {
|
|
417
|
+
await this.quotas
|
|
418
|
+
?.leave(namespace, run.id)
|
|
419
|
+
.catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
|
|
420
|
+
}
|
|
378
421
|
}
|
|
379
422
|
}
|
|
380
423
|
// A service run moves to another commit in its namespace: unchanged services stay, the
|
|
@@ -390,25 +433,31 @@ class KubernetesExecutor {
|
|
|
390
433
|
}
|
|
391
434
|
/**
|
|
392
435
|
* A service run that slept with its volumes ended without waking: its namespace goes, with
|
|
393
|
-
* the claims in it.
|
|
436
|
+
* the claims in it. A run that resumed after a restart ended: what an execution before the
|
|
437
|
+
* restart left goes, as no execution may have removed it (one cancelled in the queue).
|
|
394
438
|
*/
|
|
395
439
|
async forget(run) {
|
|
396
|
-
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
// a namespace is kept only for a run that has one from start to end
|
|
400
|
-
const namespace = getServiceRunNamespace(this.config.backend.namespacePrefix, run.id);
|
|
401
|
-
this.asleep.delete(namespace);
|
|
402
|
-
await this.enqueue(namespace, () => this.removeServiceRun(namespace, (text) => this.log?.(`run=${run.id} ${text}`)));
|
|
440
|
+
await this.removeLeft(run);
|
|
403
441
|
}
|
|
404
442
|
/**
|
|
405
|
-
* The
|
|
406
|
-
* without what runs in them (a server that stopped without
|
|
407
|
-
* it)
|
|
443
|
+
* The runs of a previous start that are not over: the service runs that sleep with their
|
|
444
|
+
* volumes, whose namespaces stay without what runs in them (a server that stopped without
|
|
445
|
+
* putting the run to sleep left it), and the runs that resume (queued), whose Jobs and
|
|
446
|
+
* services stay where they are until they execute again or end. The namespaces of any other
|
|
447
|
+
* service run go.
|
|
408
448
|
*/
|
|
409
449
|
restored(runs) {
|
|
450
|
+
for (const run of runs.filter((candidate) => candidate.state === 'queued')) {
|
|
451
|
+
try {
|
|
452
|
+
this.resuming.add(run, this.holds(run) ? getVolumeScope(run) : undefined);
|
|
453
|
+
}
|
|
454
|
+
catch (e) {
|
|
455
|
+
// its objects are not kept from a sweep, the run does not start for nothing
|
|
456
|
+
this.log?.(`what run ${run.id} left on the cluster is not kept for it: ${(0, log_1.getErrorMessage)(e)}`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
410
459
|
if (this.keepServiceRuns) {
|
|
411
|
-
for (const run of runs.filter((candidate) => candidate.up)) {
|
|
460
|
+
for (const run of runs.filter((candidate) => candidate.up && candidate.state !== 'queued')) {
|
|
412
461
|
const namespace = getServiceRunNamespace(this.config.backend.namespacePrefix, run.id);
|
|
413
462
|
this.asleep.add(namespace);
|
|
414
463
|
this.enqueue(namespace, () => removeRunObjects(this.instance, namespace, job_placement_1.RUN_LABEL, RELEASE_TIMEOUT, { claims: false })).catch((e) => this.log?.(`what runs in the namespace ${namespace} of run ${run.id} could not be removed: ${(0, log_1.getErrorMessage)(e)}`));
|
|
@@ -479,8 +528,12 @@ class KubernetesExecutor {
|
|
|
479
528
|
if (this.keep === 0) {
|
|
480
529
|
return new Set();
|
|
481
530
|
}
|
|
482
|
-
return new Set([
|
|
483
|
-
.
|
|
531
|
+
return new Set([
|
|
532
|
+
...(this.active.get(namespace) ?? new Map()).entries(),
|
|
533
|
+
// a run that resumes mounts them again
|
|
534
|
+
...this.resuming.scopes(namespace),
|
|
535
|
+
]
|
|
536
|
+
.filter(([label, scope]) => label !== except && scope !== undefined)
|
|
484
537
|
.map(([, scope]) => scope));
|
|
485
538
|
}
|
|
486
539
|
// What changes objects of a whole namespace happens one at a time, not what its runs do
|
|
@@ -506,6 +559,9 @@ class KubernetesExecutor {
|
|
|
506
559
|
}
|
|
507
560
|
this.serviceRuns.set(namespace, (this.serviceRuns.get(namespace) ?? 0) + 1);
|
|
508
561
|
this.asleep.delete(namespace);
|
|
562
|
+
// its execution holds the namespace now, and its release removes it
|
|
563
|
+
this.resuming.forget(run);
|
|
564
|
+
this.placed.add(run.id);
|
|
509
565
|
this.sweepServiceRuns();
|
|
510
566
|
const up = run.up;
|
|
511
567
|
// after what a stop of its sleep left in it went (see restored)
|
|
@@ -537,7 +593,9 @@ class KubernetesExecutor {
|
|
|
537
593
|
// release failed: the objects of a run that does not execute. One that starts
|
|
538
594
|
// after this waits for it here before it makes any.
|
|
539
595
|
if (!this.tidy.has(namespace)) {
|
|
540
|
-
const running = [
|
|
596
|
+
const running = [
|
|
597
|
+
...new Set([label, ...(this.active.get(namespace)?.keys() ?? []), ...this.resuming.labels(namespace)]),
|
|
598
|
+
];
|
|
541
599
|
await removeRunObjects(this.instance, namespace, `${job_placement_1.RUN_LABEL},${job_placement_1.RUN_LABEL} notin (${running.join(',')})`, RELEASE_TIMEOUT);
|
|
542
600
|
this.tidy.add(namespace);
|
|
543
601
|
}
|
|
@@ -557,6 +615,7 @@ class KubernetesExecutor {
|
|
|
557
615
|
// tasks run
|
|
558
616
|
await this.quotas?.apply(namespace);
|
|
559
617
|
context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
|
|
618
|
+
this.placed.add(run.id);
|
|
560
619
|
return {
|
|
561
620
|
kubernetes: this.environmentOf(run, namespace, work, capabilitiesProblem),
|
|
562
621
|
env: this.access.env,
|
|
@@ -599,38 +658,33 @@ class KubernetesExecutor {
|
|
|
599
658
|
}
|
|
600
659
|
return;
|
|
601
660
|
}
|
|
602
|
-
|
|
603
|
-
const selector = `${job_placement_1.RUN_LABEL}=${(0, job_placement_1.getRunLabelValue)(run.id)}`;
|
|
604
|
-
await removeRunObjects(this.instance, namespace, selector, RELEASE_TIMEOUT);
|
|
605
|
-
}
|
|
606
|
-
catch (e) {
|
|
607
|
-
// the next run removes what is left
|
|
608
|
-
this.tidy.delete(namespace);
|
|
609
|
-
context.emit({
|
|
610
|
-
type: 'log',
|
|
611
|
-
task: 'hammerkit',
|
|
612
|
-
stream: 'stderr',
|
|
613
|
-
text: `what the run made in ${namespace} could not be removed: ${(0, log_1.getErrorMessage)(e)}`,
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
if (this.keep > 0) {
|
|
617
|
-
try {
|
|
618
|
-
await (0, kept_claims_1.expireKeptClaims)(this.instance, namespace, (0, job_placement_1.getRunLabelValue)(run.id), new Date(Date.now() + this.keep));
|
|
619
|
-
}
|
|
620
|
-
catch (e) {
|
|
621
|
-
// without an expiry, the next look at the namespace removes them
|
|
622
|
-
context.emit({
|
|
623
|
-
type: 'log',
|
|
624
|
-
task: 'hammerkit',
|
|
625
|
-
stream: 'stderr',
|
|
626
|
-
text: `the claims the run kept in ${namespace} could not be given their expiry: ${(0, log_1.getErrorMessage)(e)}`,
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
this.sweepLater(namespace, this.keep);
|
|
630
|
-
}
|
|
661
|
+
await this.removeRun(run, namespace, (text) => context.emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text }));
|
|
631
662
|
},
|
|
632
663
|
};
|
|
633
664
|
}
|
|
665
|
+
// The end of a run of tasks: what it made in the caller's namespace goes, but for the claims
|
|
666
|
+
// it keeps, which expire from now
|
|
667
|
+
async removeRun(run, namespace, say) {
|
|
668
|
+
try {
|
|
669
|
+
const selector = `${job_placement_1.RUN_LABEL}=${(0, job_placement_1.getRunLabelValue)(run.id)}`;
|
|
670
|
+
await removeRunObjects(this.instance, namespace, selector, RELEASE_TIMEOUT);
|
|
671
|
+
}
|
|
672
|
+
catch (e) {
|
|
673
|
+
// the next run removes what is left
|
|
674
|
+
this.tidy.delete(namespace);
|
|
675
|
+
say(`what the run made in ${namespace} could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
|
|
676
|
+
}
|
|
677
|
+
if (this.keep > 0) {
|
|
678
|
+
try {
|
|
679
|
+
await (0, kept_claims_1.expireKeptClaims)(this.instance, namespace, (0, job_placement_1.getRunLabelValue)(run.id), new Date(Date.now() + this.keep));
|
|
680
|
+
}
|
|
681
|
+
catch (e) {
|
|
682
|
+
// without an expiry, the next look at the namespace removes them
|
|
683
|
+
say(`the claims the run kept in ${namespace} could not be given their expiry: ${(0, log_1.getErrorMessage)(e)}`);
|
|
684
|
+
}
|
|
685
|
+
this.sweepLater(namespace, this.keep);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
634
688
|
// The cluster as the process of a run sees it: the namespace its tasks and services go in.
|
|
635
689
|
environmentOf(run, namespace, work, capabilitiesProblem) {
|
|
636
690
|
const backend = this.config.backend;
|
|
@@ -686,6 +740,7 @@ class KubernetesExecutor {
|
|
|
686
740
|
isCallerNamespace(this.config.backend.namespacePrefix, name) &&
|
|
687
741
|
!this.serviceRuns.has(name) &&
|
|
688
742
|
!this.asleep.has(name) &&
|
|
743
|
+
!this.resuming.keeps(name) &&
|
|
689
744
|
!namespace.metadata?.deletionTimestamp) {
|
|
690
745
|
this.log?.(`removing the namespace ${name} of a service run from before the server started`);
|
|
691
746
|
await removeServiceRunNamespace(this.instance, name, 0);
|
|
@@ -747,6 +802,54 @@ class KubernetesExecutor {
|
|
|
747
802
|
}
|
|
748
803
|
}
|
|
749
804
|
exports.KubernetesExecutor = KubernetesExecutor;
|
|
805
|
+
// What the runs that resume after a restart left on the cluster, which the sweeps leave: the
|
|
806
|
+
// namespaces of service runs, and per caller namespace the runs by run label, with the volume
|
|
807
|
+
// scope of the kept claims they mount
|
|
808
|
+
class ResumingRuns {
|
|
809
|
+
constructor(prefix, namespaceOf) {
|
|
810
|
+
this.prefix = prefix;
|
|
811
|
+
this.namespaceOf = namespaceOf;
|
|
812
|
+
this.serviceRuns = new Set();
|
|
813
|
+
this.runs = new Map();
|
|
814
|
+
}
|
|
815
|
+
add(run, scope) {
|
|
816
|
+
if (run.up) {
|
|
817
|
+
this.serviceRuns.add(this.namespaceOf(run));
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const namespace = getCallerNamespace(this.prefix, run.owner);
|
|
821
|
+
const runs = this.runs.get(namespace) ?? new Map();
|
|
822
|
+
runs.set((0, job_placement_1.getRunLabelValue)(run.id), scope);
|
|
823
|
+
this.runs.set(namespace, runs);
|
|
824
|
+
}
|
|
825
|
+
has(run) {
|
|
826
|
+
return run.up
|
|
827
|
+
? this.serviceRuns.has(this.namespaceOf(run))
|
|
828
|
+
: !!this.runs.get(getCallerNamespace(this.prefix, run.owner))?.has((0, job_placement_1.getRunLabelValue)(run.id));
|
|
829
|
+
}
|
|
830
|
+
forget(run) {
|
|
831
|
+
if (run.up) {
|
|
832
|
+
this.serviceRuns.delete(this.namespaceOf(run));
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
const namespace = getCallerNamespace(this.prefix, run.owner);
|
|
836
|
+
const runs = this.runs.get(namespace);
|
|
837
|
+
runs?.delete((0, job_placement_1.getRunLabelValue)(run.id));
|
|
838
|
+
if (runs?.size === 0) {
|
|
839
|
+
this.runs.delete(namespace);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
// whether the namespace of a service run is kept for it
|
|
843
|
+
keeps(namespace) {
|
|
844
|
+
return this.serviceRuns.has(namespace);
|
|
845
|
+
}
|
|
846
|
+
labels(namespace) {
|
|
847
|
+
return [...(this.runs.get(namespace)?.keys() ?? [])];
|
|
848
|
+
}
|
|
849
|
+
scopes(namespace) {
|
|
850
|
+
return [...(this.runs.get(namespace)?.entries() ?? [])];
|
|
851
|
+
}
|
|
852
|
+
}
|
|
750
853
|
const ENFORCE_LABEL = 'pod-security.kubernetes.io/enforce';
|
|
751
854
|
const AUDIT_LABEL = 'pod-security.kubernetes.io/audit';
|
|
752
855
|
/**
|
package/dist/server/run-logs.js
CHANGED
|
@@ -113,6 +113,23 @@ class LogIndex {
|
|
|
113
113
|
lines(task) {
|
|
114
114
|
return this.taskLines.get(task) ?? [];
|
|
115
115
|
}
|
|
116
|
+
// How many lines of the log, or of one task, the first `count` events hold
|
|
117
|
+
linesBefore(count, task) {
|
|
118
|
+
const lines = task !== undefined ? this.lines(task) : undefined;
|
|
119
|
+
let low = 0;
|
|
120
|
+
let high = lines ? lines.length : this.size;
|
|
121
|
+
// the lines are in the order of their events
|
|
122
|
+
while (low < high) {
|
|
123
|
+
const middle = (low + high) >> 1;
|
|
124
|
+
if (this.lineEvent[lines ? lines[middle] : middle] < count) {
|
|
125
|
+
low = middle + 1;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
high = middle;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return low;
|
|
132
|
+
}
|
|
116
133
|
text(events, line) {
|
|
117
134
|
const event = events[this.lineEvent[line]];
|
|
118
135
|
return { task: event.task, stream: event.stream, text: event.text.slice(this.lineStart[line], this.lineEnd[line]) };
|
|
@@ -145,9 +162,15 @@ class LogIndex {
|
|
|
145
162
|
}
|
|
146
163
|
exports.LogIndex = LogIndex;
|
|
147
164
|
// The lines of the log a query selects, see LogQuery.
|
|
148
|
-
|
|
165
|
+
// `lostAfter`: Run.lostAfter
|
|
166
|
+
function readLogs(events, index, query, truncated, lostAfter) {
|
|
149
167
|
const selected = query.task !== undefined ? index.lines(query.task) : undefined;
|
|
150
168
|
const total = selected ? selected.length : index.size;
|
|
169
|
+
const lost = lostAfter !== undefined
|
|
170
|
+
? index.linesBefore(lostAfter, query.task) + 1
|
|
171
|
+
: query.from !== undefined && query.from > total + 1
|
|
172
|
+
? total + 1
|
|
173
|
+
: undefined;
|
|
151
174
|
const at = (position) => (selected ? selected[position] : position);
|
|
152
175
|
const hits = query.grep !== undefined ? index.matches(events, query.grep, query.ignoreCase) : undefined;
|
|
153
176
|
const toLine = (position) => ({
|
|
@@ -215,6 +238,7 @@ function readLogs(events, index, query, truncated) {
|
|
|
215
238
|
total,
|
|
216
239
|
...(next !== undefined ? { next } : {}),
|
|
217
240
|
truncated,
|
|
241
|
+
...(lost !== undefined ? { lost } : {}),
|
|
218
242
|
};
|
|
219
243
|
}
|
|
220
244
|
exports.readLogs = readLogs;
|