@leitwerk-dev/worker-runners 0.1.9 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-4F7EMXMU.js +119 -0
- package/dist/chunk-4F7EMXMU.js.map +1 -0
- package/dist/{chunk-HLTGLGUS.js → chunk-63BBCX7B.js} +69 -93
- package/dist/chunk-63BBCX7B.js.map +1 -0
- package/dist/{chunk-6WACBFXN.js → chunk-6BSZBMHH.js} +18 -1
- package/dist/chunk-6BSZBMHH.js.map +1 -0
- package/dist/{chunk-DL3Y3R6E.js → chunk-7NF2P2VP.js} +74 -8
- package/dist/chunk-7NF2P2VP.js.map +1 -0
- package/dist/chunk-7X5FBNT7.js +14 -0
- package/dist/chunk-7X5FBNT7.js.map +1 -0
- package/dist/chunk-D56NJQ5X.js +90 -0
- package/dist/chunk-D56NJQ5X.js.map +1 -0
- package/dist/chunk-ISEDLYKQ.js +881 -0
- package/dist/chunk-ISEDLYKQ.js.map +1 -0
- package/dist/{chunk-IRFVGUXJ.js → chunk-OSS62AOL.js} +7 -3
- package/dist/{chunk-IRFVGUXJ.js.map → chunk-OSS62AOL.js.map} +1 -1
- package/dist/chunk-UGKQOL5E.js +39 -0
- package/dist/chunk-UGKQOL5E.js.map +1 -0
- package/dist/{chunk-2TSWMVLZ.js → chunk-YBSX4N2K.js} +156 -37
- package/dist/chunk-YBSX4N2K.js.map +1 -0
- package/dist/chunk-YJMHXKNO.js +308 -0
- package/dist/chunk-YJMHXKNO.js.map +1 -0
- package/dist/{docker-engine-client-Cpmy2yma.d.ts → docker-engine-client-CnXspTGq.d.ts} +8 -21
- package/dist/docker-engine-client.d.ts +7 -20
- package/dist/docker-engine-client.d.ts.map +1 -1
- package/dist/docker-engine-http-client.d.ts +0 -8
- package/dist/docker-engine-http-client.d.ts.map +1 -1
- package/dist/docker-engine-http-client.js +12 -201
- package/dist/docker-engine-http-client.js.map +1 -1
- package/dist/docker-worker-runner.d.ts +11 -4
- package/dist/docker-worker-runner.d.ts.map +1 -1
- package/dist/docker-worker-runner.js +12 -235
- package/dist/docker-worker-runner.js.map +1 -1
- package/dist/fake-docker-engine-client.d.ts.map +1 -1
- package/dist/filesystem-process-state-exporter.d.ts +10 -0
- package/dist/filesystem-process-state-exporter.d.ts.map +1 -0
- package/dist/helper-process-state-exporter.d.ts +28 -0
- package/dist/helper-process-state-exporter.d.ts.map +1 -0
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +305 -9
- package/dist/index.js.map +1 -1
- package/dist/{kubernetes-api-client-BcwF2uon.d.ts → kubernetes-api-client-DD2YevYP.d.ts} +128 -85
- package/dist/kubernetes-api-client.d.ts +33 -6
- package/dist/kubernetes-api-client.d.ts.map +1 -1
- package/dist/kubernetes-http-client.d.ts.map +1 -1
- package/dist/kubernetes-http-client.js +496 -286
- package/dist/kubernetes-http-client.js.map +1 -1
- package/dist/kubernetes-manifests.d.ts +40 -77
- package/dist/kubernetes-manifests.d.ts.map +1 -1
- package/dist/kubernetes-startup-sampler.d.ts +14 -0
- package/dist/kubernetes-startup-sampler.d.ts.map +1 -0
- package/dist/kubernetes-volume-pool-api.d.ts +69 -0
- package/dist/kubernetes-volume-pool-api.d.ts.map +1 -0
- package/dist/kubernetes-volume-pool.d.ts +24 -0
- package/dist/kubernetes-volume-pool.d.ts.map +1 -0
- package/dist/kubernetes-worker-runner.d.ts +26 -2
- package/dist/kubernetes-worker-runner.d.ts.map +1 -1
- package/dist/kubernetes-worker-runner.js +12 -161
- package/dist/kubernetes-worker-runner.js.map +1 -1
- package/dist/local-worker-runner.d.ts +11 -1
- package/dist/local-worker-runner.d.ts.map +1 -1
- package/dist/local-worker-runner.js +14 -108
- package/dist/local-worker-runner.js.map +1 -1
- package/dist/runner-utils.d.ts +3 -1
- package/dist/runner-utils.d.ts.map +1 -1
- package/dist/session-transfer-helper-cli.d.ts +2 -0
- package/dist/session-transfer-helper-cli.d.ts.map +1 -0
- package/dist/session-transfer-helper-cli.js +9 -0
- package/dist/session-transfer-helper-cli.js.map +1 -0
- package/dist/session-transfer-helper.d.ts +4 -0
- package/dist/session-transfer-helper.d.ts.map +1 -0
- package/dist/session-transfer-helper.js +11 -0
- package/dist/session-transfer-helper.js.map +1 -0
- package/dist/types.d.ts +60 -17
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -11
- package/dist/types.js.map +1 -1
- package/dist/worker-labels.d.ts +7 -0
- package/dist/worker-labels.d.ts.map +1 -1
- package/package.json +13 -5
- package/src/docker-engine-client.ts +7 -20
- package/src/docker-engine-http-client.ts +77 -96
- package/src/docker-worker-runner.ts +180 -54
- package/src/fake-docker-engine-client.ts +13 -41
- package/src/filesystem-process-state-exporter.ts +42 -0
- package/src/helper-process-state-exporter.ts +149 -0
- package/src/index.ts +14 -5
- package/src/kubernetes-api-client.ts +70 -10
- package/src/kubernetes-http-client.ts +228 -92
- package/src/kubernetes-manifests.ts +265 -336
- package/src/kubernetes-startup-sampler.ts +191 -0
- package/src/kubernetes-volume-pool-api.ts +187 -0
- package/src/kubernetes-volume-pool.ts +357 -0
- package/src/kubernetes-worker-runner.ts +316 -39
- package/src/local-worker-runner.ts +82 -6
- package/src/runner-utils.ts +10 -2
- package/src/session-transfer-helper-cli.ts +5 -0
- package/src/session-transfer-helper.ts +105 -0
- package/src/types.ts +87 -14
- package/src/worker-labels.ts +18 -0
- package/dist/chunk-2TSWMVLZ.js.map +0 -1
- package/dist/chunk-6WACBFXN.js.map +0 -1
- package/dist/chunk-DL3Y3R6E.js.map +0 -1
- package/dist/chunk-DUFA24LP.js +0 -401
- package/dist/chunk-DUFA24LP.js.map +0 -1
- package/dist/chunk-HLTGLGUS.js.map +0 -1
- package/dist/chunk-P4DIFRBH.js +0 -187
- package/dist/chunk-P4DIFRBH.js.map +0 -1
- package/dist/docker-engine-client.js +0 -10
- package/dist/docker-engine-client.js.map +0 -1
- package/dist/fake-docker-engine-client.js +0 -131
- package/dist/fake-docker-engine-client.js.map +0 -1
- package/dist/kubernetes-api-client.js +0 -142
- package/dist/kubernetes-api-client.js.map +0 -1
- package/dist/kubernetes-manifests.js +0 -379
- package/dist/kubernetes-manifests.js.map +0 -1
- package/dist/process-volume-retention.js +0 -46
- package/dist/process-volume-retention.js.map +0 -1
- package/dist/runner-utils.js +0 -36
- package/dist/runner-utils.js.map +0 -1
- package/dist/worker-labels.js +0 -83
- package/dist/worker-labels.js.map +0 -1
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
|
+
import {
|
|
3
|
+
createHelperProcessStateExporter,
|
|
4
|
+
exportHelperEnvironment,
|
|
5
|
+
} from "./helper-process-state-exporter.js";
|
|
2
6
|
import type { KubernetesApiClient } from "./kubernetes-api-client.js";
|
|
3
7
|
import {
|
|
4
8
|
buildKubernetesDockerConfigJsonSecretManifest,
|
|
9
|
+
buildKubernetesExportHelperPodManifest,
|
|
5
10
|
buildKubernetesProcessNamespaceManifest,
|
|
6
11
|
buildKubernetesProcessPvcManifest,
|
|
7
12
|
buildKubernetesServerCaConfigMapManifest,
|
|
@@ -9,15 +14,26 @@ import {
|
|
|
9
14
|
KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
10
15
|
KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
11
16
|
KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH,
|
|
17
|
+
type KubernetesDockerPodSpecOptions,
|
|
12
18
|
type KubernetesPodSpecOptions,
|
|
13
19
|
type KubernetesProcessVolumeSpec,
|
|
20
|
+
kubernetesExportHelperPodName,
|
|
14
21
|
kubernetesProcessNamespaceName,
|
|
15
22
|
kubernetesProcessPvcName,
|
|
23
|
+
redactKubernetesDiagnostic,
|
|
16
24
|
volumeRefFromPvc,
|
|
17
25
|
} from "./kubernetes-manifests.js";
|
|
26
|
+
import { sampleKubernetesStartup, startupReceipt } from "./kubernetes-startup-sampler.js";
|
|
27
|
+
import { createKubernetesVolumePool } from "./kubernetes-volume-pool.js";
|
|
18
28
|
import { UnitExitNotifier } from "./runner-utils.js";
|
|
29
|
+
import {
|
|
30
|
+
SESSION_TRANSFER_HELPER_ENTRY_PATH,
|
|
31
|
+
SESSION_TRANSFER_HELPER_MOUNT_PATH,
|
|
32
|
+
} from "./session-transfer-helper.js";
|
|
19
33
|
import type {
|
|
20
34
|
IsolatedStartWorkerInput,
|
|
35
|
+
ProcessStateExporter,
|
|
36
|
+
ProcessStateExportHelperRelayProvider,
|
|
21
37
|
ProcessVolume,
|
|
22
38
|
StopWorkerOptions,
|
|
23
39
|
WorkerRunner,
|
|
@@ -25,7 +41,9 @@ import type {
|
|
|
25
41
|
WorkerUnitDescriptor,
|
|
26
42
|
WorkerUnitRef,
|
|
27
43
|
} from "./types.js";
|
|
44
|
+
import { WorkerStartDiagnosticError } from "./types.js";
|
|
28
45
|
import {
|
|
46
|
+
managedExportHelperLabelSelector,
|
|
29
47
|
managedProcessNamespaceLabelSelector,
|
|
30
48
|
managedWorkerLabelSelector,
|
|
31
49
|
parseWorkerUnitIdentity,
|
|
@@ -35,11 +53,32 @@ import {
|
|
|
35
53
|
WORKER_LABEL_MANAGED_BY_VALUE,
|
|
36
54
|
} from "./worker-labels.js";
|
|
37
55
|
|
|
56
|
+
const KUBERNETES_START_DIAGNOSTIC_MAX_LENGTH = 2_048;
|
|
57
|
+
|
|
58
|
+
function boundedKubernetesStartDiagnostic(
|
|
59
|
+
error: unknown,
|
|
60
|
+
sensitiveValues: readonly string[],
|
|
61
|
+
): string {
|
|
62
|
+
const detail = error instanceof Error ? error.message : "Kubernetes Pod admission failed";
|
|
63
|
+
return redactKubernetesDiagnostic(detail, sensitiveValues).slice(
|
|
64
|
+
0,
|
|
65
|
+
KUBERNETES_START_DIAGNOSTIC_MAX_LENGTH,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
38
69
|
export interface KubernetesWorkerRunnerOptions {
|
|
70
|
+
preProvision?: { count: number; onError?: () => void };
|
|
39
71
|
client: KubernetesApiClient;
|
|
40
72
|
/** Prefix used to derive one Kubernetes namespace per process instance. */
|
|
41
73
|
processNamespacePrefix: string;
|
|
42
74
|
volume: KubernetesProcessVolumeSpec;
|
|
75
|
+
/** Storage and Pod wiring for process definitions that require private Docker. */
|
|
76
|
+
docker?: KubernetesDockerPodSpecOptions & { processStorageClassName: string };
|
|
77
|
+
/** Maximum wait after deletion before replacement is rejected. */
|
|
78
|
+
podDisappearanceTimeoutMs?: number;
|
|
79
|
+
podDisappearancePollIntervalMs?: number;
|
|
80
|
+
/** Test seam for bounded disappearance polling. */
|
|
81
|
+
delay?: (ms: number) => Promise<void>;
|
|
43
82
|
/** Server-local CA bundle copied into each process namespace for worker TLS trust. */
|
|
44
83
|
serverCaFile?: string;
|
|
45
84
|
/** Namespace containing operator-managed source image-pull Secrets. */
|
|
@@ -47,25 +86,46 @@ export interface KubernetesWorkerRunnerOptions {
|
|
|
47
86
|
/** Docker registry Secrets copied into every process namespace. */
|
|
48
87
|
imagePullSecretCopies?: Array<{ sourceName: string; targetName: string }>;
|
|
49
88
|
pod?: Omit<KubernetesPodSpecOptions, "namespace">;
|
|
89
|
+
/** Stable internal URL used by PVC export helpers. */
|
|
90
|
+
serverUrl?: string;
|
|
91
|
+
/** Trusted image containing the bundled session-transfer helper entrypoint. */
|
|
92
|
+
exporterImage?: string;
|
|
93
|
+
/** Pull policy for the trusted helper image. */
|
|
94
|
+
exporterImagePullPolicy?: string;
|
|
95
|
+
/** Server-owned relay registry used by PVC export helpers. */
|
|
96
|
+
helperRelays?: ProcessStateExportHelperRelayProvider;
|
|
50
97
|
}
|
|
51
98
|
|
|
52
99
|
export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOptions): {
|
|
53
100
|
runner: WorkerRunner<IsolatedStartWorkerInput>;
|
|
54
101
|
volume: ProcessVolume;
|
|
102
|
+
exporter: ProcessStateExporter;
|
|
103
|
+
volumePool?: ReturnType<typeof createKubernetesVolumePool>;
|
|
55
104
|
} {
|
|
105
|
+
if (!options.serverUrl || !options.exporterImage || !options.helperRelays) {
|
|
106
|
+
throw new Error("Kubernetes transfer exporter is not configured");
|
|
107
|
+
}
|
|
56
108
|
const { client } = options;
|
|
109
|
+
const exporterServerUrl = options.serverUrl;
|
|
110
|
+
const exporterImage = options.exporterImage;
|
|
111
|
+
const helperRelays = options.helperRelays;
|
|
57
112
|
const serverCaFile = options.serverCaFile?.trim();
|
|
58
113
|
const namespaceForProcess = (instanceId: string) =>
|
|
59
114
|
kubernetesProcessNamespaceName(instanceId, options.processNamespacePrefix);
|
|
115
|
+
const delay =
|
|
116
|
+
options.delay ?? ((ms: number) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
117
|
+
const podDisappearanceTimeoutMs = options.podDisappearanceTimeoutMs ?? 30_000;
|
|
118
|
+
const podDisappearancePollIntervalMs = options.podDisappearancePollIntervalMs ?? 250;
|
|
60
119
|
|
|
120
|
+
const samplers = new Map<string, ReturnType<typeof sampleKubernetesStartup>>();
|
|
61
121
|
const volume: ProcessVolume = {
|
|
62
|
-
async ensure(instanceId) {
|
|
122
|
+
async ensure(instanceId, requirements, observer) {
|
|
63
123
|
const namespaceManifest = buildKubernetesProcessNamespaceManifest({
|
|
64
124
|
instanceId,
|
|
65
125
|
processNamespacePrefix: options.processNamespacePrefix,
|
|
66
126
|
});
|
|
67
127
|
await client.ensureNamespace(namespaceManifest);
|
|
68
|
-
|
|
128
|
+
const copySecrets = (options.imagePullSecretCopies ?? []).map(async (copy) => {
|
|
69
129
|
const dockerConfigJson = await client.getDockerConfigJsonSecret(
|
|
70
130
|
copy.sourceName,
|
|
71
131
|
options.serverNamespace ?? "leitwerk-system",
|
|
@@ -78,30 +138,63 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
78
138
|
dockerConfigJson,
|
|
79
139
|
}),
|
|
80
140
|
);
|
|
81
|
-
}
|
|
82
|
-
if (serverCaFile) {
|
|
83
|
-
await client.ensureConfigMap(
|
|
84
|
-
buildKubernetesServerCaConfigMapManifest({
|
|
85
|
-
instanceId,
|
|
86
|
-
namespace: namespaceManifest.metadata.name,
|
|
87
|
-
caPem: readFileSync(serverCaFile, "utf8"),
|
|
88
|
-
}),
|
|
89
|
-
);
|
|
90
|
-
}
|
|
141
|
+
});
|
|
91
142
|
const workerServiceAccount = options.pod?.workerServiceAccount?.trim();
|
|
92
|
-
if (workerServiceAccount && workerServiceAccount !== "default") {
|
|
93
|
-
await client.ensureServiceAccount(workerServiceAccount, namespaceManifest.metadata.name, {
|
|
94
|
-
[WORKER_LABEL_MANAGED_BY]: WORKER_LABEL_MANAGED_BY_VALUE,
|
|
95
|
-
[WORKER_LABEL_COMPONENT]: "worker-service-account",
|
|
96
|
-
[WORKER_LABEL_INSTANCE_ID]: instanceId,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
143
|
const manifest = buildKubernetesProcessPvcManifest({
|
|
100
144
|
instanceId,
|
|
101
145
|
namespace: namespaceManifest.metadata.name,
|
|
102
|
-
volume:
|
|
146
|
+
volume: {
|
|
147
|
+
...options.volume,
|
|
148
|
+
size: requirements?.size ?? options.volume.size,
|
|
149
|
+
...(requirements?.docker && options.docker
|
|
150
|
+
? { storageClassName: options.docker.processStorageClassName }
|
|
151
|
+
: {}),
|
|
152
|
+
},
|
|
103
153
|
});
|
|
104
|
-
await
|
|
154
|
+
await Promise.all([
|
|
155
|
+
...copySecrets,
|
|
156
|
+
...(serverCaFile
|
|
157
|
+
? [
|
|
158
|
+
client.ensureConfigMap(
|
|
159
|
+
buildKubernetesServerCaConfigMapManifest({
|
|
160
|
+
instanceId,
|
|
161
|
+
namespace: namespaceManifest.metadata.name,
|
|
162
|
+
caPem: readFileSync(serverCaFile, "utf8"),
|
|
163
|
+
}),
|
|
164
|
+
),
|
|
165
|
+
]
|
|
166
|
+
: []),
|
|
167
|
+
...(workerServiceAccount && workerServiceAccount !== "default"
|
|
168
|
+
? [
|
|
169
|
+
client.ensureServiceAccount(workerServiceAccount, namespaceManifest.metadata.name, {
|
|
170
|
+
[WORKER_LABEL_MANAGED_BY]: WORKER_LABEL_MANAGED_BY_VALUE,
|
|
171
|
+
[WORKER_LABEL_COMPONENT]: "worker-service-account",
|
|
172
|
+
[WORKER_LABEL_INSTANCE_ID]: instanceId,
|
|
173
|
+
}),
|
|
174
|
+
]
|
|
175
|
+
: []),
|
|
176
|
+
(async () => {
|
|
177
|
+
startupReceipt(observer, "pvc_requested", {
|
|
178
|
+
storageClass: manifest.spec.storageClassName,
|
|
179
|
+
});
|
|
180
|
+
await client.ensurePersistentVolumeClaim(manifest);
|
|
181
|
+
startupReceipt(observer, "pvc_acknowledged", {
|
|
182
|
+
storageClass: manifest.spec.storageClassName,
|
|
183
|
+
});
|
|
184
|
+
if (observer?.observe) {
|
|
185
|
+
samplers.get(instanceId)?.stop();
|
|
186
|
+
samplers.set(
|
|
187
|
+
instanceId,
|
|
188
|
+
sampleKubernetesStartup(
|
|
189
|
+
client,
|
|
190
|
+
observer,
|
|
191
|
+
manifest.metadata.namespace,
|
|
192
|
+
manifest.metadata.name,
|
|
193
|
+
),
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
})(),
|
|
197
|
+
]);
|
|
105
198
|
return volumeRefFromPvc({
|
|
106
199
|
instanceId,
|
|
107
200
|
pvcName: manifest.metadata.name,
|
|
@@ -116,37 +209,105 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
116
209
|
);
|
|
117
210
|
},
|
|
118
211
|
async deleteProcessResources(instanceId) {
|
|
212
|
+
samplers.get(instanceId)?.stop();
|
|
213
|
+
samplers.delete(instanceId);
|
|
119
214
|
await client.deleteNamespace(namespaceForProcess(instanceId));
|
|
120
215
|
},
|
|
121
216
|
};
|
|
122
217
|
|
|
123
218
|
const exitNotifier = new UnitExitNotifier();
|
|
124
219
|
const unsubscribers = new Map<string, () => void>();
|
|
220
|
+
const disappearancePromises = new Map<string, Promise<void>>();
|
|
125
221
|
|
|
126
222
|
function unitKey(namespace: string, unitId: string): string {
|
|
127
223
|
return `${namespace}/${unitId}`;
|
|
128
224
|
}
|
|
129
225
|
|
|
130
|
-
function
|
|
226
|
+
function deletePodAndWait(
|
|
227
|
+
name: string,
|
|
228
|
+
namespace: string,
|
|
229
|
+
gracePeriodSeconds: number,
|
|
230
|
+
): Promise<void> {
|
|
231
|
+
const key = unitKey(namespace, name);
|
|
232
|
+
const existing = disappearancePromises.get(key);
|
|
233
|
+
if (existing) return existing;
|
|
234
|
+
const operation = Promise.resolve()
|
|
235
|
+
.then(async () => {
|
|
236
|
+
const deadline = Date.now() + podDisappearanceTimeoutMs;
|
|
237
|
+
const timeoutError = new Error(
|
|
238
|
+
`Kubernetes worker Pod ${namespace}/${name} did not disappear within ${podDisappearanceTimeoutMs}ms; replacement was not started`,
|
|
239
|
+
);
|
|
240
|
+
const controller = new AbortController();
|
|
241
|
+
const { signal } = controller;
|
|
242
|
+
let rejectTimeout!: (error: Error) => void;
|
|
243
|
+
const timedOut = new Promise<never>((_resolve, reject) => {
|
|
244
|
+
rejectTimeout = reject;
|
|
245
|
+
});
|
|
246
|
+
const timer = setTimeout(() => {
|
|
247
|
+
controller.abort(timeoutError);
|
|
248
|
+
rejectTimeout(timeoutError);
|
|
249
|
+
}, podDisappearanceTimeoutMs);
|
|
250
|
+
const waitForDisappearance = async () => {
|
|
251
|
+
await client.deletePod(name, namespace, { gracePeriodSeconds, signal });
|
|
252
|
+
while (true) {
|
|
253
|
+
signal.throwIfAborted();
|
|
254
|
+
if (Date.now() >= deadline) throw timeoutError;
|
|
255
|
+
const pod = await client.getPod(name, namespace, { signal });
|
|
256
|
+
signal.throwIfAborted();
|
|
257
|
+
if (Date.now() >= deadline) throw timeoutError;
|
|
258
|
+
if (!pod) return;
|
|
259
|
+
await delay(
|
|
260
|
+
Math.min(podDisappearancePollIntervalMs, Math.max(0, deadline - Date.now())),
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
try {
|
|
265
|
+
await Promise.race([waitForDisappearance(), timedOut]);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (signal.aborted) throw timeoutError;
|
|
268
|
+
throw error;
|
|
269
|
+
} finally {
|
|
270
|
+
clearTimeout(timer);
|
|
271
|
+
}
|
|
272
|
+
})
|
|
273
|
+
.finally(() => disappearancePromises.delete(key));
|
|
274
|
+
disappearancePromises.set(key, operation);
|
|
275
|
+
return operation;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function watchPod(
|
|
279
|
+
unitId: string,
|
|
280
|
+
namespace: string,
|
|
281
|
+
sensitiveValues: readonly string[] = [],
|
|
282
|
+
): void {
|
|
131
283
|
const key = unitKey(namespace, unitId);
|
|
132
284
|
if (unsubscribers.has(key)) return;
|
|
133
|
-
const unsubscribe = client.onPodExit(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
285
|
+
const unsubscribe = client.onPodExit(
|
|
286
|
+
unitId,
|
|
287
|
+
namespace,
|
|
288
|
+
(info) => {
|
|
289
|
+
exitNotifier.fireExit(key, info);
|
|
290
|
+
unsubscribers.get(key)?.();
|
|
291
|
+
unsubscribers.delete(key);
|
|
292
|
+
},
|
|
293
|
+
{ sensitiveValues },
|
|
294
|
+
);
|
|
138
295
|
unsubscribers.set(key, unsubscribe);
|
|
139
296
|
}
|
|
140
297
|
|
|
141
298
|
const runner: WorkerRunner<IsolatedStartWorkerInput> = {
|
|
142
|
-
async start(input: IsolatedStartWorkerInput): Promise<WorkerUnit> {
|
|
143
|
-
|
|
144
|
-
|
|
299
|
+
async start(input: IsolatedStartWorkerInput, observer): Promise<WorkerUnit> {
|
|
300
|
+
observer?.report("preparing_runtime");
|
|
301
|
+
if (input.docker && !options.docker) {
|
|
302
|
+
throw new Error(
|
|
303
|
+
"Kubernetes Docker runtime is unavailable; configure kubernetes.docker completely",
|
|
304
|
+
);
|
|
145
305
|
}
|
|
146
306
|
const processNamespace = input.volume.namespace ?? namespaceForProcess(input.instanceId);
|
|
147
307
|
const manifest = buildKubernetesWorkerPodManifest(input, {
|
|
148
308
|
namespace: processNamespace,
|
|
149
309
|
...(options.pod ?? {}),
|
|
310
|
+
...(input.docker && options.docker ? { docker: options.docker } : {}),
|
|
150
311
|
...(serverCaFile
|
|
151
312
|
? {
|
|
152
313
|
serverCaConfigMap: {
|
|
@@ -157,20 +318,47 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
157
318
|
}
|
|
158
319
|
: {}),
|
|
159
320
|
});
|
|
160
|
-
|
|
321
|
+
observer?.report("allocating_runtime");
|
|
322
|
+
try {
|
|
323
|
+
startupReceipt(observer, "pod_requested", {
|
|
324
|
+
image: input.image.reference,
|
|
325
|
+
...input.resources,
|
|
326
|
+
});
|
|
327
|
+
await client.createPod(manifest);
|
|
328
|
+
startupReceipt(observer, "pod_acknowledged");
|
|
329
|
+
samplers.get(input.instanceId)?.attachPod({
|
|
330
|
+
name: manifest.metadata.name,
|
|
331
|
+
workerId: input.workerId,
|
|
332
|
+
instanceId: input.instanceId,
|
|
333
|
+
});
|
|
334
|
+
} catch (error) {
|
|
335
|
+
samplers.get(input.instanceId)?.stop();
|
|
336
|
+
samplers.delete(input.instanceId);
|
|
337
|
+
throw new WorkerStartDiagnosticError(
|
|
338
|
+
boundedKubernetesStartDiagnostic(error, Object.values(input.env)),
|
|
339
|
+
error,
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
observer?.report("starting_runtime");
|
|
161
343
|
const ref: WorkerUnitRef = {
|
|
162
344
|
instanceId: input.instanceId,
|
|
163
345
|
workerId: input.workerId,
|
|
164
346
|
unitId: manifest.metadata.name,
|
|
165
347
|
namespace: manifest.metadata.namespace,
|
|
166
348
|
};
|
|
167
|
-
watchPod(ref.unitId, ref.namespace ?? processNamespace);
|
|
168
|
-
return exitNotifier.wrapUnit(ref, unitKey(ref.namespace ?? processNamespace, ref.unitId)
|
|
349
|
+
watchPod(ref.unitId, ref.namespace ?? processNamespace, Object.values(input.env));
|
|
350
|
+
return exitNotifier.wrapUnit(ref, unitKey(ref.namespace ?? processNamespace, ref.unitId), {
|
|
351
|
+
replacementHandoff: "stop-before-replacement",
|
|
352
|
+
});
|
|
169
353
|
},
|
|
170
354
|
async stop(ref: WorkerUnitRef, opts: StopWorkerOptions): Promise<void> {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
355
|
+
samplers.get(ref.instanceId)?.stop();
|
|
356
|
+
samplers.delete(ref.instanceId);
|
|
357
|
+
await deletePodAndWait(
|
|
358
|
+
ref.unitId,
|
|
359
|
+
ref.namespace ?? namespaceForProcess(ref.instanceId),
|
|
360
|
+
Math.max(0, Math.ceil(opts.graceMs / 1000)),
|
|
361
|
+
);
|
|
174
362
|
},
|
|
175
363
|
async list(): Promise<WorkerUnitDescriptor[]> {
|
|
176
364
|
const namespaces = await client.listNamespaces(managedProcessNamespaceLabelSelector());
|
|
@@ -199,7 +387,7 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
199
387
|
}
|
|
200
388
|
return descriptors;
|
|
201
389
|
},
|
|
202
|
-
async adopt(descriptor: WorkerUnitDescriptor): Promise<WorkerUnit> {
|
|
390
|
+
async adopt(descriptor: WorkerUnitDescriptor, observer): Promise<WorkerUnit> {
|
|
203
391
|
const podNamespace = descriptor.namespace ?? namespaceForProcess(descriptor.instanceId);
|
|
204
392
|
const pod = await client.getPod(descriptor.unitId, podNamespace);
|
|
205
393
|
const identity = pod ? parseWorkerUnitIdentity(pod.labels) : null;
|
|
@@ -219,10 +407,99 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
219
407
|
unitId: descriptor.unitId,
|
|
220
408
|
namespace: podNamespace,
|
|
221
409
|
};
|
|
410
|
+
if (observer?.observe && pod.pvcName) {
|
|
411
|
+
samplers.get(descriptor.instanceId)?.stop();
|
|
412
|
+
const sampler = sampleKubernetesStartup(client, observer, podNamespace, pod.pvcName);
|
|
413
|
+
sampler.attachPod({ name: ref.unitId, workerId: ref.workerId, instanceId: ref.instanceId });
|
|
414
|
+
samplers.set(descriptor.instanceId, sampler);
|
|
415
|
+
}
|
|
222
416
|
watchPod(ref.unitId, podNamespace);
|
|
223
|
-
return exitNotifier.wrapUnit(ref, unitKey(podNamespace, ref.unitId)
|
|
417
|
+
return exitNotifier.wrapUnit(ref, unitKey(podNamespace, ref.unitId), {
|
|
418
|
+
replacementHandoff: "stop-before-replacement",
|
|
419
|
+
});
|
|
224
420
|
},
|
|
225
421
|
};
|
|
226
422
|
|
|
227
|
-
|
|
423
|
+
const exporter = createHelperProcessStateExporter({
|
|
424
|
+
volume,
|
|
425
|
+
helperRelays,
|
|
426
|
+
async launch(input) {
|
|
427
|
+
const namespace = input.volume.namespace ?? namespaceForProcess(input.instanceId);
|
|
428
|
+
const podName = kubernetesExportHelperPodName(input.instanceId, input.exportId);
|
|
429
|
+
const manifest = buildKubernetesExportHelperPodManifest(
|
|
430
|
+
{
|
|
431
|
+
instanceId: input.instanceId,
|
|
432
|
+
exportId: input.exportId,
|
|
433
|
+
image: exporterImage,
|
|
434
|
+
command: ["node", SESSION_TRANSFER_HELPER_ENTRY_PATH],
|
|
435
|
+
env: exportHelperEnvironment({
|
|
436
|
+
serverUrl: exporterServerUrl,
|
|
437
|
+
exportId: input.exportId,
|
|
438
|
+
credential: input.credential,
|
|
439
|
+
}),
|
|
440
|
+
volume: { ...input.volume, mountPath: SESSION_TRANSFER_HELPER_MOUNT_PATH },
|
|
441
|
+
},
|
|
442
|
+
{
|
|
443
|
+
namespace,
|
|
444
|
+
...(options.pod ?? {}),
|
|
445
|
+
imagePullPolicy: options.exporterImagePullPolicy,
|
|
446
|
+
...(serverCaFile
|
|
447
|
+
? {
|
|
448
|
+
serverCaConfigMap: {
|
|
449
|
+
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
450
|
+
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
451
|
+
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH,
|
|
452
|
+
},
|
|
453
|
+
}
|
|
454
|
+
: {}),
|
|
455
|
+
},
|
|
456
|
+
);
|
|
457
|
+
await client.createPod(manifest);
|
|
458
|
+
const exit = new Promise<{ exitCode: number | null; reason?: string }>((resolve) => {
|
|
459
|
+
client.onPodExit(podName, namespace, (info) =>
|
|
460
|
+
resolve({ exitCode: info.exitCode, reason: info.reason }),
|
|
461
|
+
);
|
|
462
|
+
});
|
|
463
|
+
return {
|
|
464
|
+
wait: () => exit,
|
|
465
|
+
remove: () => client.deletePod(podName, namespace, { gracePeriodSeconds: 0 }),
|
|
466
|
+
};
|
|
467
|
+
},
|
|
468
|
+
async reconcileHelpers() {
|
|
469
|
+
const namespaces = await client.listNamespaces(managedProcessNamespaceLabelSelector());
|
|
470
|
+
for (const namespace of namespaces) {
|
|
471
|
+
const helpers = await client.listPods(namespace.name, managedExportHelperLabelSelector());
|
|
472
|
+
await Promise.all(
|
|
473
|
+
helpers.map((helper) =>
|
|
474
|
+
client.deletePod(helper.name, helper.namespace, { gracePeriodSeconds: 0 }),
|
|
475
|
+
),
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
let volumePool: ReturnType<typeof createKubernetesVolumePool> | undefined;
|
|
482
|
+
if (options.preProvision) {
|
|
483
|
+
if (!client.volumePool || !options.volume.storageClassName || !options.serverNamespace) {
|
|
484
|
+
throw new Error("Volume pre-provisioning requires an API, namespace and StorageClass");
|
|
485
|
+
}
|
|
486
|
+
volumePool = createKubernetesVolumePool({
|
|
487
|
+
api: client.volumePool,
|
|
488
|
+
namespace: options.serverNamespace,
|
|
489
|
+
storageClassName: options.volume.storageClassName,
|
|
490
|
+
size: options.volume.size,
|
|
491
|
+
accessModes: options.volume.accessModes,
|
|
492
|
+
...options.preProvision,
|
|
493
|
+
image: exporterImage,
|
|
494
|
+
imagePullPolicy: options.exporterImagePullPolicy,
|
|
495
|
+
imagePullSecrets: (options.pod?.imagePullSecrets ?? []).map(
|
|
496
|
+
(name) =>
|
|
497
|
+
options.imagePullSecretCopies?.find((copy) => copy.targetName === name)?.sourceName ??
|
|
498
|
+
name,
|
|
499
|
+
),
|
|
500
|
+
nodeSelector: options.pod?.nodeSelector,
|
|
501
|
+
tolerations: options.pod?.tolerations,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
return { runner, volume, exporter, volumePool };
|
|
228
505
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { type ChildProcess, spawn } from "node:child_process";
|
|
1
|
+
import { type ChildProcess, execFile, spawn } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { promisify } from "node:util";
|
|
6
|
+
import { createFilesystemProcessStateExporter } from "./filesystem-process-state-exporter.js";
|
|
5
7
|
import { UnitExitNotifier } from "./runner-utils.js";
|
|
6
8
|
import type {
|
|
7
9
|
LocalStartWorkerInput,
|
|
10
|
+
ProcessStateExporter,
|
|
8
11
|
StopWorkerOptions,
|
|
9
12
|
WorkerExitInfo,
|
|
10
13
|
WorkerRunner,
|
|
@@ -22,8 +25,34 @@ export interface LocalWorkerRunnerOptions {
|
|
|
22
25
|
args?: readonly string[];
|
|
23
26
|
/** Working directory for the spawned worker. Defaults to the installed server package dir. */
|
|
24
27
|
cwd?: string;
|
|
28
|
+
/** Process storage roots used by the read-only session exporter. */
|
|
29
|
+
processWorkspacesDir?: string;
|
|
30
|
+
treeFilesDir?: string;
|
|
31
|
+
/** Explicit acknowledgement that Docker processes inherit host Docker authority. */
|
|
32
|
+
allowHostDocker?: boolean;
|
|
25
33
|
/** Test seam. Production uses node's spawn directly. */
|
|
26
34
|
localWorkerSpawnImpl?: typeof spawn;
|
|
35
|
+
/** Test seam for the shared host-Docker preflight. */
|
|
36
|
+
dockerPreflightImpl?: (timeoutMs: number) => Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Verifies that the inherited Docker CLI context can reach its daemon. */
|
|
40
|
+
export async function preflightHostDocker(timeoutMs: number): Promise<void> {
|
|
41
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
42
|
+
throw new Error("Docker preflight requires a positive startup timeout");
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
await promisify(execFile)("docker", ["info"], {
|
|
46
|
+
env: process.env,
|
|
47
|
+
timeout: timeoutMs,
|
|
48
|
+
killSignal: "SIGKILL",
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error && typeof error === "object" && "killed" in error && error.killed) {
|
|
52
|
+
throw new Error(`Docker preflight timed out after ${timeoutMs}ms`, { cause: error });
|
|
53
|
+
}
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
27
56
|
}
|
|
28
57
|
|
|
29
58
|
interface LocalWorkerUnitState {
|
|
@@ -74,11 +103,13 @@ function exitInfo(code: number | null, signal: NodeJS.Signals | null): WorkerExi
|
|
|
74
103
|
*/
|
|
75
104
|
export function createLocalWorkerRunner(options: LocalWorkerRunnerOptions): {
|
|
76
105
|
runner: WorkerRunner<LocalStartWorkerInput>;
|
|
106
|
+
exporter: ProcessStateExporter;
|
|
77
107
|
} {
|
|
78
108
|
const command = options.command ?? "node";
|
|
79
109
|
const args = resolveLocalWorkerSpawnArgs(options.args ?? [DEFAULT_LOCAL_WORKER_ENTRY_SPECIFIER]);
|
|
80
110
|
const cwd = options.cwd ?? findServerPackageDir();
|
|
81
111
|
const localWorkerSpawnImpl = options.localWorkerSpawnImpl ?? spawn;
|
|
112
|
+
const dockerPreflightImpl = options.dockerPreflightImpl ?? preflightHostDocker;
|
|
82
113
|
const units = new Map<string, LocalWorkerUnitState>();
|
|
83
114
|
const exitNotifier = new UnitExitNotifier();
|
|
84
115
|
|
|
@@ -87,16 +118,50 @@ export function createLocalWorkerRunner(options: LocalWorkerRunnerOptions): {
|
|
|
87
118
|
}
|
|
88
119
|
|
|
89
120
|
const runner: WorkerRunner<LocalStartWorkerInput> = {
|
|
90
|
-
async start(input: LocalStartWorkerInput): Promise<WorkerUnit> {
|
|
91
|
-
|
|
92
|
-
|
|
121
|
+
async start(input: LocalStartWorkerInput, observer): Promise<WorkerUnit> {
|
|
122
|
+
observer?.report("preparing_runtime");
|
|
123
|
+
if (input.docker) {
|
|
124
|
+
if (options.allowHostDocker !== true) {
|
|
125
|
+
throw new Error("Docker-requiring processes need local_worker.allow_host_docker: true");
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
const configuredTimeoutMs = Number.parseInt(
|
|
129
|
+
input.env.LEITWERK_WORKER_STARTUP_TIMEOUT_MS ?? "30000",
|
|
130
|
+
10,
|
|
131
|
+
);
|
|
132
|
+
const deadlineMs = Number.parseInt(
|
|
133
|
+
input.env.LEITWERK_WORKER_STARTUP_DEADLINE_MS ?? "",
|
|
134
|
+
10,
|
|
135
|
+
);
|
|
136
|
+
const timeoutMs = Number.isFinite(deadlineMs)
|
|
137
|
+
? Math.min(configuredTimeoutMs, deadlineMs - Date.now())
|
|
138
|
+
: configuredTimeoutMs;
|
|
139
|
+
if (timeoutMs <= 0)
|
|
140
|
+
throw new Error("Worker startup deadline expired before Docker preflight");
|
|
141
|
+
await dockerPreflightImpl(timeoutMs);
|
|
142
|
+
if (Date.now() >= deadlineMs) {
|
|
143
|
+
throw new Error("Worker startup deadline expired during Docker preflight");
|
|
144
|
+
}
|
|
145
|
+
} catch (error) {
|
|
146
|
+
throw new Error("Local Docker preflight failed: Docker CLI or daemon is unavailable", {
|
|
147
|
+
cause: error,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
93
150
|
}
|
|
94
151
|
const unitId = nextUnitId(input.instanceId, input.workerId);
|
|
95
152
|
const ref: WorkerUnitRef = { instanceId: input.instanceId, workerId: input.workerId, unitId };
|
|
153
|
+
observer?.report("starting_runtime");
|
|
96
154
|
const child = localWorkerSpawnImpl(command, args, {
|
|
97
155
|
cwd,
|
|
98
156
|
stdio: ["ignore", "inherit", "inherit"],
|
|
99
|
-
env: {
|
|
157
|
+
env: {
|
|
158
|
+
...Object.fromEntries(
|
|
159
|
+
Object.entries(process.env).filter(
|
|
160
|
+
([key]) => !/(?:^|_)(?:TOKEN|API_KEY|PASSWORD|SECRET)$/.test(key),
|
|
161
|
+
),
|
|
162
|
+
),
|
|
163
|
+
...input.env,
|
|
164
|
+
},
|
|
100
165
|
});
|
|
101
166
|
const state: LocalWorkerUnitState = {
|
|
102
167
|
ref,
|
|
@@ -141,5 +206,16 @@ export function createLocalWorkerRunner(options: LocalWorkerRunnerOptions): {
|
|
|
141
206
|
},
|
|
142
207
|
};
|
|
143
208
|
|
|
144
|
-
|
|
209
|
+
const processWorkspacesDir = path.resolve(
|
|
210
|
+
options.processWorkspacesDir ?? "/tmp/leitwerk/workspaces",
|
|
211
|
+
);
|
|
212
|
+
const treeFilesDir = path.resolve(options.treeFilesDir ?? "/tmp/leitwerk/trees");
|
|
213
|
+
const exporter = createFilesystemProcessStateExporter({
|
|
214
|
+
allowedRoots: [processWorkspacesDir, treeFilesDir],
|
|
215
|
+
resolveSource: (instanceId) => ({
|
|
216
|
+
workspaceRoot: path.join(processWorkspacesDir, instanceId),
|
|
217
|
+
sessionFile: path.join(treeFilesDir, `${instanceId}.jsonl`),
|
|
218
|
+
}),
|
|
219
|
+
});
|
|
220
|
+
return { runner, exporter };
|
|
145
221
|
}
|
package/src/runner-utils.ts
CHANGED
|
@@ -31,7 +31,15 @@ export class UnitExitNotifier {
|
|
|
31
31
|
for (const listener of list.splice(0)) listener(info);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
wrapUnit<T extends WorkerUnitRef>(
|
|
35
|
-
|
|
34
|
+
wrapUnit<T extends WorkerUnitRef>(
|
|
35
|
+
ref: T,
|
|
36
|
+
key: string,
|
|
37
|
+
options?: { replacementHandoff?: WorkerUnit["replacementHandoff"] },
|
|
38
|
+
): WorkerUnit {
|
|
39
|
+
return {
|
|
40
|
+
...ref,
|
|
41
|
+
...(options?.replacementHandoff ? { replacementHandoff: options.replacementHandoff } : {}),
|
|
42
|
+
onExit: (listener) => this.onExit(key, listener),
|
|
43
|
+
};
|
|
36
44
|
}
|
|
37
45
|
}
|