hammerkit 1.27.0-next.1 → 1.27.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 (53) hide show
  1. package/build.schema.json +9 -0
  2. package/dist/cache/backends/registry-cache-backend.js +2 -2
  3. package/dist/executer/event-cache.js +20 -2
  4. package/dist/harness/sandbox-client.js +104 -66
  5. package/dist/kubernetes/apply.js +27 -16
  6. package/dist/kubernetes/claim-layout.js +71 -0
  7. package/dist/kubernetes/ensure-http-route.js +1 -1
  8. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +5 -9
  9. package/dist/kubernetes/ensure-kubernetes-persistent-volume-claim-exists.js +1 -1
  10. package/dist/kubernetes/ensure-kubernetes-service-exists.js +4 -9
  11. package/dist/kubernetes/ensure-persistent-data.js +30 -20
  12. package/dist/kubernetes/get-service-ip.js +2 -2
  13. package/dist/kubernetes/job-secrets.js +33 -19
  14. package/dist/kubernetes/kept-claims.js +22 -10
  15. package/dist/kubernetes/network-policy.js +83 -26
  16. package/dist/kubernetes/remove-persistent-data.js +3 -1
  17. package/dist/kubernetes/resources.js +25 -3
  18. package/dist/kubernetes/restore-kubernetes-data.js +9 -3
  19. package/dist/kubernetes/secrets.js +7 -11
  20. package/dist/kubernetes/volumes.js +6 -3
  21. package/dist/log.js +1 -1
  22. package/dist/network/egress-proxy.js +45 -6
  23. package/dist/optimizer/work-task-cache-description.js +2 -0
  24. package/dist/planner/utils/append-work-dependencies.js +5 -2
  25. package/dist/planner/utils/append-work-service.js +1 -0
  26. package/dist/planner/utils/append-work-task.js +4 -1
  27. package/dist/planner/utils/task-arguments.js +3 -0
  28. package/dist/planner/work-runtime-docker.js +2 -1
  29. package/dist/planner/work-runtime-kubernetes.js +74 -48
  30. package/dist/planner/work-runtime-local.js +2 -1
  31. package/dist/program.js +12 -5
  32. package/dist/remote/annotations.js +13 -1
  33. package/dist/remote/remote-commands.js +11 -2
  34. package/dist/remote/remote-work-commands.js +89 -1
  35. package/dist/schema/build-file-base-task-schema.js +5 -1
  36. package/dist/schema/build-file-container-service-schema.js +2 -0
  37. package/dist/schema/reference-parser.js +1 -0
  38. package/dist/secrets/github-app-token-command.js +4 -1
  39. package/dist/server/executor.js +1 -1
  40. package/dist/server/kubernetes-backend.js +80 -31
  41. package/dist/server/kubernetes-sandbox-storage.js +2 -1
  42. package/dist/server/oauth.js +12 -1
  43. package/dist/server/plan.js +46 -10
  44. package/dist/server/run-plan.js +12 -15
  45. package/dist/server/run-request.js +6 -0
  46. package/dist/server/runs.js +10 -3
  47. package/dist/server/sandboxes-api.js +10 -4
  48. package/dist/server/server-command.js +39 -20
  49. package/dist/server/server-config.js +35 -4
  50. package/dist/server/server.js +16 -2
  51. package/dist/utils/abort-on-signals.js +46 -6
  52. package/dist/utils/tar.js +25 -0
  53. package/package.json +1 -1
@@ -5,10 +5,12 @@
5
5
  // `CONNECT` tunnels to an allowed host pass, everything else gets a 403 and a
6
6
  // `refused` event on stdout. For each `host:port` entry it also listens on that
7
7
  // port and forwards raw TCP to that host, which the item reaches under the
8
- // host's name. The script is passed to `node -e`, so the proxy
8
+ // host's name. On a server's cluster it also lets the init containers that read
9
+ // the task's secrets reach the endpoints of their providers: a tunnel to port 443
10
+ // that carries the proxy's token, which those containers get and the task not. The script is passed to `node -e`, so the proxy
9
11
  // needs no image of its own, only a stock Node image.
10
12
  Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.formatEgressProxyEvent = exports.formatRefusedHost = exports.parseEgressProxyEvent = exports.getProxylessEnvironment = exports.getProxyClientEnvironment = exports.getEgressProxyPorts = exports.getItemProxyEnvironment = exports.getEgressProxyEnvironment = exports.getEgressProxyCommand = exports.EGRESS_PROXY_SCRIPT = exports.EGRESS_PROXY_HOST = exports.EGRESS_PROXY_PORT = exports.EGRESS_PROXY_IMAGE = void 0;
13
+ exports.formatEgressProxyEvent = exports.formatRefusedHost = exports.parseEgressProxyEvent = exports.getProxylessEnvironment = exports.getProxyClientEnvironment = exports.getEgressProxyPorts = exports.getItemProxyEnvironment = exports.getSecretProxyEnvironment = exports.getEgressProxyEnvironment = exports.getEgressProxyCommand = exports.EGRESS_PROXY_SCRIPT = exports.EGRESS_PROXY_HOST = exports.EGRESS_PROXY_PORT = exports.EGRESS_PROXY_IMAGE = void 0;
12
14
  const work_network_1 = require("../planner/work-network");
