hammerkit 2.0.0-next.7 → 2.0.0-next.9

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.
@@ -6,6 +6,7 @@ const abort_event_1 = require("../utils/abort-event");
6
6
  const apply_1 = require("./apply");
7
7
  const sleep_1 = require("../utils/sleep");
8
8
  const cluster_state_1 = require("./cluster-state");
9
+ const quota_wait_1 = require("./quota-wait");
9
10
  // The waits below poll instead of using the client's Watch: Watch aborts every
10
11
  // request after 30s, which a pod or deployment still pulling its image outlasts.
11
12
  async function awaitRunningState(instance, env, name, phase, pollInterval = 1000) {
@@ -22,11 +23,16 @@ async function awaitRunningState(instance, env, name, phase, pollInterval = 1000
22
23
  }
23
24
  }
24
25
  exports.awaitRunningState = awaitRunningState;
25
- // Wait for a job to finish by polling its status: resolves once it succeeded,
26
- // rejects when it failed (its `Failed` condition, i.e. retries are exhausted).
27
- // On abort the job is deleted — with its pods — and an AbortError is thrown, so
28
- // a cancelled or timed-out task leaves nothing running on the cluster.
29
- async function awaitJobCompletion(instance, env, name, abort, pollInterval = 1000) {
26
+ async function awaitJobCompletion(instance, env, name, abort, pollInterval = 1000, quota) {
27
+ const wait = quota
28
+ ? new quota_wait_1.QuotaWait(instance, env, {
29
+ subject: `the pod of job ${name}`,
30
+ timeout: quota.timeout,
31
+ onWaiting: quota.onWaiting,
32
+ })
33
+ : null;
34
+ // when the job last had a pod: a refusal before it is not one of the pod it waits for now
35
+ let hadPod = 0;
30
36
  for (;;) {
31
37
  if (abort.aborted) {
32
38
  await deleteJob(instance, env, name);
@@ -41,10 +47,50 @@ async function awaitJobCompletion(instance, env, name, abort, pollInterval = 100
41
47
  if (failed) {
42
48
  throw new Error(`job ${name} failed: ${failed.message ?? failed.reason ?? 'unknown reason'}`);
43
49
  }
50
+ if (wait) {
51
+ let refusal = null;
52
+ if (status?.active || status?.ready) {
53
+ hadPod = Date.now();
54
+ }
55
+ else {
56
+ // the job controller can't make the pod: no pod to wait on, only its events say why
57
+ refusal = await readJobRefusal(instance, env, name, job, hadPod);
58
+ }
59
+ try {
60
+ await wait.check(refusal);
61
+ }
62
+ catch (e) {
63
+ await deleteJob(instance, env, name);
64
+ throw e;
65
+ }
66
+ }
44
67
  await sleep(pollInterval, abort);
45
68
  }
46
69
  }
47
70
  exports.awaitJobCompletion = awaitJobCompletion;
71
+ // The last `FailedCreate` of the job for its quota after `after`. Events the server may not list
72
+ // leave the wait as it was.
73
+ async function readJobRefusal(instance, env, name, job, after) {
74
+ try {
75
+ const events = await instance.coreApi.listNamespacedEvent({
76
+ namespace: env.namespace,
77
+ fieldSelector: `involvedObject.kind=Job,involvedObject.name=${name},reason=FailedCreate`,
78
+ });
79
+ const uid = job.metadata?.uid;
80
+ const latest = events.items
81
+ .filter((event) => !uid || !event.involvedObject?.uid || event.involvedObject.uid === uid)
82
+ .filter((event) => eventTime(event) > after && (0, quota_wait_1.getQuotaRefusal)(event.message))
83
+ .sort((a, b) => eventTime(b) - eventTime(a))[0];
84
+ return (0, quota_wait_1.getQuotaRefusal)(latest?.message);
85
+ }
86
+ catch {
87
+ return null;
88
+ }
89
+ }
90
+ function eventTime(event) {
91
+ const at = event.lastTimestamp ?? event.eventTime ?? event.firstTimestamp ?? event.metadata?.creationTimestamp;
92
+ return at ? new Date(at).getTime() : 0;
93
+ }
48
94
  function sleep(ms, abort) {
49
95
  if (abort.aborted) {
50
96
  return Promise.resolve();
@@ -77,6 +123,20 @@ async function deleteJob(instance, env, name) {
77
123
  }
78
124
  }
79
125
  exports.deleteJob = deleteJob;
126
+ // The Deployment says when its ReplicaSet can't make a pod: `ReplicaFailure`, `FailedCreate`.
127
+ function getDeploymentRefusal(deployment) {
128
+ const failure = deployment.status?.conditions?.find((condition) => condition.type === 'ReplicaFailure' && condition.status === 'True');
129
+ return (0, quota_wait_1.getQuotaRefusal)(failure?.message);
130
+ }
131
+ function getDeployQuotaWait(instance, env, name, readiness, now) {
132
+ return new quota_wait_1.QuotaWait(instance, env, {
133
+ subject: readiness.subject ?? name,
134
+ // without a limit of its own it waits as long as the run does
135
+ timeout: readiness.quotaTimeout ?? Number.POSITIVE_INFINITY,
136
+ onWaiting: (text) => readiness.onWaiting?.(text),
137
+ now,
138
+ });
139
+ }
80
140
  // how long a reason that may still pass (an image pull a registry refused for now, a
81
141
  // container the runtime could not create) may last before it fails the service
82
142
  exports.PASSING_FAILURE_MS = 60000;
@@ -96,12 +156,14 @@ async function awaitDeployRunningState(instance, env, name, readiness, pollInter
96
156
  let said;
97
157
  // since when a pod has had a failure that may still pass, by pod
98
158
  const failingSince = new Map();
159
+ const quota = getDeployQuotaWait(instance, env, name, readiness, now);
99
160
  for (;;) {
100
161
  const deployment = await instance.appsApi.readNamespacedDeployment({ name, namespace: env.namespace });
101
162
  // available: ready for the Deployment's minReadySeconds
102
163
  if ((deployment.status?.availableReplicas ?? 0) > 0) {
103
164
  return;
104
165
  }
166
+ await quota.check(getDeploymentRefusal(deployment));
105
167
  const created = deployment.metadata?.creationTimestamp;
106
168
  const listed = await instance.coreApi.listNamespacedPod({
107
169
  namespace: env.namespace,
@@ -163,6 +225,7 @@ async function awaitDeployRollout(instance, env, name, readiness, pollInterval =
163
225
  let runningSince = null;
164
226
  let said;
165
227
  const failingSince = new Map();
228
+ const quota = getDeployQuotaWait(instance, env, name, readiness, now);
166
229
  for (;;) {
167
230
  if (readiness.signal?.aborted) {
168
231
  throw new abort_1.AbortError();
@@ -179,6 +242,7 @@ async function awaitDeployRollout(instance, env, name, readiness, pollInterval =
179
242
  if (stalled && (status?.observedGeneration ?? 0) >= readiness.generation) {
180
243
  throw new Error(`${timeoutMessage}: ${stalled.message ?? 'its rollout made no progress'}`);
181
244
  }
245
+ await quota.check(getDeploymentRefusal(deployment));
182
246
  const listed = await instance.coreApi.listNamespacedPod({
183
247
  namespace: env.namespace,
184
248
  labelSelector: readiness.podSelector,
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getServiceContainerName = exports.MIN_READY_SECONDS = exports.getPodTemplateHash = exports.POD_ANNOTATION = exports.ROLLOUT_DEADLINE_SECONDS = exports.ensureKubernetesDeploymentExists = exports.DEFINITION_LABEL = void 0;
3
+ exports.getServiceContainerName = exports.MIN_READY_SECONDS = exports.getPodTemplateHash = exports.POD_ANNOTATION = exports.ROLLOUT_DEADLINE_SECONDS = exports.ensureKubernetesDeploymentExists = exports.DEBUG_CONTAINER_RESOURCES = exports.DEFINITION_LABEL = void 0;
4
4
  const service_definition_1 = require("../planner/service-definition");
5
5
  const work_service_1 = require("../planner/work-service");
6
6
  const volumes_1 = require("./volumes");
@@ -17,10 +17,15 @@ const job_placement_1 = require("./job-placement");
17
17
  const network_policy_1 = require("./network-policy");
18
18
  const egress_proxy_1 = require("../network/egress-proxy");
19
19
  const service_rollouts_1 = require("./service-rollouts");
20
+ const quota_wait_1 = require("./quota-wait");
20
21
  // what the service was created from, to tell a changed one
21
22
  exports.DEFINITION_LABEL = 'hammerkit.dev/definition';
22
23
  // how often a starting container is checked: as the liveness probe
23
24
  const STARTUP_PERIOD_SECONDS = 5;
25
+ exports.DEBUG_CONTAINER_RESOURCES = {
26
+ requests: { cpu: '10m', memory: '16Mi' },
27
+ limits: { cpu: '100m', memory: '256Mi' },
28
+ };
24
29
  async function ensureKubernetesDeploymentExists(instance, env, service, persistence, secrets, stateKey,
25
30
  // the needed services by name, and the proxy of a limited network
26
31
  network,
@@ -131,6 +136,9 @@ rollout = {}) {
131
136
  command: ['sleep'],
132
137
  args: ['3600'],
133
138
  volumeMounts: persistence.mounts.map((m) => m.mount),
139
+ // a shell to look at the service's files: without a size of its own it would get
140
+ // the namespace's default, and count as much against its quota as the service
141
+ resources: exports.DEBUG_CONTAINER_RESOURCES,
134
142
  ...(0, job_placement_1.getContainerSecurity)(env),
135
143
  },
136
144
  ],
@@ -149,6 +157,7 @@ rollout = {}) {
149
157
  subject: `service ${service.name}`,
150
158
  onWaiting: (text) => service.status.write('warn', `${service.name} ${text}`),
151
159
  ...(service.data.restarts !== null ? { restarts: service.data.restarts } : {}),
160
+ quotaTimeout: quota_wait_1.QUOTA_TIMEOUT,
152
161
  };
153
162
  if (env.server?.serviceRun) {
154
163
  await rollOut(instance, env, service, deployment, readiness, rollout);
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ensurePersistentData = exports.packSource = exports.clearStateFiles = exports.ENSURE_OUTPUT_FILES = exports.restoreLiveFiles = exports.syncLiveFiles = exports.SYNC_FILES = exports.getStateDirsInUse = exports.REMOVE_OTHER_STATES = exports.execInPod = exports.clearClaim = exports.getPodForPersistence = exports.applyWithinQuota = exports.QUOTA_TIMEOUT = exports.TRANSFER_POD_RESOURCES = void 0;
4
+ const quota_wait_1 = require("./quota-wait");
5
+ Object.defineProperty(exports, "QUOTA_TIMEOUT", { enumerable: true, get: function () { return quota_wait_1.QUOTA_TIMEOUT; } });
4
6
  const volumes_1 = require("./volumes");
5
7
  const apply_1 = require("./apply");
6
8
  const path_1 = require("path");
@@ -50,12 +52,9 @@ exports.TRANSFER_POD_RESOURCES = {
50
52
  requests: { cpu: '50m', memory: '64Mi' },
51
53
  limits: { memory: '512Mi' },
52
54
  };
53
- // how long a transfer pod waits for room under the quota of its namespace, such as the
54
- // pods of other tasks of the run ending, before its task fails
55
- exports.QUOTA_TIMEOUT = 10 * 60 * 1000;
56
55
  // A Job waits for room under a full quota, a pod is refused: so a transfer pod is
57
56
  // created again until the pods of the tasks it runs beside are done and make room
58
- async function applyWithinQuota(instance, podSpec, service, timeout = exports.QUOTA_TIMEOUT, pollInterval = 2000,
57
+ async function applyWithinQuota(instance, podSpec, service, timeout = quota_wait_1.QUOTA_TIMEOUT, pollInterval = 2000,
59
58
  // a run stopped while it waits: it stops waiting
60
59
  signal) {
61
60
  const deadline = Date.now() + timeout;
@@ -155,7 +154,7 @@ signal, fn) {
155
154
  };
156
155
  service.status.write('debug', `create pod ${name}`);
157
156
  await removeStalePod(instance, env.namespace, name);
158
- const pod = await applyWithinQuota(instance, podSpec, service, exports.QUOTA_TIMEOUT, 2000, signal);
157
+ const pod = await applyWithinQuota(instance, podSpec, service, quota_wait_1.QUOTA_TIMEOUT, 2000, signal);
159
158
  if (pod.status?.phase !== 'Running') {
160
159
  service.status.write('debug', `await running state ${name}`);
161
160
  await (0, await_running_state_1.awaitRunningState)(instance, env, name, 'Running');
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.followPodLogs = void 0;
3
+ exports.followPodLogs = exports.MAX_LOG_LINE_CHARS = void 0;
4
4
  const stream_1 = require("stream");
5
- const readline_1 = require("readline");
6
5
  const log_1 = require("../log");
6
+ const read_lines_1 = require("../utils/read-lines");
7
7
  // what is read of an instance of a container that already ended
8
8
  const MAX_ENDED_LOG_BYTES = 1024 * 1024;
9
9
  // how often in a row the log of one instance is read again after its reading broke off at once
10
10
  const MAX_READS = 20;
11
+ // the most of one line of a followed log that is held, its timestamp included
12
+ exports.MAX_LOG_LINE_CHARS = 64 * 1024;
11
13
  // `timestamps: true` puts the time a line was written before it (RFC3339 with up to nine
12
14
  // digits of a second), which says where a log that broke off got to
13
15
  const TIMESTAMPED = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?Z(?: (.*))?$/;
@@ -137,7 +139,7 @@ function followPodLogs(instance, namespace, labelSelector, container, onLine, op
137
139
  end();
138
140
  }));
139
141
  output.on('end', end);
140
- (0, readline_1.createInterface)({ input: output }).on('line', reader(position));
142
+ (0, read_lines_1.readLines)(output, exports.MAX_LOG_LINE_CHARS, reader(position));
141
143
  streaming++;
142
144
  let controller;
143
145
  try {
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QuotaWait = exports.exceedsQuota = exports.getQuotaRefusal = exports.QUOTA_TIMEOUT = void 0;
4
+ const units_1 = require("../utils/units");
5
+ // How long a pod the quota of its namespace refuses waits for room before what needs it fails:
6
+ // a transfer pod in any case, a task's or service's only when no pod that will end holds the
7
+ // room (see QuotaWait)
8
+ exports.QUOTA_TIMEOUT = 10 * 60 * 1000;
9
+ // What a refusal of the quota of the namespace says, from where it starts: `exceeded quota:
10
+ // hammerkit, requested: limits.memory=4Gi, used: limits.memory=24Gi, limited: limits.memory=26Gi`
11
+ function getQuotaRefusal(message) {
12
+ const index = message ? message.search(/exceeded quota/i) : -1;
13
+ return message && index >= 0 ? message.slice(index) : null;
14
+ }
15
+ exports.getQuotaRefusal = getQuotaRefusal;
16
+ // A pod that asks for more of a resource than the quota allows at all never fits.
17
+ function exceedsQuota(refusal) {
18
+ const requested = readAmounts(/requested: (\S+?)(?:,\s|$)/.exec(refusal)?.[1]);
19
+ const limited = readAmounts(/limited: (\S+?)(?:,\s|$)/.exec(refusal)?.[1]);
20
+ return [...requested].some(([resource, amount]) => {
21
+ const limit = limited.get(resource);
22
+ return limit !== undefined && amount > limit;
23
+ });
24
+ }
25
+ exports.exceedsQuota = exceedsQuota;
26
+ // `limits.memory=4Gi,requests.cpu=1` → limits.memory 4Gi, requests.cpu 1
27
+ function readAmounts(list = '') {
28
+ const amounts = new Map();
29
+ for (const entry of list.split(',')) {
30
+ const [resource, value] = entry.split('=');
31
+ const amount = (0, units_1.parseQuantity)(value);
32
+ if (resource && amount !== undefined) {
33
+ amounts.set(resource, amount);
34
+ }
35
+ }
36
+ return amounts;
37
+ }
38
+ // A pod that will end and free its room: one of a Job (a task) or a pod of its own (the copy of
39
+ // a task's files). The pods of a Deployment (a service) stay as long as what needs them.
40
+ function willEnd(pod) {
41
+ if (pod.metadata?.deletionTimestamp || (pod.status?.phase !== 'Pending' && pod.status?.phase !== 'Running')) {
42
+ return false;
43
+ }
44
+ return !(pod.metadata?.ownerReferences ?? []).some((owner) => owner.kind === 'ReplicaSet');
45
+ }
46
+ /**
47
+ * The wait of a pod the quota of its namespace refuses. It is told what the quota refused,
48
+ * fails at once when the pod asks for more than the quota allows at all, and after `timeout`
49
+ * goes on waiting only while a pod that will end (a task's, another run's) holds room in the
50
+ * namespace: when only services hold it, they wait for what waits for them, and it fails.
51
+ */
52
+ class QuotaWait {
53
+ constructor(instance, env, options) {
54
+ this.instance = instance;
55
+ this.env = env;
56
+ this.options = options;
57
+ this.since = null;
58
+ this.told = '';
59
+ }
60
+ // Called on each poll with what the quota refused now, or null when nothing is refused.
61
+ async check(refusal) {
62
+ const now = this.options.now ?? Date.now;
63
+ if (!refusal) {
64
+ this.since = null;
65
+ return;
66
+ }
67
+ if (refusal !== this.told) {
68
+ this.told = refusal;
69
+ this.options.onWaiting?.(`waits for room under the quota of the namespace: ${refusal}`);
70
+ }
71
+ if (exceedsQuota(refusal)) {
72
+ throw new Error(`${this.options.subject} asks for more than the quota of the namespace allows: ${refusal}`);
73
+ }
74
+ this.since = this.since ?? now();
75
+ if (now() - this.since < this.options.timeout) {
76
+ return;
77
+ }
78
+ if (await this.othersWillEnd()) {
79
+ // checked again after another `timeout`
80
+ this.since = now();
81
+ return;
82
+ }
83
+ throw new Error(`${this.options.subject} did not fit the quota of the namespace within ${Math.round(this.options.timeout / 60000)} minutes, and only services hold its room: ${refusal}`);
84
+ }
85
+ async othersWillEnd() {
86
+ try {
87
+ const pods = await this.instance.coreApi.listNamespacedPod({ namespace: this.env.namespace });
88
+ return pods.items.some(willEnd);
89
+ }
90
+ catch {
91
+ // what it can not see may still end
92
+ return true;
93
+ }
94
+ }
95
+ }
96
+ exports.QuotaWait = QuotaWait;
97
+ //# sourceMappingURL=quota-wait.js.map
@@ -1,12 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.read = exports.write = void 0;
3
+ exports.readSized = exports.read = exports.write = void 0;
4
4
  const yaml_1 = require("yaml");
5
5
  async function write(filename, content, context) {
6
6
  await context.file.writeFile(filename, (0, yaml_1.stringify)(content));
7
7
  }
8
8
  exports.write = write;
9
9
  async function read(fileName, context) {
10
+ return (await readSized(fileName, context)).value;
11
+ }
12
+ exports.read = read;
13
+ // The parsed file and the bytes it took.
14
+ async function readSized(fileName, context) {
10
15
  context.status.context({ type: 'cli', name: 'hammerkit' }).write('debug', `read ${fileName} build file`);
11
16
  let content;
12
17
  try {
@@ -16,7 +21,7 @@ async function read(fileName, context) {
16
21
  throw new Error(`unable to read ${fileName}`);
17
22
  }
18
23
  try {
19
- return (0, yaml_1.parse)(content);
24
+ return { value: (0, yaml_1.parse)(content), bytes: Buffer.byteLength(content, 'utf8') };
20
25
  }
21
26
  catch (e) {
22
27
  if (e instanceof Error) {
@@ -27,5 +32,5 @@ async function read(fileName, context) {
27
32
  }
28
33
  }
29
34
  }
30
- exports.read = read;
35
+ exports.readSized = readSized;
31
36
  //# sourceMappingURL=read-build-file.js.map
@@ -5,11 +5,27 @@ function templateValue(value, envs) {
5
5
  if (!value || !envs) {
6
6
  return value;
7
7
  }
8
- let result = value;
9
- for (const [key, envValue] of Object.entries(envs.variables)) {
10
- result = result.replace(new RegExp(`\\$\\{${key}\\}|\\$${key}`, 'gi'), envValue);
8
+ // A name is matched as the text it is, in any case; when two names match the same `$`, the
9
+ // one listed first wins. One pass over the value: a value put in is not read again.
10
+ const names = Object.keys(envs.variables).filter((name) => name.length > 0);
11
+ if (names.length === 0) {
12
+ return value;
13
+ }
14
+ const values = new Map();
15
+ for (const name of names) {
16
+ const key = name.toLowerCase();
17
+ if (!values.has(key)) {
18
+ values.set(key, envs.variables[name]);
19
+ }
11
20
  }
12
- return result;
21
+ const alternatives = names.map(escapeRegExp).join('|');
22
+ const pattern = new RegExp(`\\$\\{(${alternatives})\\}|\\$(${alternatives})`, 'gi');
23
+ return value.replace(pattern, (match, braced, bare) => {
24
+ return values.get((braced ?? bare ?? '').toLowerCase()) ?? match;
25
+ });
13
26
  }
14
27
  exports.templateValue = templateValue;
28
+ function escapeRegExp(text) {
29
+ return text.replace(/[\\^$.*+?()[\]{}|/-]/g, '\\$&');
30
+ }
15
31
  //# sourceMappingURL=template-value.js.map
@@ -385,7 +385,10 @@ function kubernetesTaskRuntime(task, kubernetes) {
385
385
  try {
386
386
  if (!pod.status?.succeeded) {
387
387
  try {
388
- await (0, await_running_state_1.awaitJobCompletion)(instance, kubernetes, spec.metadata.name, options.abort);
388
+ await (0, await_running_state_1.awaitJobCompletion)(instance, kubernetes, spec.metadata.name, options.abort, 1000, {
389
+ onWaiting: (text) => task.status.write('warn', `${task.name} ${text}`),
390
+ timeout: ensure_persistent_data_1.QUOTA_TIMEOUT,
391
+ });
389
392
  }
390
393
  catch (e) {
391
394
  if (!(e instanceof abort_1.AbortError)) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.appendBuildFile = exports.createParseContext = void 0;
3
+ exports.appendBuildFile = exports.MAX_BUILD_FILE_ITEMS = exports.MAX_BUILD_FILE_BYTES = exports.MAX_INCLUDE_INPUT_BYTES = exports.MAX_BUILD_FILES = exports.MAX_INCLUDE_DEPTH = exports.createParseContext = void 0;
4
4
  const build_file_schema_1 = require("./build-file-schema");
5
5
  const read_build_file_1 = require("../parser/read-build-file");
6
6
  const path_1 = require("path");
@@ -20,6 +20,17 @@ async function createParseContext(fileName, environment, options) {
20
20
  return { ctx, scope };
21
21
  }
22
22
  exports.createParseContext = createParseContext;
23
+ // How deep includes and references may nest, how many build files one parse may read (a
24
+ // file included with other inputs counts again), and how large the inputs one include
25
+ // passes on may be. A file that includes itself with inputs that change each time ends at
26
+ // these instead of reading on without end.
27
+ exports.MAX_INCLUDE_DEPTH = 32;
28
+ exports.MAX_BUILD_FILES = 1000;
29
+ exports.MAX_INCLUDE_INPUT_BYTES = 64 * 1024;
30
+ // What the build files of one parse may take together, and how many tasks and services they
31
+ // may declare, a file included again counted again.
32
+ exports.MAX_BUILD_FILE_BYTES = 16 * 1024 * 1024;
33
+ exports.MAX_BUILD_FILE_ITEMS = 10000;
23
34
  async function appendBuildFile(cwd, ctx, environment, fileName, namePrefix, remote, inputs,
24
35
  // on a server: the file is included with `publish: false`, or below such an include
25
36
  leftOut = false) {
@@ -30,12 +41,26 @@ leftOut = false) {
30
41
  if (ctx.files[key]) {
31
42
  return ctx.files[key];
32
43
  }
33
- const input = await (0, read_build_file_1.read)(fileName, environment);
44
+ if (namePrefix.length > exports.MAX_INCLUDE_DEPTH) {
45
+ throw new Error(`${relativeName} (${fileName}) is included ${namePrefix.length} levels deep, at most ${exports.MAX_INCLUDE_DEPTH} are read`);
46
+ }
47
+ if (Object.keys(ctx.files).length >= exports.MAX_BUILD_FILES) {
48
+ throw new Error(`${relativeName} (${fileName}): the build file includes more than ${exports.MAX_BUILD_FILES} files, counting a file included with other inputs again`);
49
+ }
50
+ if (inputs && Buffer.byteLength(JSON.stringify(inputs), 'utf8') > exports.MAX_INCLUDE_INPUT_BYTES) {
51
+ throw new Error(`${relativeName} (${fileName}): the inputs passed to it take more than ${exports.MAX_INCLUDE_INPUT_BYTES} bytes`);
52
+ }
53
+ const input = await readCounted(ctx, fileName, environment, relativeName);
34
54
  (0, removed_properties_1.assertNoRemovedProperties)(input, fileName);
35
55
  const result = await build_file_schema_1.buildFileSchema.safeParseAsync(input);
36
56
  if (!result.success) {
37
57
  throw new parse_error_1.ParseError(result.error, fileName);
38
58
  }
59
+ const read = readOf(ctx);
60
+ read.items += Object.keys(result.data.tasks ?? {}).length + Object.keys(result.data.services ?? {}).length;
61
+ if (read.items > exports.MAX_BUILD_FILE_ITEMS) {
62
+ throw new Error(`${relativeName} (${fileName}): the build file declares more than ${exports.MAX_BUILD_FILE_ITEMS} tasks and services, counting a file included with other inputs again`);
63
+ }
39
64
  const scope = {
40
65
  fileName,
41
66
  cwd,
@@ -77,6 +102,25 @@ leftOut = false) {
77
102
  return scope;
78
103
  }
79
104
  exports.appendBuildFile = appendBuildFile;
105
+ // A build file, parsed once per parse and counted each time it is read.
106
+ async function readCounted(ctx, fileName, environment, relativeName) {
107
+ const read = readOf(ctx);
108
+ let parsed = read.parsed.get(fileName);
109
+ if (!parsed) {
110
+ parsed = await (0, read_build_file_1.readSized)(fileName, environment);
111
+ read.parsed.set(fileName, parsed);
112
+ }
113
+ read.bytes += parsed.bytes;
114
+ if (read.bytes > exports.MAX_BUILD_FILE_BYTES) {
115
+ throw new Error(`${relativeName} (${fileName}): the build file includes more than ${exports.MAX_BUILD_FILE_BYTES} bytes of build files, counting a file included with other inputs again`);
116
+ }
117
+ // every scope gets a value of its own
118
+ return structuredClone(parsed.value);
119
+ }
120
+ function readOf(ctx) {
121
+ ctx.read ?? (ctx.read = { bytes: 0, items: 0, parsed: new Map() });
122
+ return ctx.read;
123
+ }
80
124
  // The scope of a git source a server does not fetch, see ParseScope.notFetched
81
125
  function notFetched(cwd, namePrefix, include) {
82
126
  return {
@@ -7,41 +7,8 @@ const apply_1 = require("../kubernetes/apply");
7
7
  const job_placement_1 = require("../kubernetes/job-placement");
8
8
  const volumes_1 = require("../kubernetes/volumes");
9
9
  const network_policy_1 = require("../kubernetes/network-policy");
10
- const UNITS = {
11
- n: 1e-9,
12
- u: 1e-6,
13
- m: 1e-3,
14
- '': 1,
15
- k: 1e3,
16
- K: 1e3,
17
- M: 1e6,
18
- G: 1e9,
19
- T: 1e12,
20
- P: 1e15,
21
- E: 1e18,
22
- Ki: 1024,
23
- Mi: 1024 ** 2,
24
- Gi: 1024 ** 3,
25
- Ti: 1024 ** 4,
26
- Pi: 1024 ** 5,
27
- Ei: 1024 ** 6,
28
- };
29
- // A quantity of Kubernetes (`250m`, `1.5`, `512Mi`, `12345678n`, `1e3`), as a number.
30
- function parseQuantity(value) {
31
- if (value === undefined) {
32
- return undefined;
33
- }
34
- if (typeof value === 'number') {
35
- return value;
36
- }
37
- const match = /^([+-]?[0-9.]+(?:[eE][+-]?\d+)?)([a-zA-Z]*)$/.exec(value.trim());
38
- if (!match || !(match[2] in UNITS)) {
39
- return undefined;
40
- }
41
- const number = Number(match[1]);
42
- return Number.isFinite(number) ? number * UNITS[match[2]] : undefined;
43
- }
44
- exports.parseQuantity = parseQuantity;
10
+ const units_1 = require("../utils/units");
11
+ Object.defineProperty(exports, "parseQuantity", { enumerable: true, get: function () { return units_1.parseQuantity; } });
45
12
  function add(into, cpu, memory) {
46
13
  if (cpu !== undefined) {
47
14
  into.cpu = (into.cpu ?? 0) + cpu;
@@ -182,8 +149,8 @@ class KubernetesCapacity {
182
149
  use.pods++;
183
150
  for (const container of pod.spec?.containers ?? []) {
184
151
  const requests = container.resources?.requests ?? {};
185
- add(group.requested, parseQuantity(requests.cpu) ?? 0, parseQuantity(requests.memory) ?? 0);
186
- add(use.requested, parseQuantity(requests.cpu) ?? 0, parseQuantity(requests.memory) ?? 0);
152
+ add(group.requested, (0, units_1.parseQuantity)(requests.cpu) ?? 0, (0, units_1.parseQuantity)(requests.memory) ?? 0);
153
+ add(use.requested, (0, units_1.parseQuantity)(requests.cpu) ?? 0, (0, units_1.parseQuantity)(requests.memory) ?? 0);
187
154
  }
188
155
  }
189
156
  }
@@ -196,8 +163,8 @@ class KubernetesCapacity {
196
163
  namespace,
197
164
  });
198
165
  const hard = quota.spec?.hard ?? {};
199
- const cpu = parseQuantity(hard['requests.cpu'] ?? hard.cpu);
200
- const memory = parseQuantity(hard['requests.memory'] ?? hard.memory);
166
+ const cpu = (0, units_1.parseQuantity)(hard['requests.cpu'] ?? hard.cpu);
167
+ const memory = (0, units_1.parseQuantity)(hard['requests.memory'] ?? hard.memory);
201
168
  if (cpu !== undefined || memory !== undefined) {
202
169
  group.quota = { ...(cpu !== undefined ? { cpu } : {}), ...(memory !== undefined ? { memory } : {}) };
203
170
  }
@@ -215,10 +182,10 @@ class KubernetesCapacity {
215
182
  for (const pod of metrics.items) {
216
183
  const use = usesOfPods.get(pod.metadata?.name ?? '');
217
184
  for (const container of pod.containers) {
218
- add(used, parseQuantity(container.usage.cpu), parseQuantity(container.usage.memory));
185
+ add(used, (0, units_1.parseQuantity)(container.usage.cpu), (0, units_1.parseQuantity)(container.usage.memory));
219
186
  if (use) {
220
187
  use.used ?? (use.used = { cpu: 0, memory: 0 });
221
- add(use.used, parseQuantity(container.usage.cpu), parseQuantity(container.usage.memory));
188
+ add(use.used, (0, units_1.parseQuantity)(container.usage.cpu), (0, units_1.parseQuantity)(container.usage.memory));
222
189
  }
223
190
  }
224
191
  }
@@ -248,7 +215,7 @@ class KubernetesCapacity {
248
215
  const allocatable = {};
249
216
  for (const node of nodes.items) {
250
217
  const of = node.status?.allocatable ?? {};
251
- add(allocatable, parseQuantity(of.cpu), parseQuantity(of.memory));
218
+ add(allocatable, (0, units_1.parseQuantity)(of.cpu), (0, units_1.parseQuantity)(of.memory));
252
219
  }
253
220
  machines = { count: nodes.items.length, allocatable };
254
221
  if (this.instance.metrics) {
@@ -256,7 +223,7 @@ class KubernetesCapacity {
256
223
  const metrics = await this.instance.metrics.getNodeMetrics();
257
224
  const nodesUsed = {};
258
225
  for (const node of metrics.items) {
259
- add(nodesUsed, parseQuantity(node.usage.cpu), parseQuantity(node.usage.memory));
226
+ add(nodesUsed, (0, units_1.parseQuantity)(node.usage.cpu), (0, units_1.parseQuantity)(node.usage.memory));
260
227
  }
261
228
  machines.used = nodesUsed;
262
229
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPushRefusal = exports.KubernetesSandboxStorage = exports.getSandboxNames = void 0;
3
+ exports.AnswerReader = exports.getPushRefusal = exports.KubernetesSandboxStorage = exports.getSandboxNames = void 0;
4
4
  const crypto_1 = require("crypto");
5
5
  const promises_1 = require("fs/promises");
6
6
  const path_1 = require("path");
@@ -542,7 +542,9 @@ class KubernetesSandboxStorage {
542
542
  // the process ends when the server stops waiting for it: closing the connection does
543
543
  // not end a command in a pod
544
544
  const seconds = Math.ceil((['create', 'fetch', 'move', 'archive'].includes(request.op) ? FETCH_TIMEOUT_MS : REQUEST_TIMEOUT_MS) / 1000);
545
- const done = this.withPod(key, (pod) => this.exec(pod, ['timeout', String(seconds), 'hammerkit', 'server-sandbox', CLAIM_PATH], stream_1.Readable.from([Buffer.from(JSON.stringify(encoded))]), reader, signal));
545
+ const done = this.withPod(key, (pod) => this.exec(pod, ['timeout', String(seconds), 'hammerkit', 'server-sandbox', CLAIM_PATH], stream_1.Readable.from([Buffer.from(JSON.stringify(encoded))]), reader,
546
+ // a request that failed (an answer too large) stops the command too
547
+ AbortSignal.any([signal, reader.stopped])));
546
548
  done.then(() => reader.complete(), (e) => reader.fail(e));
547
549
  const answer = await reader.answer;
548
550
  if (!answer.header.ok) {
@@ -911,16 +913,22 @@ function holdBack(socket, stdout) {
911
913
  * memory, unless the first line says it is a stream: then it is passed on as it comes.
912
914
  */
913
915
  class AnswerReader extends stream_1.Writable {
914
- constructor(body) {
916
+ constructor(body, maxBytes = MAX_ANSWER_BYTES) {
915
917
  super();
916
918
  this.body = body;
919
+ this.maxBytes = maxBytes;
917
920
  this.chunks = [];
918
921
  this.size = 0;
922
+ this.stop = new AbortController();
923
+ // aborted once the answer failed: nothing reads the rest
924
+ this.stopped = this.stop.signal;
919
925
  this.answer = new Promise((resolve, reject) => {
920
926
  this.resolveAnswer = resolve;
921
927
  this.rejectAnswer = reject;
922
928
  });
923
929
  this.answer.catch(() => undefined);
930
+ // an answer that is too large or no answer at all fails the request, not the server
931
+ this.on('error', (e) => this.fail(e));
924
932
  }
925
933
  _write(chunk, _encoding, callback) {
926
934
  if (this.stream) {
@@ -933,7 +941,7 @@ class AnswerReader extends stream_1.Writable {
933
941
  return;
934
942
  }
935
943
  this.size += chunk.length;
936
- if (this.size > MAX_ANSWER_BYTES) {
944
+ if (this.size > this.maxBytes) {
937
945
  callback(new Error('the answer of the pod of the sandbox is too large'));
938
946
  return;
939
947
  }
@@ -986,6 +994,7 @@ class AnswerReader extends stream_1.Writable {
986
994
  }
987
995
  }
988
996
  fail(e) {
997
+ this.stop.abort();
989
998
  const error = e instanceof Error ? e : new Error(String(e));
990
999
  if (this.stream) {
991
1000
  this.stream.destroy(error);
@@ -993,6 +1002,7 @@ class AnswerReader extends stream_1.Writable {
993
1002
  this.rejectAnswer(error);
994
1003
  }
995
1004
  }
1005
+ exports.AnswerReader = AnswerReader;
996
1006
  // The files of a sandbox on a cluster: each call is a request to the pod of its files.
997
1007
  class KubernetesSandboxFiles {
998
1008
  constructor(request, rawFile) {