hammerkit 2.1.0-next.2 → 2.1.0-next.21

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 (87) hide show
  1. package/build.schema.json +39 -18
  2. package/dist/cli.js +5 -0
  3. package/dist/dashboard/assets/graph-canvas-D6GMgkoe.js +8 -0
  4. package/dist/dashboard/assets/index-BNq9j5Jo.js +23 -0
  5. package/dist/dashboard/assets/index-DP3_hKOe.css +2 -0
  6. package/dist/dashboard/index.html +2 -2
  7. package/dist/environment/replace-env-variables.js +8 -5
  8. package/dist/environment/run-source-envs.js +2 -1
  9. package/dist/executer/archive-task-entry.js +1 -1
  10. package/dist/executer/deadline.js +63 -10
  11. package/dist/executer/event-cache.js +5 -1
  12. package/dist/executer/execute-work-task.js +15 -3
  13. package/dist/executer/runner-commands.js +36 -0
  14. package/dist/file/get-file-context.js +4 -7
  15. package/dist/harness/connect.js +12 -8
  16. package/dist/harness/reconnect.js +141 -0
  17. package/dist/harness/sandbox-client.js +241 -33
  18. package/dist/harness/service-tools.js +11 -2
  19. package/dist/kubernetes/cluster-state.js +48 -14
  20. package/dist/kubernetes/ensure-persistent-data.js +15 -4
  21. package/dist/kubernetes/follow-pod-logs.js +8 -5
  22. package/dist/kubernetes/kept-claims.js +26 -5
  23. package/dist/kubernetes/resources.js +5 -1
  24. package/dist/kubernetes/sandbox-runner.js +526 -156
  25. package/dist/kubernetes/store-kubernetes-data.js +18 -3
  26. package/dist/kubernetes/sync-changes.js +463 -0
  27. package/dist/kubernetes/volumes.js +23 -1
  28. package/dist/optimizer/get-work-cache-stats.js +7 -0
  29. package/dist/optimizer/work-task-cache-description.js +3 -2
  30. package/dist/planner/image-build.js +5 -0
  31. package/dist/planner/utils/dependency-outputs.js +7 -6
  32. package/dist/planner/validate.js +41 -0
  33. package/dist/planner/work-item-status.js +12 -2
  34. package/dist/planner/work-runtime-aggregate.js +1 -0
  35. package/dist/planner/work-runtime-docker.js +33 -6
  36. package/dist/planner/work-runtime-kubernetes.js +313 -70
  37. package/dist/planner/work-runtime-local.js +8 -4
  38. package/dist/planner/work-secret.js +33 -11
  39. package/dist/planner/work-service-id.js +30 -6
  40. package/dist/program.js +19 -1
  41. package/dist/remote/annotations.js +37 -5
  42. package/dist/remote/bundle.js +6 -8
  43. package/dist/remote/collect-bundle.js +3 -2
  44. package/dist/remote/device-login.js +4 -1
  45. package/dist/remote/remote-commands.js +16 -7
  46. package/dist/remote/remote-work-commands.js +266 -75
  47. package/dist/remote/server-client.js +38 -40
  48. package/dist/schema/build-file-base-task-schema.js +16 -1
  49. package/dist/schema/build-file-image-build-schema.js +5 -8
  50. package/dist/schema/reference-parser.js +108 -43
  51. package/dist/schema/work-scope-parser.js +8 -0
  52. package/dist/secrets/prefetch-secrets.js +20 -1
  53. package/dist/server/artifacts-api.js +56 -0
  54. package/dist/server/definitions-api.js +62 -8
  55. package/dist/server/executor.js +117 -30
  56. package/dist/server/http.js +4 -1
  57. package/dist/server/keep-artifacts.js +230 -0
  58. package/dist/server/kubernetes-backend.js +186 -59
  59. package/dist/server/kubernetes-sandbox-storage.js +3 -2
  60. package/dist/server/overview.js +73 -9
  61. package/dist/server/paging.js +167 -0
  62. package/dist/server/plan.js +16 -1
  63. package/dist/server/run-access.js +24 -0
  64. package/dist/server/run-artifacts.js +171 -0
  65. package/dist/server/run-logs.js +25 -1
  66. package/dist/server/run-request.js +11 -0
  67. package/dist/server/run-source-environment.js +1 -1
  68. package/dist/server/run-worker.js +267 -33
  69. package/dist/server/runs.js +527 -99
  70. package/dist/server/sandbox-files-api.js +27 -12
  71. package/dist/server/sandbox-files.js +12 -10
  72. package/dist/server/sandbox-follow.js +54 -0
  73. package/dist/server/sandbox-pod.js +1 -1
  74. package/dist/server/sandbox-storage.js +38 -1
  75. package/dist/server/sandbox-sync.js +392 -0
  76. package/dist/server/sandboxes-api.js +116 -19
  77. package/dist/server/sandboxes.js +38 -3
  78. package/dist/server/server-config.js +14 -0
  79. package/dist/server/server.js +169 -38
  80. package/dist/server/task-order.js +56 -0
  81. package/dist/utils/project-path.js +20 -0
  82. package/dist/utils/tar-files.js +52 -0
  83. package/dist/utils/tar.js +37 -1
  84. package/package.json +1 -1
  85. package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
  86. package/dist/dashboard/assets/index-BndChMep.css +0 -2
  87. package/dist/dashboard/assets/index-DRSwIRXd.js +0 -17