13
15
  const network_schema_1 = require("../schema/network-schema");
14
16
  // Pinned, so the code behind the boundary is the same on every host. To bump
@@ -25,6 +27,7 @@ exports.EGRESS_PROXY_SCRIPT = `
25
27
  const http = require('http')
26
28
  const net = require('net')
27
29
  const dns = require('dns')
30
+ const crypto = require('crypto')
28
31
  const entries = JSON.parse(process.env.HAMMERKIT_ALLOW || '[]').map((h) => h.toLowerCase())
29
32
  // names and patterns for HTTP(S)
30
33
  const allow = entries.filter((e) => !e.includes(':'))
@@ -38,6 +41,9 @@ const publicOnly = JSON.parse(process.env.HAMMERKIT_PUBLIC_ONLY || '[]').map((h)
38
41
  // the names of the needed services, for tools that ignore NO_PROXY: exact
39
42
  // names only, never patterns
40
43
  const needs = JSON.parse(process.env.HAMMERKIT_NEEDS || '[]').map((h) => h.toLowerCase())
44
+ // the endpoints of secret providers, for a request with the token only
45
+ const secretAllow = JSON.parse(process.env.HAMMERKIT_SECRET_ALLOW || '[]').map((h) => h.toLowerCase())
46
+ const secretToken = process.env.HAMMERKIT_SECRET_TOKEN || ''
41
47
  const port = parseInt(process.env.HAMMERKIT_PROXY_PORT || '${exports.EGRESS_PROXY_PORT}', 10)
42
48
  // the name whose address the proxy listens on, every address when unset
43
49
  const bind = process.env.HAMMERKIT_PROXY_BIND
@@ -64,6 +70,31 @@ function allowedBy(host, port) {
64
70
  }
65
71
  return allow.some((rule) => rule.startsWith('*.') && name.endsWith(rule.substring(1))) ? 'wildcard' : null
66
72
  }
73
+ // Whether the request carries the token as the password of Proxy-Authorization (Basic).
74
+ function hasSecretToken(req) {
75
+ if (secretToken.length === 0) {
76
+ return false
77
+ }
78
+ const match = /^basic\\s+(\\S+)$/i.exec(String(req.headers['proxy-authorization'] || ''))
79
+ if (!match) {
80
+ return false
81
+ }
82
+ const credentials = Buffer.from(match[1], 'base64').toString('utf8')
83
+ const colon = credentials.indexOf(':')
84
+ if (colon < 0) {
85
+ return false
86
+ }
87
+ const given = Buffer.from(credentials.substring(colon + 1), 'utf8')
88
+ const expected = Buffer.from(secretToken, 'utf8')
89
+ return given.length === expected.length && crypto.timingSafeEqual(given, expected)
90
+ }
91
+ // the rule of a tunnel to an endpoint of a secret provider: like a wildcard, it reaches
92
+ // no private address
93
+ function allowedForSecrets(req, host, port) {
94
+ const name = host.toLowerCase().replace(/\\.$/, '')
95
+ const listed = secretAllow.some((rule) => (rule.startsWith('*.') ? name.endsWith(rule.substring(1)) : name === rule))
96
+ return listed && port === 443 && hasSecretToken(req) ? 'wildcard' : null
97
+ }
67
98
  function ipv4(address) {
68
99
  const mapped = /^::ffff:(\\d+\\.\\d+\\.\\d+\\.\\d+)$/i.exec(address)
69
100
  const value = mapped ? mapped[1] : address
@@ -197,7 +228,7 @@ const server = http.createServer((req, res) => {
197
228
  server.on('connect', (req, socket, head) => {
198
229
  const target = parseAuthority(req.url || '')
199
230
  socket.on('error', () => socket.destroy())
200
- const rule = target ? allowedBy(target.host, target.port) : null
231
+ const rule = target ? allowedBy(target.host, target.port) || allowedForSecrets(req, target.host, target.port) : null
201
232
  if (!rule) {
202
233
  refuse(target ? target.host : String(req.url))
203
234
  socket.end('HTTP/1.1 403 Forbidden\\r\\ncontent-type: text/plain\\r\\n\\r\\nhammerkit: network access is not allowed\\n')
@@ -290,7 +321,8 @@ exports.getEgressProxyCommand = getEgressProxyCommand;
290
321
  // `needs` are the names of the needed services, allowed as exactly those
291
322
  // names. `bind` is the name the proxy listens on alone, so that nothing on its
292
323
  // other networks reaches it. `publicOnly` are the entries of `hosts` that reach
293
- // no private address (see WorkNetwork).
324
+ // no private address (see WorkNetwork). The endpoints of secret providers and
325
+ // the token they need are set by the caller (getSecretProxyEnvironment).
294
326
  function getEgressProxyEnvironment(hosts, needs, bind, publicOnly = []) {
295
327
  return {
296
328
  HAMMERKIT_ALLOW: JSON.stringify(hosts),
@@ -301,6 +333,12 @@ function getEgressProxyEnvironment(hosts, needs, bind, publicOnly = []) {
301
333
  };
302
334
  }
303
335
  exports.getEgressProxyEnvironment = getEgressProxyEnvironment;
336
+ // The endpoints of secret providers the proxy lets a tunnel with its token reach; the
337
+ // token itself is set where the proxy runs, see HAMMERKIT_SECRET_TOKEN.
338
+ function getSecretProxyEnvironment(hosts) {
339
+ return hosts.length > 0 ? { HAMMERKIT_SECRET_ALLOW: JSON.stringify(hosts) } : {};
340
+ }
341
+ exports.getSecretProxyEnvironment = getSecretProxyEnvironment;
304
342
  // The proxy of a task or service: the hosts of its network in this run (under
305
343
  // the run's ceiling), and the names of the services it needs, on every runtime.
306
344
  function getItemProxyEnvironment(item, bind) {
@@ -315,8 +353,9 @@ function getEgressProxyPorts(network) {
315
353
  return [exports.EGRESS_PROXY_PORT, ...(0, work_network_1.getTcpForwards)(network).map((forward) => forward.port)];
316
354
  }
317
355
  exports.getEgressProxyPorts = getEgressProxyPorts;
318
- function getProxyClientEnvironment(directHosts) {
319
- const proxy = `http://${exports.EGRESS_PROXY_HOST}:${exports.EGRESS_PROXY_PORT}`;
356
+ // `token` is sent as the password, for the endpoints of secret providers.
357
+ function getProxyClientEnvironment(directHosts, token) {
358
+ const proxy = `http://${token !== undefined ? `hammerkit:${token}@` : ''}${exports.EGRESS_PROXY_HOST}:${exports.EGRESS_PROXY_PORT}`;
320
359
  const noProxy = ['localhost', '127.0.0.1', ...directHosts].join(',');
321
360
  return {
322
361
  HTTP_PROXY: proxy,
@@ -6,6 +6,7 @@ const crypto_1 = require("crypto");
6
6
  const replace_env_variables_1 = require("../environment/replace-env-variables");
7
7
  const portable_path_1 = require("../planner/utils/portable-path");
8
8
  const work_secret_1 = require("../planner/work-secret");
9
+ const claim_layout_1 = require("../kubernetes/claim-layout");
9
10
  // Every path is made project-relative (portablePath) so the description, and the
10
11
  // task id hashed from it, is identical wherever the project is checked out.
11
12
  //
@@ -40,6 +41,7 @@ function getWorkTaskCacheDescription(item) {
40
41
  cwd: portable(task.cwd),
41
42
  deps: item.deps.length > 0 ? item.deps.map((dep) => dep.id()).sort() : undefined,
42
43
  secrets: cachedSecrets.length > 0 ? cachedSecrets : undefined,
44
+ claimLayout: (0, claim_layout_1.hasPathsEndingAlike)(item) || (0, claim_layout_1.hasOutputsFlatteningAlike)(item) ? 2 : undefined,
43
45
  };
44
46
  }
45
47
  exports.getWorkTaskCacheDescription = getWorkTaskCacheDescription;
@@ -19,7 +19,10 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
19
19
  }
