hammerkit 2.0.0 → 2.1.0-next.10

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 (55) hide show
  1. package/build.schema.json +126 -28
  2. package/dist/cli.js +5 -0
  3. package/dist/dashboard/assets/graph-canvas-BFnEN5i0.js +8 -0
  4. package/dist/dashboard/assets/{index-DRSwIRXd.js → index-CE5PSB3y.js} +15 -9
  5. package/dist/dashboard/assets/index-Dt3ITvRO.css +2 -0
  6. package/dist/dashboard/index.html +2 -2
  7. package/dist/docker/item-network.js +6 -5
  8. package/dist/docker/remove-orphaned-containers.js +5 -4
  9. package/dist/environment/replace-env-variables.js +8 -5
  10. package/dist/environment/run-source-envs.js +2 -1
  11. package/dist/executer/check-readiness.js +8 -4
  12. package/dist/executer/docker-healthcheck.js +73 -0
  13. package/dist/executer/docker-service.js +21 -15
  14. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
  15. package/dist/kubernetes/kept-claims.js +12 -9
  16. package/dist/network/healthcheck-request.js +38 -0
  17. package/dist/optimizer/work-task-cache-description.js +5 -3
  18. package/dist/planner/image-build.js +5 -0
  19. package/dist/planner/service-definition.js +2 -1
  20. package/dist/planner/utils/append-work-service.js +12 -4
  21. package/dist/planner/utils/dependency-outputs.js +7 -6
  22. package/dist/planner/validate.js +41 -0
  23. package/dist/planner/work-healthcheck.js +23 -2
  24. package/dist/planner/work-runtime-docker.js +24 -1
  25. package/dist/planner/work-runtime-kubernetes.js +37 -16
  26. package/dist/planner/work-secret.js +33 -11
  27. package/dist/planner/work-service-id.js +30 -6
  28. package/dist/planner/work-service.js +2 -2
  29. package/dist/program.js +19 -1
  30. package/dist/remote/annotations.js +8 -2
  31. package/dist/remote/remote-status.js +1 -1
  32. package/dist/remote/remote-work-commands.js +5 -3
  33. package/dist/schema/build-file-base-task-schema.js +16 -1
  34. package/dist/schema/build-file-image-build-schema.js +5 -8
  35. package/dist/schema/build-file-service-container-healthcheck.js +25 -7
  36. package/dist/schema/reference-parser.js +108 -43
  37. package/dist/schema/work-scope-parser.js +8 -0
  38. package/dist/secrets/prefetch-secrets.js +20 -1
  39. package/dist/server/borrowed-quota.js +216 -0
  40. package/dist/server/capacity.js +18 -6
  41. package/dist/server/kubernetes-backend.js +114 -28
  42. package/dist/server/kubernetes-sandbox-storage.js +3 -2
  43. package/dist/server/overview.js +7 -1
  44. package/dist/server/plan.js +16 -1
  45. package/dist/server/run-source-environment.js +1 -1
  46. package/dist/server/runs.js +4 -6
  47. package/dist/server/sandbox-files-api.js +27 -12
  48. package/dist/server/sandbox-files.js +3 -2
  49. package/dist/server/sandbox-pod.js +1 -1
  50. package/dist/server/sandboxes-api.js +65 -14
  51. package/dist/server/sandboxes.js +19 -3
  52. package/dist/server/server-config.js +59 -2
  53. package/package.json +1 -1
  54. package/dist/dashboard/assets/graph-canvas-DtJSpova.js +0 -8
  55. package/dist/dashboard/assets/index-BndChMep.css +0 -2
@@ -45,7 +45,8 @@ const pushSchema = zod_1.z
45
45
  * The sandboxes of a caller: checkouts on the server that runs work on.
46
46
  *
47
47
  * POST /v1/sandboxes { repo, ref } fetch a branch into a new sandbox
48
- * GET /v1/sandboxes the caller's sandboxes, their limits and usage
48
+ * GET /v1/sandboxes the caller's sandboxes, their limits and usage;
49
+ * `?scope=all` everybody's, with `observe`
49
50
  * GET /v1/sandboxes/<id> one sandbox
50
51
  * DELETE /v1/sandboxes/<id> cancel its runs and remove it
51
52
  * POST /v1/sandboxes/<id>/fetch { ref } move a sandbox without changes to a branch's commit