@@ -15,10 +15,13 @@ const log_1 = require("../log");
15
15
  const network_policy_1 = require("../kubernetes/network-policy");
16
16
  const network_gate_1 = require("../kubernetes/network-gate");
17
17
  const sleep_1 = require("../utils/sleep");
18
+ const kubernetes_sandbox_storage_1 = require("./kubernetes-sandbox-storage");
19
+ const sandbox_runner_1 = require("../kubernetes/sandbox-runner");
18
20
  const version_1 = require("../version");
19
21
  const executor_1 = require("./executor");
20
22
  const kept_claims_1 = require("../kubernetes/kept-claims");
21
23
  const units_1 = require("../utils/units");
24
+ const runs_1 = require("./runs");
22
25
  const server_config_1 = require("./server-config");
23
26
  // the name of the quota and limit range the server keeps in a caller's namespace
24
27
  const POLICY_NAME = 'hammerkit';
@@ -270,8 +273,11 @@ class KubernetesExecutor {
270
273
  // per caller namespace, what changes objects of the whole namespace: its setup, the
271
274
  // removal of what a stopped server left, the sweeps of kept claims. The last one waiting.
272
275
  this.queues = new Map();
273
- // per caller namespace, the runs that execute in it and mount kept claims: by run label,
274
- // their volume scope
276
+ // per caller namespace, the runs that execute in it: by run label, the volume scope of the
277
+ // kept claims they mount, if any. Every run that executes is here, not only those that mount
278
+ // kept claims: a tidy of the namespace (after a release that failed, or the first after a
279
+ // restart) leaves the objects of all of them, a resumed run's too once it no longer counts
280
+ // as resuming
275
281
  this.active = new Map();
276
282
  // per caller namespace, when its kept claims are next looked at
277
283
  this.sweeps = new Map();
@@ -285,6 +291,9 @@ class KubernetesExecutor {
285
291
  this.serviceRuns = new Map();
286
292
  this.serviceRunsSwept = false;
287
293
  this.asleep = new Set();
294
+ // the runs whose execution got its place on the cluster, until it ended: their release
295
+ // removes what they made
296
+ this.placed = new Set();
288
297
  this.access = getClusterAccess(config.backend, processEnvs);
289
298
  this.instance = (0, kubernetes_instance_1.createKubernetesInstances)({
290
299
  type: 'kubernetes',
@@ -316,6 +325,7 @@ class KubernetesExecutor {
316
325
  log: options.log,
317
326
  });
318
327
  }
328
+ this.resuming = new ResumingRuns(config.backend.namespacePrefix, (run) => this.namespaceOf(run));
319
329
  this.host = new executor_1.HostExecutor({ ...options, placement: this.placement() });
320
330
  }
321
331
  // what the namespaces of the server's callers and service runs request and use
@@ -345,13 +355,61 @@ class KubernetesExecutor {
345
355
  });