20
20
  if (((0, work_task_1.isContainerWorkTask)(depNode.data) || (0, work_service_1.isContainerWorkService)(depNode.data)) &&
21
21
  ((0, work_task_1.isContainerWorkTask)(item.data) || (0, work_service_1.isContainerWorkService)(item.data))) {
22
+ // the outputs of the dep's own deps too, which stay those of the task that made them
23
+ // (on Kubernetes, they are in its claim)
22
24
  for (const generate of depNode.data.generates) {
25
+ const inherited = generate.inherited ?? depNode;
23
26
  if (generate.isFile) {
24
27
  // TODO check if thats correct
25
28
  continue;
@@ -31,7 +34,7 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
31
34
  export: false,
32
35
  resetOnChange: false,
33
36
  containerPath: generate.path,
34
- inherited: depNode,
37
+ inherited,
35
38
  readOnly: false,
36
39
  });
37
40
  }
@@ -44,7 +47,7 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
44
47
  exportAlways: false,
45
48
  resetOnChange: false,
46
49
  path: generate.path,
47
- inherited: depNode,
50
+ inherited,
48
51
  isFile: false,
49
52
  });
50
53
  }
@@ -120,6 +120,7 @@ function parseService(workTree, service, environment, context) {
120
120
  volumeSize: service.schema.volumeSize ?? null,
121
121
  network: (0, work_network_1.parseWorkNetwork)(service.schema.network),
122
122
  networkCeiling: null,
123
+ args: service.schema.args ?? null,
123
124
  };
124
125
  }
125
126
  }