@@ -53,7 +54,10 @@ const pushSchema = zod_1.z
53
54
  * ... /v1/sandboxes/<id>/tree, files/<path>, search, edits, changes: its files, see
54
55
  * `handleSandboxFiles`
55
56
  *
56
- * A sandbox is its owner's: anybody else gets `404`.
57
+ * A sandbox is its owner's: anybody else gets `404`. Who observes (`observe` on an
58
+ * entitlement) may read every sandbox, as they read every run: list them, and get one, its
59
+ * tree, files, search and changes. Everything that changes a sandbox or runs on it stays
60
+ * its owner's, and is `404` to them too.
57
61
  */
58
62
  async function handleSandboxes(config, sandboxes, repos, log, principal, req, res, url) {
59
63
  const match = /^\/v1\/sandboxes(?:\/([^/]+)(?:\/(fetch|push|tree|search|edits|changes)|\/(files)\/(.+))?)?$/.exec(url.pathname);
@@ -64,12 +68,23 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
64
68
  const action = named ?? files;
65
69
  const method = req.method;
66
70
  const who = `iss=${principal.iss} sub=${principal.sub}`;
71
+ const show = (sandbox) => view(config, sandboxes, principal, sandbox);
67
72
  try {
68
73
  if (!id) {
69
74
  if (method === 'GET') {
75
+ const scope = url.searchParams.get('scope') ?? 'mine';
76
+ if (scope !== 'mine' && scope !== 'all') {
77
+ (0, http_1.send)(res, 400, { error: 'scope: mine or all' });
78
+ return true;
79
+ }
80
+ if (scope === 'all' && !(0, policy_1.getGrant)(config.entitlements, principal).observe) {
81
+ (0, http_1.send)(res, 403, { error: 'scope all needs an entitlement with observe' });
82
+ return true;
83
+ }
70
84
  const limits = sandboxes.limits;
85
+ // the limits and the usage are the caller's, whichever sandboxes they list
71
86
  (0, http_1.send)(res, 200, {
72
- sandboxes: sandboxes.list(principal).map((sandbox) => view(sandboxes, sandbox)),
87
+ sandboxes: (scope === 'all' ? sandboxes.all() : sandboxes.list(principal)).map(show),
73
88
  limits: { count: limits.count, bytes: limits.bytes },
74
89
  used: sandboxes.usage(principal),
75
90
  });
@@ -84,21 +99,29 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
84
99
  }
85
100
  const sandbox = await sandboxes.create(principal, body.repo, body.ref);
86
101
  log(`sandbox create ${sandbox.id} ${who} repo=${sandbox.repo} commit=${sandbox.commit} bytes=${sandbox.bytes}`);
87
- (0, http_1.send)(res, 201, view(sandboxes, sandbox));
102
+ (0, http_1.send)(res, 201, show(sandbox));
88
103
  }
89
104
  else {
90
105
  (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET, POST' });
91
106
  }
92
107
  return true;
93
108
  }
94
- const sandbox = runs_1.SANDBOX_ID_PATTERN.test(id) ? sandboxes.get(principal, id) : undefined;
109
+ const known = runs_1.SANDBOX_ID_PATTERN.test(id);
110
+ const own = known ? sandboxes.get(principal, id) : undefined;
111
+ const observes = (0, policy_1.getGrant)(config.entitlements, principal).observe;
112
+ const reading = isRead(action, method);
113
+ // who observes reads the sandbox of anybody; a change is for its owner, and else not found
114
+ const sandbox = own ?? (known && observes && reading ? sandboxes.peek(id) : undefined);
95
115
  if (!sandbox) {
96
116
  (0, http_1.send)(res, 404, { error: 'not found' });
97
117
  return true;
98
118
  }
99
119
  if (action && action !== 'fetch' && action !== 'push') {
100
- // the caller may have lost the repository, or the sandbox's branch, since they created it
101
- const refused = (0, policy_1.decideSandbox)((0, policy_1.getGrant)(config.entitlements, principal), sandbox.repo, sandbox.ref, (repo) => repos.readable(repo));
120
+ // the owner may have lost the repository, or the sandbox's branch, since they created
121
+ // it; who observes reads it all the same
122
+ const refused = own && !(observes && reading)
123
+ ? (0, policy_1.decideSandbox)((0, policy_1.getGrant)(config.entitlements, principal), sandbox.repo, sandbox.ref, (repo) => repos.readable(repo))
124
+ : undefined;
102
125
  if (refused) {
103
126
  log(`refuse sandbox ${action} ${id} ${who}: ${refused.error}`);
104
127
  (0, http_1.send)(res, refused.status, { error: refused.error });
@@ -133,15 +156,15 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
133
156
  if ('branch' in body) {
134
157
  const pushed = await sandboxes.push(principal, id, body.branch, { ...author(principal), message: body.message }, (0, policy_1.sandboxPushBranches)(grant, sandbox.repo));
135
158
  log(`sandbox push ${id} ${who} branch=${body.branch} commit=${pushed.commit}`);
136
- (0, http_1.send)(res, 200, { branch: body.branch, commit: pushed.commit, sandbox: view(sandboxes, pushed) });
159
+ (0, http_1.send)(res, 200, { branch: body.branch, commit: pushed.commit, sandbox: show(pushed) });
137
160
  return true;
138
161
  }
139
162
  const fetched = await sandboxes.fetch(principal, id, body.ref);
140
163
  log(`sandbox fetch ${id} ${who} ref=${body.ref} commit=${fetched.commit}`);
141
- (0, http_1.send)(res, 200, view(sandboxes, fetched));
164
+ (0, http_1.send)(res, 200, show(fetched));
142
165
  }
143
166
  else if (method === 'GET') {
144
- (0, http_1.send)(res, 200, view(sandboxes, sandbox));
167
+ (0, http_1.send)(res, 200, show(sandbox));
145
168
  }
146
169
  else if (method === 'DELETE') {
147
170
  // a caller who lost the entitlement can still delete what they keep
@@ -206,11 +229,39 @@ function author(principal) {
206
229
  return { name, email: `${local}@${host}` };
207
230
  }
208
231
  exports.author = author;
209
- // What the owner sees: everything but the owner, and the run that holds it.
210
- function view(sandboxes, sandbox) {
232
+ /**
233
+ * Whether a request only reads a sandbox, which who observes may: the sandbox, its tree, a
234
+ * file, a search and its changes. Edits, a write of a file, a fetch, a push and a delete
235
+ * change it.
236
+ */
237
+ function isRead(action, method) {
238
+ switch (action) {
239
+ case undefined:
240
+ return method === 'GET';
241
+ case 'tree':
242
+ case 'changes':
243
+ return method === 'GET';
244
+ case 'files':
245
+ return method === 'GET' || method === 'HEAD';
246
+ case 'search':
247
+ // ripgrep's query is a body
248
+ return method === 'POST';
249
+ default:
250
+ return false;
251
+ }
252
+ }
253
+ // A sandbox as the caller sees it, and the run that holds it: their own without its owner,
254
+ // one of somebody else (who observes) with whose it is, as a run of somebody else names it.
255
+ function view(config, sandboxes, principal, sandbox) {
211
256
  const { owner, ...visible } = sandbox;
212
- void owner;
213
257
  const run = sandboxes.holder(sandbox.id);
214
- return { ...visible, busy: run !== undefined, run: run ?? null };
258
+ const mine = owner.iss === principal.iss && owner.sub === principal.sub;
259
+ const name = owner.name ?? (0, policy_1.subjectName)(config.entitlements, owner);
260
+ return {
261
+ ...visible,
262
+ ...(mine ? {} : { owner: { iss: owner.iss, sub: owner.sub, ...(name ? { name } : {}) } }),
263
+ busy: run !== undefined,
264
+ run: run ?? null,
265
+ };
215
266
  }
216
267
  //# sourceMappingURL=sandboxes-api.js.map
@@ -6,6 +6,7 @@ const promises_1 = require("fs/promises");
6
6
  const path_1 = require("path");
7
7
  const units_1 = require("../utils/units");
8
8
  const git_checkout_1 = require("./git-checkout");
9
+ const oauth_1 = require("./oauth");
9
10
  const runs_1 = require("./runs");
10
11
  const repo_1 = require("./repo");
11
12
  const sandbox_storage_1 = require("./sandbox-storage");
@@ -22,7 +23,8 @@ class SandboxError extends Error {
22
23
  exports.SandboxError = SandboxError;
23
24
  /**
24
25
  * The sandboxes of a server: who owns which, the limits, and one change at a time per
25
- * sandbox. Everything is scoped to the owner: somebody else's sandbox is "not found".
26
+ * sandbox. Everything is scoped to the owner: somebody else's sandbox is "not found". Only
27
+ * `all` and `peek` show the sandboxes of everybody, for who may read them (`observe`).
26
28
  */
27
29
  class SandboxRegistry {
28
30
  constructor(options) {
@@ -111,6 +113,18 @@ class SandboxRegistry {
111
113
  const sandbox = this.sandboxes.get(id);
112
114
  return sandbox && owns(owner, sandbox) && !this.deleting.has(id) ? copy(sandbox) : undefined;
113
115
  }
116
+ // The sandboxes of everybody, oldest first: what who observes sees.
117
+ all() {
118
+ return [...this.sandboxes.values()]
119
+ .filter((sandbox) => !this.deleting.has(sandbox.id))
120
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt))
121
+ .map(copy);
122
+ }
123
+ // A sandbox of anybody, for who observes: to read, never to change.
124
+ peek(id) {
125
+ const sandbox = this.sandboxes.get(id);
126
+ return sandbox && !this.deleting.has(id) ? copy(sandbox) : undefined;
127
+ }
114
128
  holder(id) {
115
129
  return this.runs.holder(id);
116
130
  }
@@ -188,9 +202,10 @@ class SandboxRegistry {
188
202
  }
189
203
  this.creating.set(id, { owner: { iss: owner.iss, sub: owner.sub }, bytes });
190
204
  const at = this.timestamp();
205
+ const name = (0, oauth_1.displayName)(owner);
191
206
  const sandbox = {
192
207
  id,
193
- owner: { iss: owner.iss, sub: owner.sub },
208
+ owner: { iss: owner.iss, sub: owner.sub, ...(name ? { name } : {}) },
194
209
  repo,
195
210
  ref,
196
211
  commit,
@@ -548,8 +563,9 @@ function owns(owner, sandbox) {
548
563
  const of = 'owner' in sandbox ? sandbox.owner : sandbox;
549
564
  return of.iss === owner.iss && of.sub === owner.sub;
550
565
  }
566
+ // what the storage keeps a sandbox by: its owner's identity, not the name they had
551
567
  function keyOf(sandbox) {
552
- return { id: sandbox.id, owner: sandbox.owner };
568
+ return { id: sandbox.id, owner: { iss: sandbox.owner.iss, sub: sandbox.owner.sub } };
553
569
  }
554
570
  function copy(sandbox) {
555
571
  return {
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.detectPlatform = exports.getSecretCatalog = exports.readServerConfig = exports.getServerAccounts = exports.getSandboxRuntimes = exports.serverConfigSchema = exports.sourceSchema = exports.entitlementSchema = exports.endpointGrantSchema = exports.endpointLevelSchema = exports.sandboxGrantSchema = exports.sandboxBranchTypeSchema = exports.buildsLevelSchema = exports.accountLevelSchema = exports.getMaxVolumeSize = exports.DEFAULT_MAX_VOLUME_SIZE = exports.DEFAULT_DENIED_EGRESS = exports.kubernetesPoolSchema = exports.accountSchema = exports.issuerSchema = exports.serverPlatformSchema = void 0;
3
+ exports.getMaxQuotaIssues = exports.detectPlatform = exports.getSecretCatalog = exports.readServerConfig = exports.getServerAccounts = exports.getSandboxRuntimes = exports.serverConfigSchema = exports.sourceSchema = exports.entitlementSchema = exports.endpointGrantSchema = exports.endpointLevelSchema = exports.sandboxGrantSchema = exports.sandboxBranchTypeSchema = exports.buildsLevelSchema = exports.accountLevelSchema = exports.getMaxVolumeSize = exports.DEFAULT_MAX_VOLUME_SIZE = exports.DEFAULT_DENIED_EGRESS = exports.kubernetesPoolSchema = exports.accountSchema = exports.issuerSchema = exports.serverPlatformSchema = void 0;
4
4
  const run_source_envs_1 = require("../environment/run-source-envs");
5
5
  const net_1 = require("net");
6
6
  const zod_1 = require("zod");
7
7
  const yaml_1 = require("yaml");
8
8
  const log_1 = require("../log");
9
9
  const units_1 = require("../utils/units");
10
+ const borrowed_quota_1 = require("./borrowed-quota");
10
11
  const version_1 = require("../version");
11
12
  const network_schema_1 = require("../schema/network-schema");
12
13
  const capabilities_schema_1 = require("../schema/capabilities-schema");
@@ -275,8 +276,12 @@ const kubernetesBackendSchema = zod_1.z
275
276
  })
276
277
  .strict())
277
278
  .default({}),
278
- // the ResourceQuota of every caller's namespace, such as `requests.cpu: '8'`
279
+ // the ResourceQuota of every caller's namespace, such as `requests.cpu: '8'`: what each
280
+ // caller is promised
279
281
  quota: zod_1.z.record(zod_1.z.union([zod_1.z.string().min(1), zod_1.z.number()]).transform(String)).optional(),
282
+ // how far the CPU and memory of a caller's quota rise above `quota` while the nodes of the
283
+ // pools have room no other caller is promised (see BorrowedQuotas); never when left out
284
+ maxQuota: zod_1.z.record(zod_1.z.union([zod_1.z.string().min(1), zod_1.z.number()]).transform(String)).optional(),
280
285
  // what a container without `resources` requests (a LimitRange), so it is
281
286
  // admitted under a quota that limits compute
282
287
  defaultResources: zod_1.z
@@ -854,6 +859,13 @@ exports.serverConfigSchema = zod_1.z
854
859
  }
855
860
  }
856
861
  }
862
+ for (const issue of getMaxQuotaIssues(backend.quota, backend.maxQuota)) {
863
+ ctx.addIssue({
864
+ code: zod_1.z.ZodIssueCode.custom,
865
+ path: ['backend', 'maxQuota', ...issue.path],
866
+ message: issue.message,
867
+ });
868
+ }
857
869
  if (backend.maxVolumeSize && (0, units_1.parseSize)(backend.volumeSize) > (0, units_1.parseSize)(backend.maxVolumeSize)) {
858
870
  ctx.addIssue({
859
871
  code: zod_1.z.ZodIssueCode.custom,
@@ -1071,4 +1083,49 @@ function detectPlatform() {
1071
1083
  return { os, arch };
1072
1084
  }
1073
1085
  exports.detectPlatform = detectPlatform;
1086
+ // What is wrong with `backend.maxQuota`: a key a caller may borrow (CPU and memory, see
1087
+ // BORROWED_RESOURCES) that `quota` has too, no less than there, and a limit only with the
1088
+ // requests of its resource, which say how much room the caller takes of the nodes.
1089
+ function getMaxQuotaIssues(quota, maxQuota) {
1090
+ if (!maxQuota) {
1091
+ return [];
1092
+ }
1093
+ if (!quota) {
1094
+ return [{ path: [], message: 'a caller borrows above backend.quota, which is not set' }];
1095
+ }
1096
+ const issues = [];
1097
+ for (const [key, value] of Object.entries(maxQuota)) {
1098
+ const resource = (0, borrowed_quota_1.getBorrowedResource)(key);
1099
+ if (!resource) {
1100
+ issues.push({
1101
+ path: [key],
1102
+ message: `${key} can't be borrowed, only ${Object.values(borrowed_quota_1.BORROWED_RESOURCES)
1103
+ .flatMap((keys) => [...keys.requests, ...keys.limits])
1104
+ .join(', ')}`,
1105
+ });
1106
+ continue;
1107
+ }
1108
+ if (quota[key] === undefined) {
1109
+ issues.push({ path: [key], message: `${key} is not in backend.quota, the share it rises from` });
1110
+ continue;
1111
+ }
1112
+ const max = (0, units_1.parseQuantity)(value);
1113
+ const share = (0, units_1.parseQuantity)(quota[key]);
1114
+ if (max === undefined) {
1115
+ issues.push({ path: [key], message: `expected a quantity such as 8, 500m or 16Gi` });
1116
+ }
1117
+ else if (share !== undefined && max < share) {
1118
+ issues.push({ path: [key], message: `${value} is less than ${quota[key]} of backend.quota` });
1119
+ }
1120
+ const requests = borrowed_quota_1.BORROWED_RESOURCES[resource].requests;
1121
+ if (!requests.includes(key) && !requests.some((other) => maxQuota[other] !== undefined)) {
1122
+ issues.push({
1123
+ path: [key],
1124
+ message: `${key} rises with the requests of ${resource}, add ${requests[0]} to backend.maxQuota`,
1125
+ });
1126
+ }
1127
+ }
1128
+ return issues;
1129
+ }
1130
+ exports.getMaxQuotaIssues = getMaxQuotaIssues;
1074
1131
  //# sourceMappingURL=server-config.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "2.0.0",
4
+ "version": "2.1.0-next.10",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",