346
356
  }
347
357
  async execute(run, context) {
348
- if (run.up) {
349
- if (run.source.type === 'sandbox') {
350
- throw new Error('on a cluster a service run starts from a branch, push the sandbox and start it from there');
358
+ // what a run that resumes left is this execution's from now: the end of a run that got its
359
+ // place on the cluster (release) removes it, the end of one that did not removes it here
360
+ const left = this.resuming.has(run);
361
+ try {
362
+ await this.executeOn(run, context);
363
+ }
364
+ finally {
365
+ const placed = this.placed.delete(run.id);
366
+ if (left && !placed && !(0, runs_1.isDetached)(context)) {
367
+ // the run executed: its process removed its commands when it ended
368
+ await this.removeLeft(run, false);
351
369
  }
352
- // in a namespace of its own, apart from the caller's runs and their claims
370
+ }
371
+ }
372
+ // What a run left on the cluster, that no release removes
373
+ async removeLeft(run, commands = true) {
374
+ const say = (text) => this.log?.(`run=${run.id} ${text}`);
375
+ this.resuming.forget(run);
376
+ try {
377
+ if (!run.up) {
378
+ const namespace = getCallerNamespace(this.config.backend.namespacePrefix, run.owner);
379
+ if (commands) {
380
+ await this.removeRunCommands(run, namespace, say);
381
+ }
382
+ await this.removeRun(run, namespace, say);
383
+ return;
384
+ }
385
+ // a namespace is kept only for a run that has one from start to end
386
+ const namespace = this.namespaceOf(run);
387
+ this.asleep.delete(namespace);
388
+ await this.enqueue(namespace, () => this.removeServiceRun(namespace, say));
389
+ }
390
+ catch (e) {
391
+ say(`what the run left could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
392
+ }
393
+ }
394
+ // What the commands of a run on a sandbox left in its runner: they ran on over the restart
395
+ // for the run to take them up again, which it no longer does. Another run may be on the
396
+ // sandbox by now, so only the run's own commands are killed.
397
+ async removeRunCommands(run, namespace, say) {
398
+ if (run.source.type !== 'sandbox' || !this.config.backend.sandboxes.runner) {
399
+ return;
400
+ }
401
+ await (0, sandbox_runner_1.removeRunCommands)(this.instance, namespace, (0, kubernetes_sandbox_storage_1.getSandboxNames)(run.source.id).label, run.id, {
402
+ clean: false,
403
+ }).catch((e) => say(`what the commands of the run left in the sandbox could not be removed: ${(0, log_1.getErrorMessage)(e)}`));
404
+ }
405
+ async executeOn(run, context) {
406
+ if (run.up) {
407
+ // in a namespace of its own, apart from the caller's runs and their claims, from a branch
408
+ // or from the files of a sandbox (copied, see HostExecutor.execute)
353
409
  return this.host.execute(run, context);
354
410
  }
411
+ // it executes: its objects are kept from a tidy as those of any run that executes
412
+ this.resuming.forget(run);
355
413
  const namespace = getCallerNamespace(this.config.backend.namespacePrefix, run.owner);
356
414
  const label = (0, job_placement_1.getRunLabelValue)(run.id);
357
415
  let runs = this.active.get(namespace);
@@ -359,9 +417,7 @@ class KubernetesExecutor {
359
417
  runs = new Map();
360
418
  this.active.set(namespace, runs);
361
419
  }
362
- if (this.holds(run)) {
363
- runs.set(label, getVolumeScope(run));
364
- }
420
+ runs.set(label, this.holds(run) ? getVolumeScope(run) : undefined);
365
421
  // the caller is promised its share while the run executes
366
422
  this.quotas?.enter(namespace, run.id);
367
423
  try {
@@ -372,11 +428,19 @@ class KubernetesExecutor {
372
428
  if (runs.size === 0 && this.active.get(namespace) === runs) {
373
429
  this.active.delete(namespace);
374
430
  }
375
- await this.quotas
376
- ?.leave(namespace, run.id)
377
- .catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
431
+ // a run the next server resumes still has its Jobs, under the quota it had
432
+ if (!(0, runs_1.isDetached)(context)) {
433
+ await this.quotas
434
+ ?.leave(namespace, run.id)
435
+ .catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
436
+ }
378
437
  }
379
438
  }
439
+ // The changed files of a sandbox for the services of a service run here: they are synced in
440
+ // place, nothing of the run moves (see RunExecutor.syncFiles)
441
+ syncFiles(run, changes) {
442
+ return !!run.up && this.host.syncChildFiles(run.id, changes);
443
+ }
380
444
  // A service run moves to another commit in its namespace: unchanged services stay, the
381
445
  // others roll to their new pods (see kubernetesServiceRuntime)
382
446
  movesBeside(run) {
@@ -390,25 +454,32 @@ class KubernetesExecutor {
390
454
  }
391
455
  /**
392
456
  * A service run that slept with its volumes ended without waking: its namespace goes, with
393
- * the claims in it.
457
+ * the claims in it. A run that resumed after a restart ended: what an execution before the
458
+ * restart left goes, as no execution may have removed it (one cancelled in the queue).
394
459
  */
395
460
  async forget(run) {
396
- if (!run.up) {
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}`)));
461
+ // the commands of a run are left only when it was restored and never executed again
462
+ await this.removeLeft(run, this.resuming.has(run));
403
463
  }
404
464
  /**
405
- * The service runs of a previous start that sleep with their volumes: their namespaces stay,
406
- * without what runs in them (a server that stopped without putting the run to sleep left
407
- * it). The namespaces of any other service run go.
465
+ * The runs of a previous start that are not over: the service runs that sleep with their
466
+ * volumes, whose namespaces stay without what runs in them (a server that stopped without
467
+ * putting the run to sleep left it), and the runs that resume (queued), whose Jobs and
468
+ * services stay where they are until they execute again or end. The namespaces of any other
469
+ * service run go.
408
470
  */
409
471
  restored(runs) {
472
+ for (const run of runs.filter((candidate) => candidate.state === 'queued')) {
473
+ try {
474
+ this.resuming.add(run, this.holds(run) ? getVolumeScope(run) : undefined);
475
+ }
476
+ catch (e) {
477
+ // its objects are not kept from a sweep, the run does not start for nothing
478
+ this.log?.(`what run ${run.id} left on the cluster is not kept for it: ${(0, log_1.getErrorMessage)(e)}`);
479
+ }
480
+ }
410
481
  if (this.keepServiceRuns) {
411
- for (const run of runs.filter((candidate) => candidate.up)) {
482
+ for (const run of runs.filter((candidate) => candidate.up && candidate.state !== 'queued')) {
412
483
  const namespace = getServiceRunNamespace(this.config.backend.namespacePrefix, run.id);
413
484
  this.asleep.add(namespace);
414
485
  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 +550,12 @@ class KubernetesExecutor {
479
550
  if (this.keep === 0) {
480
551
  return new Set();
481
552
  }
482
- return new Set([...(this.active.get(namespace) ?? new Map()).entries()]
483
- .filter(([label]) => label !== except)
553
+ return new Set([
554
+ ...(this.active.get(namespace) ?? new Map()).entries(),
555
+ // a run that resumes mounts them again
556
+ ...this.resuming.scopes(namespace),
557
+ ]
558
+ .filter(([label, scope]) => label !== except && scope !== undefined)
484
559
  .map(([, scope]) => scope));
485
560
  }
486
561
  // What changes objects of a whole namespace happens one at a time, not what its runs do
@@ -506,6 +581,9 @@ class KubernetesExecutor {
506
581
  }
507
582
  this.serviceRuns.set(namespace, (this.serviceRuns.get(namespace) ?? 0) + 1);
508
583
  this.asleep.delete(namespace);
584
+ // its execution holds the namespace now, and its release removes it
585
+ this.resuming.forget(run);
586
+ this.placed.add(run.id);
509
587
  this.sweepServiceRuns();
510
588
  const up = run.up;
511
589
  // after what a stop of its sleep left in it went (see restored)
@@ -524,7 +602,7 @@ class KubernetesExecutor {
524
602
  // its pods take room the callers can't borrow
525
603
  await this.quotas?.serviceRun(namespace, this.config.runs.up.quota ?? this.config.backend.quota);
526
604
  context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
527
- return { kubernetes: this.environmentOf(run, namespace), env: this.access.env };
605
+ return { kubernetes: this.environmentOf(run, namespace, context), env: this.access.env };
528
606
  }
529
607
  const label = (0, job_placement_1.getRunLabelValue)(run.id);
530
608
  let capabilitiesProblem;
@@ -537,7 +615,9 @@ class KubernetesExecutor {
537
615
  // release failed: the objects of a run that does not execute. One that starts
538
616
  // after this waits for it here before it makes any.
539
617
  if (!this.tidy.has(namespace)) {
540
- const running = [...new Set([label, ...(this.active.get(namespace)?.keys() ?? [])])];
618
+ const running = [
619
+ ...new Set([label, ...(this.active.get(namespace)?.keys() ?? []), ...this.resuming.labels(namespace)]),
620
+ ];
541
621
  await removeRunObjects(this.instance, namespace, `${job_placement_1.RUN_LABEL},${job_placement_1.RUN_LABEL} notin (${running.join(',')})`, RELEASE_TIMEOUT);
542
622
  this.tidy.add(namespace);
543
623
  }
@@ -557,8 +637,9 @@ class KubernetesExecutor {
557
637
  // tasks run
558
638
  await this.quotas?.apply(namespace);
559
639
  context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
640
+ this.placed.add(run.id);
560
641
  return {
561
- kubernetes: this.environmentOf(run, namespace, work, capabilitiesProblem),
642
+ kubernetes: this.environmentOf(run, namespace, context, work, capabilitiesProblem),
562
643
  env: this.access.env,
563
644
  };
564
645
  },
@@ -599,40 +680,35 @@ class KubernetesExecutor {
599
680
  }
600
681
  return;
601
682
  }
602
- try {
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
- }
683
+ await this.removeRun(run, namespace, (text) => context.emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text }));
631
684
  },
632
685
  };
633
686
  }
687
+ // The end of a run of tasks: what it made in the caller's namespace goes, but for the claims
688
+ // it keeps, which expire from now
689
+ async removeRun(run, namespace, say) {
690
+ try {
691
+ const selector = `${job_placement_1.RUN_LABEL}=${(0, job_placement_1.getRunLabelValue)(run.id)}`;
692
+ await removeRunObjects(this.instance, namespace, selector, RELEASE_TIMEOUT);
693
+ }
694
+ catch (e) {
695
+ // the next run removes what is left
696
+ this.tidy.delete(namespace);
697
+ say(`what the run made in ${namespace} could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
698
+ }
699
+ if (this.keep > 0) {
700
+ try {
701
+ await (0, kept_claims_1.expireKeptClaims)(this.instance, namespace, (0, job_placement_1.getRunLabelValue)(run.id), new Date(Date.now() + this.keep));
702
+ }
703
+ catch (e) {
704
+ // without an expiry, the next look at the namespace removes them
705
+ say(`the claims the run kept in ${namespace} could not be given their expiry: ${(0, log_1.getErrorMessage)(e)}`);
706
+ }
707
+ this.sweepLater(namespace, this.keep);
708
+ }
709
+ }
634
710
  // The cluster as the process of a run sees it: the namespace its tasks and services go in.
635
- environmentOf(run, namespace, work, capabilitiesProblem) {
711
+ environmentOf(run, namespace, context, work, capabilitiesProblem) {
636
712
  const backend = this.config.backend;
637
713
  const openGate = (0, network_gate_1.getOpenNetworkGate)(backend);
638
714
  return {
@@ -656,6 +732,8 @@ class KubernetesExecutor {
656
732
  ...(this.keep > 0 && !run.up ? { volumeScope: getVolumeScope(run) } : {}),
657
733
  ...(run.up ? { serviceRun: true } : {}),
658
734
  ...(openGate ? { openGate } : {}),
735
+ // what the execution before a restart left, its tasks and services take over
736
+ ...(context.resumed ? { resumed: { logs: context.resumed.logs } } : {}),
659
737
  ...(backend.capabilities
660
738
  ? {
661
739
  capabilities: {
@@ -686,6 +764,7 @@ class KubernetesExecutor {
686
764
  isCallerNamespace(this.config.backend.namespacePrefix, name) &&
687
765
  !this.serviceRuns.has(name) &&
688
766
  !this.asleep.has(name) &&
767
+ !this.resuming.keeps(name) &&
689
768
  !namespace.metadata?.deletionTimestamp) {
690
769
  this.log?.(`removing the namespace ${name} of a service run from before the server started`);
691
770
  await removeServiceRunNamespace(this.instance, name, 0);
@@ -747,6 +826,54 @@ class KubernetesExecutor {
747
826
  }
748
827
  }
749
828
  exports.KubernetesExecutor = KubernetesExecutor;
829
+ // What the runs that resume after a restart left on the cluster, which the sweeps leave: the
830
+ // namespaces of service runs, and per caller namespace the runs by run label, with the volume
831
+ // scope of the kept claims they mount
832
+ class ResumingRuns {
833
+ constructor(prefix, namespaceOf) {
834
+ this.prefix = prefix;
835
+ this.namespaceOf = namespaceOf;
836
+ this.serviceRuns = new Set();
837
+ this.runs = new Map();
838
+ }
839
+ add(run, scope) {
840
+ if (run.up) {
841
+ this.serviceRuns.add(this.namespaceOf(run));
842
+ return;
843
+ }
844
+ const namespace = getCallerNamespace(this.prefix, run.owner);
845
+ const runs = this.runs.get(namespace) ?? new Map();
846
+ runs.set((0, job_placement_1.getRunLabelValue)(run.id), scope);
847
+ this.runs.set(namespace, runs);
848
+ }
849
+ has(run) {
850
+ return run.up
851
+ ? this.serviceRuns.has(this.namespaceOf(run))
852
+ : !!this.runs.get(getCallerNamespace(this.prefix, run.owner))?.has((0, job_placement_1.getRunLabelValue)(run.id));
853
+ }
854
+ forget(run) {
855
+ if (run.up) {
856
+ this.serviceRuns.delete(this.namespaceOf(run));
857
+ return;
858
+ }
859
+ const namespace = getCallerNamespace(this.prefix, run.owner);
860
+ const runs = this.runs.get(namespace);
861
+ runs?.delete((0, job_placement_1.getRunLabelValue)(run.id));
862
+ if (runs?.size === 0) {
863
+ this.runs.delete(namespace);
864
+ }
865
+ }
866
+ // whether the namespace of a service run is kept for it
867
+ keeps(namespace) {
868
+ return this.serviceRuns.has(namespace);
869
+ }
870
+ labels(namespace) {
871
+ return [...(this.runs.get(namespace)?.keys() ?? [])];
872
+ }
873
+ scopes(namespace) {
874
+ return [...(this.runs.get(namespace)?.entries() ?? [])];
875
+ }
876
+ }
750
877
  const ENFORCE_LABEL = 'pod-security.kubernetes.io/enforce';
751
878
  const AUDIT_LABEL = 'pod-security.kubernetes.io/audit';
752
879
  /**
@@ -1030,8 +1030,9 @@ class KubernetesSandboxFiles {
1030
1030
  changes() {
1031
1031
  return this.ask({ op: 'changes' });
1032
1032
  }
1033
- diff(format, author) {
1034
- return this.ask({ op: 'diff', format, author });
1033
+ diff(format, author, paths) {
1034
+ // a pod of an older image leaves `paths` out and answers the whole diff
1035
+ return this.ask({ op: 'diff', format, author, ...(paths ? { paths } : {}) });
1035
1036
  }
1036
1037
  async commit(author) {
1037
1038
  return (await this.ask({ op: 'commit', author })) ?? undefined;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.visibleCapacity = exports.filterRuns = exports.FilterRefused = exports.parseRunFilter = exports.getOverview = exports.queuePlace = exports.visibleReason = exports.toListedRun = exports.mayRead = exports.seesActivityOf = exports.seesActivity = exports.maySee = exports.decides = exports.owns = exports.mayDecide = exports.mayRetry = exports.runActions = exports.getViewer = exports.describeWait = void 0;
3
+ exports.visibleCapacity = exports.withoutField = exports.runValues = exports.runSource = exports.filterRuns = exports.FilterRefused = exports.parseRunFilter = exports.STATES = exports.RUN_FIELDS = exports.getOverview = exports.queuePlace = exports.visibleReason = exports.toListedRun = exports.mayRead = exports.seesActivityOf = exports.seesActivity = exports.maySee = exports.decides = exports.owns = exports.mayDecide = exports.mayRetry = exports.runActions = exports.getViewer = exports.describeWait = void 0;
4
4
  const policy_1 = require("./policy");
5
5
  const runs_1 = require("./runs");
6
6
  const service_runs_1 = require("./service-runs");
7
7
  const repo_1 = require("./repo");
8
8
  const units_1 = require("../utils/units");
9
+ const paging_1 = require("./paging");
9
10
  var wait_reason_1 = require("./wait-reason");
10
11
  Object.defineProperty(exports, "describeWait", { enumerable: true, get: function () { return wait_reason_1.describeWait; } });
11
12
  function getViewer(config, principal) {
@@ -227,7 +228,9 @@ exports.getOverview = getOverview;
227
228
  function meter(runs, limit) {
228
229
  return { used: runs.length, ...(limit !== undefined ? { limit } : {}), runs: [...runs] };
229
230
  }
230
- const STATES = [
231
+ // what the filters of the runs page narrow by, and GET /v1/runs?values= answers the values of
232
+ exports.RUN_FIELDS = ['state', 'source', 'task'];
233
+ exports.STATES = [
231
234
  'awaiting-approval',
232
235
  'queued',
233
236
  'running',
@@ -238,7 +241,8 @@ const STATES = [
238
241
  'cancelled',
239
242
  'rejected',
240
243
  ];
241
- // `scope`, `state` (a comma separated list) and `kind` of GET /v1/runs.
244
+ // `scope`, `state` (a comma separated list, or repeated), `kind`, `id`, `sandbox`, `since`,
245
+ // `until`, `source` and `task` (each repeated for more than one) and `q` of GET /v1/runs.
242
246
  function parseRunFilter(params, grant, now = Date.now()) {
243
247
  const scope = params.get('scope') ?? 'mine';
244
248
  if (scope !== 'mine' && scope !== 'visible' && scope !== 'all') {
@@ -247,11 +251,14 @@ function parseRunFilter(params, grant, now = Date.now()) {
247
251
  if (scope === 'all' && !grant.observe) {
248
252
  throw new FilterRefused('scope all needs an entitlement with observe');
249
253
  }
250
- const state = params.get('state');
251
- const states = state
252
- ? state.split(',').map((value) => {
253
- if (!STATES.includes(value)) {
254
- throw new Error(`state: one or more of ${STATES.join(', ')}`);
254
+ const state = params
255
+ .getAll('state')
256
+ .flatMap((value) => value.split(','))
257
+ .filter(Boolean);
258
+ const states = state.length
259
+ ? state.map((value) => {
260
+ if (!exports.STATES.includes(value)) {
261
+ throw new Error(`state: one or more of ${exports.STATES.join(', ')}`);
255
262
  }
256
263
  return value;
257
264
  })
@@ -264,18 +271,29 @@ function parseRunFilter(params, grant, now = Date.now()) {
264
271
  if (id !== undefined && !/^run_[A-Za-z0-9_-]{16}$/.test(id)) {
265
272
  throw new Error('id: the id of a run, run_...');
266
273
  }
274
+ const sandbox = params.get('sandbox') ?? undefined;
275
+ if (sandbox !== undefined && !runs_1.SANDBOX_ID_PATTERN.test(sandbox)) {
276
+ throw new Error('sandbox: the id of a sandbox, sbx_...');
277
+ }
267
278
  const since = parseMoment(params.get('since'), now, 'since');
268
279
  const until = parseMoment(params.get('until'), now, 'until');
269
280
  if (since !== undefined && until !== undefined && until < since) {
270
281
  throw new Error('until: after since');
271
282
  }
283
+ const sources = (0, paging_1.readList)(params, 'source');
284
+ const tasks = (0, paging_1.readList)(params, 'task');
285
+ const text = (0, paging_1.readText)(params);
272
286
  return {
273
287
  scope,
274
288
  ...(states ? { states } : {}),
275
289
  ...(kind ? { kind } : {}),
276
290
  ...(id ? { id } : {}),
291
+ ...(sandbox ? { sandbox } : {}),
277
292
  ...(since !== undefined ? { since } : {}),
278
293
  ...(until !== undefined ? { until } : {}),
294
+ ...(sources ? { sources } : {}),
295
+ ...(tasks ? { tasks } : {}),
296
+ ...(text ? { text } : {}),
279
297
  };
280
298
  }
281
299
  exports.parseRunFilter = parseRunFilter;
@@ -299,12 +317,58 @@ exports.FilterRefused = FilterRefused;
299
317
  function filterRuns(runs, viewer, filter) {
300
318
  return runs.filter((run) => (filter.scope === 'mine' ? owns(viewer, run) : maySee(viewer, run)) &&
301
319
  (!filter.id || run.id === filter.id) &&
320
+ (!filter.sandbox || (run.source.type === 'sandbox' && run.source.id === filter.sandbox)) &&
302
321
  (!filter.states || filter.states.includes(run.state)) &&
303
322
  (!filter.kind || (filter.kind === 'services') === !!run.up) &&
304
323
  (filter.since === undefined || Date.parse(run.createdAt) >= filter.since) &&
305
- (filter.until === undefined || Date.parse(run.createdAt) <= filter.until));
324
+ (filter.until === undefined || Date.parse(run.createdAt) <= filter.until) &&
325
+ (!filter.sources || filter.sources.includes(runSource(run))) &&
326
+ (!filter.tasks || runWork(run).some((name) => filter.tasks?.includes(name))) &&
327
+ (!filter.text || (0, paging_1.hasText)(runText(viewer, run), filter.text)));
306
328
  }
307
329
  exports.filterRuns = filterRuns;
330
+ // where a run is from, as the source filter names it: its repository, or `upload`
331
+ function runSource(run) {
332
+ return run.source.type === 'upload' ? 'upload' : run.source.repo;
333
+ }
334
+ exports.runSource = runSource;
335
+ // the tasks a run runs, or the services it keeps up
336
+ function runWork(run) {
337
+ return [...run.tasks, ...(run.up?.services ?? [])];
338
+ }
339
+ // what of a run the text of a search is looked for in: what the list shows of it
340
+ function runText(viewer, run) {
341
+ return [
342
+ run.id,
343
+ run.owner.sub,
344
+ run.owner.name ?? viewer.nameOf?.(run.owner),
345
+ ...runWork(run),
346
+ run.up?.name,
347
+ run.source.type === 'upload' ? undefined : run.source.repo,
348
+ run.source.type === 'ref' ? run.source.ref : undefined,
349
+ ];
350
+ }
351
+ // The values a field of the runs has, as GET /v1/runs?values= answers them: the states in the
352
+ // order a run goes through them, the others the most used first.
353
+ function runValues(field) {
354
+ switch (field) {
355
+ case 'state':
356
+ return { of: (run) => [run.state], order: exports.STATES };
357
+ case 'source':
358
+ return { of: (run) => [runSource(run)] };
359
+ case 'task':
360
+ return { of: runWork };
361
+ }
362
+ }
363
+ exports.runValues = runValues;
364
+ // The filter without the one of a field: what the values of that field are counted over.
365
+ function withoutField(filter, field) {
366
+ const key = { state: 'states', source: 'sources', task: 'tasks' }[field];
367
+ const { [key]: dropped, ...rest } = filter;
368
+ void dropped;
369
+ return rest;
370
+ }
371
+ exports.withoutField = withoutField;
308
372
  // The machines and what the runs take, with whose the namespaces are where the caller may
309
373
  // know: their own, and every one with `observe`.
310
374
  function visibleCapacity(capacity, viewer) {