@@ -83,6 +83,7 @@ function parseTask(cwd, task, environment, context) {
83
83
  resources: (0, work_resources_1.parseWorkResources)(task.schema.resources),
84
84
  network: (0, work_network_1.parseWorkNetwork)(task.schema.network),
85
85
  networkCeiling: null,
86
+ args: task.schema.args ?? null,
86
87
  runner: task.schema.runner ?? task.scope.schema.runner ?? null,
87
88
  };
88
89
  if ((0, build_file_task_schema_1.isBuildFileContainerTaskSchema)(task.schema)) {
@@ -100,7 +101,9 @@ function parseTask(cwd, task, environment, context) {
100
101
  if (fileSecret) {
101
102
  throw new Error(`task ${task.relativeName}: a local task takes secrets as env only (${(0, work_secret_1.getSecretName)(fileSecret)})`);
102
103
  }
103
- if (baseWorkNode.network.type !== 'open') {
104
+ // on a server's cluster it runs in a pod of a pool's tool image (runInToolImage)
105
+ const contained = !!environment.hosted?.kubernetes || !!environment.hosted?.localTasksInContainers;
106
+ if (baseWorkNode.network.type !== 'open' && !contained) {
104
107
  throw new Error(`task ${task.relativeName}: network ${(0, work_network_1.formatNetwork)(baseWorkNode.network)} needs a container, ` +
105
108
  `hammerkit can't limit the network of a local task`);
106
109
  }
@@ -101,6 +101,9 @@ function applyArguments(selection, args, keepCache = false) {
101
101
  if (item.type === 'kubernetes-service') {
102
102
  throw new Error(`service ${item.name} is a kubernetes service and has no cmd for the arguments after --`);
103
103
  }
104
+ if (item.args === false) {
105
+ throw new Error(`${item.type === 'container-service' ? 'service' : 'task'} ${item.name} takes no arguments after -- (args: false)`);
106
+ }
104
107
  const target = getArgumentsTarget(item);
105
108
  if (!target) {
106
109
  throw new Error(item.type === 'container-service'
@@ -12,6 +12,7 @@ const event_cache_1 = require("../executer/event-cache");
12
12
  const get_docker_executor_1 = require("../executer/get-docker-executor");
13
13
  const path_1 = require("path");
14
14
  const tar_1 = require("tar");
15
+ const tar_2 = require("../utils/tar");
15
16
  const plan_work_volume_1 = require("./utils/plan-work-volume");
16
17
  const work_instance_id_1 = require("./work-instance-id");
17
18
  const stream_1 = require("stream");
@@ -302,7 +303,7 @@ async function archiveContainer(docker, environment, item, path) {
302
303
  for (const generatedArchive of (0, event_cache_1.getArchivePaths)(item.data, path)) {
303
304
  // a file output is a bind mount of a host file, so archive it from the host
304
305
  const readable = hostFiles.has(generatedArchive.path)
305
- ? stream_1.Readable.from((0, tar_1.create)({ cwd: (0, path_1.dirname)(generatedArchive.path) }, [(0, path_1.basename)(generatedArchive.path)]))
306
+ ? stream_1.Readable.from((0, tar_2.packArchive)({ cwd: (0, path_1.dirname)(generatedArchive.path) }, [(0, path_1.basename)(generatedArchive.path)]))
306
307
  : await container.getArchive({
307
308
  path: generatedArchive.path,
308
309
  });
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.kubernetesForwardRuntime = exports.kubernetesServiceRuntime = exports.kubernetesTaskRuntime = exports.describeFailedJob = void 0;
6
+ exports.kubernetesForwardRuntime = exports.kubernetesServiceRuntime = exports.kubernetesTaskRuntime = exports.getTaskJobContainers = exports.describeFailedJob = void 0;
7
7
  const abort_1 = require("../executer/abort");
8
8
  const kubernetes_service_1 = require("../executer/kubernetes-service");
9
9
  const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
@@ -38,7 +38,7 @@ const work_secret_1 = require("./work-secret");
38
38
  async function listJobNames(instance, kubernetes, task) {
39
39
  const jobs = await instance.batchApi.listNamespacedJob({
40
40
  namespace: kubernetes.namespace,
41
- labelSelector: `hammerkit.dev/id=${task.id()}`,
41
+ labelSelector: (0, resources_1.getItemSelector)(task, kubernetes),
42
42
  });
43
43
  return jobs.items.flatMap((job) => (job.metadata?.name ? [job.metadata.name] : []));
44
44
  }
@@ -125,6 +125,41 @@ async function describeFailedJob(instance, kubernetes, task, job, container, sec
125
125
  return undefined;
126
126
  }
127
127
  exports.describeFailedJob = describeFailedJob;
128
+ /**
129
+ * The containers of the Job of a command: the network gate first, nothing runs before the
130
+ * item's policy holds, then the init containers that read its secrets, then the command. The
131
+ * proxy's token (`network.secretEnv`) reaches the init containers through `secretSpec` only:
132
+ * the command's container gets the proxy variables of a limited network without it.
133
+ */
134
+ function getTaskJobContainers(options) {
135
+ const { task, cmd, envs, secrets, network, secretSpec, kubernetes } = options;
136
+ // the arguments after `--` keep their `$` as text, Kubernetes expands `$(NAME)`
137
+ const command = secretSpec.command(task.data.shell, (0, task_arguments_1.getKubernetesCommandText)(cmd));
138
+ const initContainers = [...network.initContainers, ...secretSpec.initContainers];
139
+ return {
140
+ ...(initContainers.length > 0 ? { initContainers } : {}),
141
+ containers: [
142
+ {
143
+ image: task.data.image,
144
+ // through the task's shell, as on Docker
145
+ command: command.command,
146
+ args: command.args,
147
+ workingDir: cmd.cwd,
148
+ env: [
149
+ ...Object.entries(envs).map(([key, value]) => ({ name: key, value })),
150
+ ...secrets.env,
151
+ ...network.env,
152
+ ...command.env,
153
+ ],
154
+ name: options.container,
155
+ volumeMounts: [...options.mounts, ...secrets.mounts, ...secretSpec.mounts],
156
+ ...(0, container_resources_1.getKubernetesResources)(task.data.resources),
157
+ ...(0, job_placement_1.getContainerSecurity)(kubernetes),
158
+ },
159
+ ],
160
+ };
161
+ }
162
+ exports.getTaskJobContainers = getTaskJobContainers;
128
163
  function kubernetesTaskRuntime(task, kubernetes) {
129
164
  const instance = (0, kubernetes_instance_1.createKubernetesInstances)(kubernetes);
130
165
  // on a server that keeps them, the task's claim stays after the run with the state of its
@@ -166,10 +201,11 @@ function kubernetesTaskRuntime(task, kubernetes) {
166
201
  }
167
202
  const server = kubernetes.server;
168
203
  const { delivered, inJob } = (0, job_secrets_1.splitJobSecrets)(task.data.secrets, server);
169
- const secrets = (0, secrets_1.getKubernetesSecretRefs)(task, delivered);
204
+ const secrets = (0, secrets_1.getKubernetesSecretRefs)(task, kubernetes, delivered);
170
205
  const admitted = environment.hosted?.accounts;
206
+ let secretHosts;
171
207
  try {
172
- (0, job_secrets_1.assertSecretsReachable)(task, inJob);
208
+ secretHosts = (0, job_secrets_1.getSecretHosts)(task, inJob, server);
173
209
  if (server) {
174
210
  (0, job_secrets_1.assertJobAccounts)(inJob, server, admitted);
175
211
  }
@@ -194,7 +230,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
194
230
  : {}),
195
231
  };
196
232
  const labels = {
197
- 'hammerkit.dev/id': task.id(),
233
+ ...(0, resources_1.getItemLabels)(task, kubernetes),
198
234
  'hammerkit.dev/state': options.stateKey,
199
235
  ...workspace.labels,
200
236
  [network_policy_1.RUN_LABEL]: scope.run,
@@ -210,7 +246,11 @@ function kubernetesTaskRuntime(task, kubernetes) {
210
246
  // NetworkPolicy and proxy of a limited network, both of this run
211
247
  let network;
212
248
  try {
213
- network = await (0, network_policy_1.ensureKubernetesItemNetwork)(instance, kubernetes, task, { followLog: true, ...scope });
249
+ network = await (0, network_policy_1.ensureKubernetesItemNetwork)(instance, kubernetes, task, {
250
+ followLog: true,
251
+ secretHosts,
252
+ ...scope,
253
+ });
214
254
  }
215
255
  catch (e) {
216
256
  await removeTaskSecrets(instance, kubernetes, task).catch(() => undefined);
@@ -218,7 +258,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
218
258
  return;
219
259
  }
220
260
  try {
221
- const podName = (0, resources_1.getResourceName)(task, '-job');
261
+ const podName = (0, resources_1.getResourceName)(task, kubernetes, '-job');
222
262
  let i = 0;
223
263
  for (const cmd of task.data.cmds) {
224
264
  const container = `cmd-${++i}`;
@@ -226,15 +266,13 @@ function kubernetesTaskRuntime(task, kubernetes) {
226
266
  try {
227
267
  // made for each Job right before it, so a later command's token is as fresh
228
268
  const credentials = await (0, job_secrets_1.ensureJobCredentials)(instance, kubernetes, task, inJob, admitted);
229
- secretSpec = (0, job_secrets_1.getJobSecretSpec)(task, inJob, server, credentials, environment);
269
+ secretSpec = (0, job_secrets_1.getJobSecretSpec)(task, inJob, server, credentials, environment, network.secretEnv);
230
270
  }
231
271
  catch (e) {
232
272
  await removeTaskSecrets(instance, kubernetes, task).catch(() => undefined);
233
273
  options.state.set({ stateKey: options.stateKey, type: 'error', errorMessage: (0, log_1.getErrorMessage)(e) });
234
274
  return;
235
275
  }
236
- // the arguments after `--` keep their `$` as text, Kubernetes expands `$(NAME)`
237
- const command = secretSpec.command(task.data.shell, (0, task_arguments_1.getKubernetesCommandText)(cmd));
238
276
  const spec = {
239
277
  kind: 'Job',
240
278
  apiVersion: 'batch/v1',
@@ -255,33 +293,17 @@ function kubernetesTaskRuntime(task, kubernetes) {
255
293
  labels,
256
294
  },
257
295
  spec: {
258
- // the network gate first: nothing runs before the item's policy holds
259
- ...(network.initContainers.length + secretSpec.initContainers.length > 0
260
- ? { initContainers: [...network.initContainers, ...secretSpec.initContainers] }
261
- : {}),
262
- containers: [
263
- {
264
- image: task.data.image,
265
- // through the task's shell, as on Docker
266
- command: command.command,
267
- args: command.args,
268
- workingDir: cmd.cwd,
269
- env: [
270
- ...Object.entries(envs).map(([key, value]) => ({ name: key, value })),
271
- ...secrets.env,
272
- ...network.env,
273
- ...command.env,
274
- ],
275
- name: container,
276
- volumeMounts: [
277
- ...persistence.mounts.map((m) => m.mount),
278
- ...secrets.mounts,
279
- ...secretSpec.mounts,
280
- ],
281
- ...(0, container_resources_1.getKubernetesResources)(task.data.resources),
282
- ...(0, job_placement_1.getContainerSecurity)(kubernetes),
283
- },
284
- ],
296
+ ...getTaskJobContainers({
297
+ task,
298
+ cmd,
299
+ container,
300
+ envs,
301
+ secrets,
302
+ network,
303
+ secretSpec,
304
+ mounts: persistence.mounts.map((m) => m.mount),
305
+ kubernetes,
306
+ }),
285
307
  volumes: [
286
308
  ...persistence.volumes,
287
309
  ...persistence.workspace,
@@ -365,7 +387,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
365
387
  async stop() {
366
388
  const jobs = await instance.batchApi.listNamespacedJob({
367
389
  namespace: kubernetes.namespace,
368
- labelSelector: `hammerkit.dev/id=${task.id()}`,
390
+ labelSelector: (0, resources_1.getItemSelector)(task, kubernetes),
369
391
  });
370
392
  for (const job of jobs.items) {
371
393
  if (job.metadata?.name) {
@@ -374,7 +396,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
374
396
  }
375
397
  const deployments = await instance.appsApi.listNamespacedDeployment({
376
398
  namespace: kubernetes.namespace,
377
- labelSelector: `hammerkit.dev/id=${task.id()}`,
399
+ labelSelector: (0, resources_1.getItemSelector)(task, kubernetes),
378
400
  });
379
401
  for (const deploy of deployments.items) {
380
402
  if (deploy.metadata?.name) {
@@ -386,7 +408,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
386
408
  }
387
409
  const pods = await instance.coreApi.listNamespacedPod({
388
410
  namespace: kubernetes.namespace,
389
- labelSelector: `hammerkit.dev/id=${task.id()}`,
411
+ labelSelector: (0, resources_1.getItemSelector)(task, kubernetes),
390
412
  });
391
413
  for (const pod of pods.items) {
392
414
  if (pod.metadata?.name) {
@@ -408,7 +430,7 @@ function kubernetesTaskRuntime(task, kubernetes) {
408
430
  }
409
431
  const jobs = await instance.batchApi.listNamespacedJob({
410
432
  namespace: kubernetes.namespace,
411
- labelSelector: `hammerkit.dev/id=${task.id()}`,
433
+ labelSelector: (0, resources_1.getItemSelector)(task, kubernetes),
412
434
  });
413
435
  const completedStates = jobs.items
414
436
  .filter((j) => j.status?.succeeded && j.metadata?.labels?.['hammerkit.dev/state'])
@@ -445,7 +467,7 @@ function kubernetesServiceRuntime(service, kubernetes) {
445
467
  async initialize(state) {
446
468
  const deployments = await instance.appsApi.listNamespacedDeployment({
447
469
  namespace: kubernetes.namespace,
448
- labelSelector: `hammerkit.dev/id=${service.id()}`,
470
+ labelSelector: (0, resources_1.getItemSelector)(service, kubernetes),
449
471
  });
450
472
  const deployment = deployments.items[0];
451
473
  if (!deployment?.metadata?.name) {
@@ -472,7 +494,7 @@ function kubernetesServiceRuntime(service, kubernetes) {
472
494
  async stop() {
473
495
  const deployments = await instance.appsApi.listNamespacedDeployment({
474
496
  namespace: kubernetes.namespace,
475
- labelSelector: `hammerkit.dev/id=${service.id()}`,
497
+ labelSelector: (0, resources_1.getItemSelector)(service, kubernetes),
476
498
  });
477
499
  for (const deployment of deployments.items) {
478
500
  if (deployment.metadata?.name) {
@@ -492,9 +514,13 @@ function kubernetesServiceRuntime(service, kubernetes) {
492
514
  await (0, ensure_kubernetes_service_exists_1.ensureKubernetesServiceExists)(instance, kubernetes, service);
493
515
  await (0, ensure_persistent_data_1.ensurePersistentData)(instance, kubernetes, environment, service, persistence);
494
516
  await (0, secrets_1.ensureKubernetesSecret)(instance, kubernetes, service, service.data.secrets, environment);
495
- const network = await (0, network_policy_1.ensureKubernetesItemNetwork)(instance, kubernetes, service, { followLog: false, run: null });
496
- await (0, ensure_kubernetes_deployment_exists_1.ensureKubernetesDeploymentExists)(instance, kubernetes, service, persistence, (0, secrets_1.getKubernetesSecretRefs)(service, service.data.secrets), options.stateKey, network);
497
- const name = (0, resources_1.getResourceName)(service);
517
+ // on a server, the run's: its runs share the namespace, and the service goes with the run
518
+ const network = await (0, network_policy_1.ensureKubernetesItemNetwork)(instance, kubernetes, service, {
519
+ followLog: false,
520
+ run: kubernetes.server ? (0, job_placement_1.getRunLabelValue)(kubernetes.server.run) : null,
521
+ });
522
+ await (0, ensure_kubernetes_deployment_exists_1.ensureKubernetesDeploymentExists)(instance, kubernetes, service, persistence, (0, secrets_1.getKubernetesSecretRefs)(service, kubernetes, service.data.secrets), options.stateKey, network);
523
+ const name = (0, resources_1.getResourceName)(service, kubernetes);
498
524
  options.state.set({
499
525
  type: 'running',
500
526
  dns: { containerId: name },
@@ -526,7 +552,7 @@ function kubernetesServiceRuntime(service, kubernetes) {
526
552
  }
527
553
  const services = await instance.coreApi.listNamespacedService({
528
554
  namespace: kubernetes.namespace,
529
- labelSelector: `hammerkit.dev/id=${service.id()}`,
555
+ labelSelector: (0, resources_1.getItemSelector)(service, kubernetes),
530
556
  });
531
557
  for (const service of services.items) {
532
558
  if (service.metadata?.name) {
@@ -542,7 +568,7 @@ function kubernetesServiceRuntime(service, kubernetes) {
542
568
  async currentStateKey() {
543
569
  const deployments = await instance.appsApi.listNamespacedDeployment({
544
570
  namespace: kubernetes.namespace,
545
- labelSelector: `hammerkit.dev/id=${service.id()}`,
571
+ labelSelector: (0, resources_1.getItemSelector)(service, kubernetes),
546
572
  });
547
573
  const deployment = deployments.items[0];
548
574
  return deployment?.metadata?.labels?.['hammerkit.dev/state'] ?? null;
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getLocalWorkRuntime = exports.getStateFilename = void 0;
7
7
  const tar_1 = require("tar");
8
+ const tar_2 = require("../utils/tar");
8
9
  const path_1 = require("path");
9
10
  const local_task_1 = require("../executer/local-task");
10
11
  const event_cache_1 = require("../executer/event-cache");
@@ -121,7 +122,7 @@ async function archiveLocal(environment, task, path) {
121
122
  for (const generatedArchive of (0, event_cache_1.getArchivePaths)(task.data, path)) {
122
123
  await environment.file.writeStream(generatedArchive.filename,
123
124
  // tar's stream is a Minipass stream; Readable.from adapts it to a Node one
124
- stream_1.Readable.from((0, tar_1.create)({ cwd: task.data.cwd, gzip: true }, [(0, path_1.relative)(task.data.cwd, generatedArchive.path)])));
125
+ stream_1.Readable.from((0, tar_2.packArchive)({ cwd: task.data.cwd, gzip: true }, [(0, path_1.relative)(task.data.cwd, generatedArchive.path)])));
125
126
  }
126
127
  }
127
128
  async function restoreLocal(environment, task, path) {
package/dist/program.js CHANGED
@@ -84,7 +84,8 @@ function printTaskDefinition(environment, definition) {
84
84
  }
85
85
  }
86
86
  // `ls --json`: what a caller (an agent picking a task) needs to know, with the
87
- // command that arguments after `--` are appended to (null: it takes none).
87
+ // command that arguments after `--` are appended to (null: it takes none) and
88
+ // `args` as the build file says it (false: it refuses them).
88
89
  function getListJson(tasks, services) {
89
90
  return {
90
91
  tasks: tasks.map(({ item }) => ({
@@ -96,6 +97,7 @@ function getListJson(tasks, services) {
96
97
  needs: item.needs.map((n) => n.name),
97
98
  labels: item.data.labels,
98
99
  command: (0, task_arguments_1.getArgumentsTarget)(item.data)?.cmd ?? null,
100
+ args: item.data.args,
99
101
  })),
100
102
  services: services.map(({ item }) => ({
101
103
  name: item.name,
@@ -105,6 +107,7 @@ function getListJson(tasks, services) {
105
107
  ports: item.data.ports.map((p) => ({ host: p.hostPort, container: p.containerPort })),
106
108
  labels: item.data.labels,
107
109
  command: (0, task_arguments_1.getArgumentsTarget)(item.data)?.cmd ?? null,
110
+ args: item.data.type === 'container-service' ? item.data.args : null,
108
111
  })),
109
112
  };
110
113
  }
@@ -159,11 +162,14 @@ async function getProgram(environment, argv) {
159
162
  if (fileIndex >= 0) {
160
163
  args.splice(fileIndex, 2);
161
164
  }
162
- (0, remote_commands_1.registerRemoteCommands)(program, environment, fileName);
165
+ (0, remote_commands_1.registerRemoteCommands)(program, environment, fileName, passthrough);
163
166
  program.hook('preAction', (_, actionCommand) => {
164
- const takesArguments = actionCommand.parent === program && ['run', 'up'].includes(actionCommand.name());
167
+ const takesArguments = (actionCommand.parent === program && ['run', 'up'].includes(actionCommand.name())) ||
168
+ (actionCommand.parent?.name() === 'remote' && actionCommand.name() === 'run');
165
169
  if (passthrough.length > 0 && !takesArguments) {
166
- program.error(`${actionCommand.name()} takes no arguments after --, only run and up do`, { exitCode: 1 });
170
+ program.error(`${actionCommand.name()} takes no arguments after --, only run, up and remote run do`, {
171
+ exitCode: 1,
172
+ });
167
173
  }
168
174
  });
169
175
  if (await environment.file.exists(fileName)) {
@@ -780,7 +786,8 @@ tasks:
780
786
  program.option('--file', 'set build file', '.hammerkit.yaml');
781
787
  program.option('--set <NAME=value>', 'value for $NAME / ${NAME} references, ahead of the shell and .env (repeatable)');
782
788
  program.configureOutput({
783
- writeOut: (str) => environment.console.info(str),
789
+ // --version and --help as they are, so `VERSION=$(hammerkit --version)` works
790
+ writeOut: (str) => environment.stdout.write(str),
784
791
  writeErr: (str) => {
785
792
  environment.console.error(str);
786
793
  },
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.applyAnnotations = exports.collectAnnotations = exports.remoteRepo = void 0;
3
+ exports.applyAnnotations = exports.collectAnnotations = exports.readCheckout = exports.remoteRepo = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  const os_1 = require("os");
6
6
  const path_1 = require("path");
@@ -36,6 +36,18 @@ function remoteRepo(url) {
36
36
  }
37
37
  }
38
38
  exports.remoteRepo = remoteRepo;
39
+ // The checkout a directory is in: the repository of its `origin`, the branch it is on and
40
+ // its commit, what can be found of them.
41
+ async function readCheckout(cwd) {
42
+ if ((await git(cwd, 'rev-parse', '--is-inside-work-tree')) !== 'true') {
43
+ return {};
44
+ }
45
+ const remote = await git(cwd, 'remote', 'get-url', 'origin');
46
+ // a detached HEAD is on no branch
47
+ const branch = await git(cwd, 'symbolic-ref', '--quiet', '--short', 'HEAD');
48
+ return { repo: remote ? remoteRepo(remote) : undefined, branch, commit: await git(cwd, 'rev-parse', 'HEAD') };
49
+ }
50
+ exports.readCheckout = readCheckout;
39
51
  // Where a build file is published from: its path in its repository (or on this
40
52
  // machine, outside of one), the repository and commit, this machine and its user.
41
53
  // `files` are the paths of the bundle, relative to the build file's directory: when
@@ -16,7 +16,9 @@ const remote_work_commands_1 = require("./remote-work-commands");
16
16
  const mcp_command_1 = require("../harness/mcp-command");
17
17
  // Host-level commands: they only touch the machine's config file, so they work
18
18
  // with or without a build file in the working directory.
19
- function registerRemoteCommands(program, environment, buildFile) {
19
+ function registerRemoteCommands(program, environment, buildFile,
20
+ // what follows `--`, the arguments of `remote run`
21
+ passthrough = []) {
20
22
  const remote = program.command('remote').description('register the servers runs can be sent to');
21
23
  const guarded = (failure, fn) => {
22
24
  return async (...args) => {
@@ -154,7 +156,14 @@ function registerRemoteCommands(program, environment, buildFile) {
154
156
  environment.stdout.write(`repositories: ${me.repos.map(server_client_1.printable).join(', ')}\n`);
155
157
  }
156
158
  }));
157
- (0, remote_work_commands_1.registerWorkCommands)(remote, { environment, buildFile, getServer, guarded });
159
+ (0, remote_work_commands_1.registerWorkCommands)(remote, {
160
+ environment,
161
+ buildFile,
162
+ getServer,
163
+ guarded,
164
+ passthrough,
165
+ fail: (message, exitCode) => program.error(message, { exitCode }),
166
+ });
158
167
  (0, mcp_command_1.registerMcpCommand)(remote, environment);
159
168
  }
160
169
  exports.registerRemoteCommands = registerRemoteCommands;