@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
|
@@ -0,0 +1,881 @@
|
|
|
1
|
+
import {
|
|
2
|
+
WorkerStartDiagnosticError
|
|
3
|
+
} from "./chunk-7X5FBNT7.js";
|
|
4
|
+
import {
|
|
5
|
+
createHelperProcessStateExporter,
|
|
6
|
+
exportHelperEnvironment
|
|
7
|
+
} from "./chunk-4F7EMXMU.js";
|
|
8
|
+
import {
|
|
9
|
+
KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
10
|
+
KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
11
|
+
KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH,
|
|
12
|
+
buildKubernetesDockerConfigJsonSecretManifest,
|
|
13
|
+
buildKubernetesExportHelperPodManifest,
|
|
14
|
+
buildKubernetesProcessNamespaceManifest,
|
|
15
|
+
buildKubernetesProcessPvcManifest,
|
|
16
|
+
buildKubernetesServerCaConfigMapManifest,
|
|
17
|
+
buildKubernetesWorkerPodManifest,
|
|
18
|
+
kubernetesExportHelperPodName,
|
|
19
|
+
kubernetesProcessNamespaceName,
|
|
20
|
+
kubernetesProcessPvcName,
|
|
21
|
+
redactKubernetesDiagnostic,
|
|
22
|
+
volumeRefFromPvc
|
|
23
|
+
} from "./chunk-YJMHXKNO.js";
|
|
24
|
+
import {
|
|
25
|
+
WORKER_LABEL_COMPONENT,
|
|
26
|
+
WORKER_LABEL_INSTANCE_ID,
|
|
27
|
+
WORKER_LABEL_MANAGED_BY,
|
|
28
|
+
WORKER_LABEL_MANAGED_BY_VALUE,
|
|
29
|
+
managedExportHelperLabelSelector,
|
|
30
|
+
managedProcessNamespaceLabelSelector,
|
|
31
|
+
managedWorkerLabelSelector,
|
|
32
|
+
parseWorkerUnitIdentity
|
|
33
|
+
} from "./chunk-6BSZBMHH.js";
|
|
34
|
+
import {
|
|
35
|
+
UnitExitNotifier
|
|
36
|
+
} from "./chunk-OSS62AOL.js";
|
|
37
|
+
import {
|
|
38
|
+
SESSION_TRANSFER_HELPER_ENTRY_PATH,
|
|
39
|
+
SESSION_TRANSFER_HELPER_MOUNT_PATH
|
|
40
|
+
} from "./chunk-D56NJQ5X.js";
|
|
41
|
+
|
|
42
|
+
// src/kubernetes-worker-runner.ts
|
|
43
|
+
import { readFileSync } from "fs";
|
|
44
|
+
|
|
45
|
+
// src/kubernetes-startup-sampler.ts
|
|
46
|
+
function startupReceipt(observer, milestone, metadata = {}) {
|
|
47
|
+
try {
|
|
48
|
+
observer?.observe?.({
|
|
49
|
+
milestone,
|
|
50
|
+
observedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
51
|
+
sourceAt: null,
|
|
52
|
+
sourceKind: "server",
|
|
53
|
+
objectUid: null,
|
|
54
|
+
notBefore: null,
|
|
55
|
+
metadata
|
|
56
|
+
});
|
|
57
|
+
} catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function sampleKubernetesStartup(client, observer, namespace, pvcName) {
|
|
61
|
+
let pod;
|
|
62
|
+
let podUid;
|
|
63
|
+
let pvcUid;
|
|
64
|
+
let lastUnbound = null;
|
|
65
|
+
let stopped = false;
|
|
66
|
+
let timer;
|
|
67
|
+
let active;
|
|
68
|
+
let failures = 0;
|
|
69
|
+
const seen = /* @__PURE__ */ new Set();
|
|
70
|
+
const deadline = Date.now() + 12e4;
|
|
71
|
+
const emit = (milestone, objectUid, sourceAt, metadata, sourceKind = "kubernetes_status", notBefore = null) => {
|
|
72
|
+
if (stopped || seen.has(milestone) || sourceAt !== null && !Number.isFinite(Date.parse(sourceAt)))
|
|
73
|
+
return;
|
|
74
|
+
observer.observe?.({
|
|
75
|
+
milestone,
|
|
76
|
+
observedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
77
|
+
sourceAt,
|
|
78
|
+
objectUid,
|
|
79
|
+
sourceKind,
|
|
80
|
+
notBefore,
|
|
81
|
+
metadata: {
|
|
82
|
+
...metadata,
|
|
83
|
+
...sourceAt !== null ? { precision: sourcePrecision(sourceAt) } : {}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
seen.add(milestone);
|
|
87
|
+
};
|
|
88
|
+
const stop = () => {
|
|
89
|
+
stopped = true;
|
|
90
|
+
if (timer) clearTimeout(timer);
|
|
91
|
+
active?.abort();
|
|
92
|
+
};
|
|
93
|
+
const reportFailure = () => {
|
|
94
|
+
if (!stopped && failures++ < 3)
|
|
95
|
+
console.warn("Worker startup observation unavailable: Kubernetes collection failed");
|
|
96
|
+
};
|
|
97
|
+
const cycle = async () => {
|
|
98
|
+
if (stopped || observer.shouldStop?.() || Date.now() >= deadline) {
|
|
99
|
+
stop();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
active = new AbortController();
|
|
103
|
+
const timeout = setTimeout(() => active?.abort(), 1500);
|
|
104
|
+
const signal = active.signal;
|
|
105
|
+
const collectPvc = async () => {
|
|
106
|
+
if (!seen.has("pvc_bound") && client.getPersistentVolumeClaim) {
|
|
107
|
+
const pvc = await client.getPersistentVolumeClaim(pvcName, namespace, { signal });
|
|
108
|
+
if (pvc?.uid) {
|
|
109
|
+
if (pvcUid && pvcUid !== pvc.uid) {
|
|
110
|
+
stop();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
pvcUid = pvc.uid;
|
|
114
|
+
if (pvc.phase === "Bound")
|
|
115
|
+
emit(
|
|
116
|
+
"pvc_bound",
|
|
117
|
+
pvc.uid,
|
|
118
|
+
null,
|
|
119
|
+
{ storageClass: pvc.storageClass, precision: "sampling_interval" },
|
|
120
|
+
"kubernetes_status",
|
|
121
|
+
lastUnbound
|
|
122
|
+
);
|
|
123
|
+
else lastUnbound = (/* @__PURE__ */ new Date()).toISOString();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const collectPod = async () => {
|
|
128
|
+
if (pod) {
|
|
129
|
+
const summary = await client.getPod(pod.name, namespace, { signal });
|
|
130
|
+
if (!summary) {
|
|
131
|
+
stop();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const identity = parseWorkerUnitIdentity(summary.labels);
|
|
135
|
+
if (!identity || identity.workerId !== pod.workerId || identity.instanceId !== pod.instanceId || podUid && podUid !== summary.uid) {
|
|
136
|
+
stop();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (summary.uid) {
|
|
140
|
+
podUid = summary.uid;
|
|
141
|
+
const metadata = {
|
|
142
|
+
node: summary.node,
|
|
143
|
+
imageId: summary.imageId,
|
|
144
|
+
namespace,
|
|
145
|
+
podName: pod.name,
|
|
146
|
+
...summary.resources
|
|
147
|
+
};
|
|
148
|
+
if (summary.createdAt) emit("pod_created", podUid, summary.createdAt, metadata);
|
|
149
|
+
if (summary.scheduledAt) emit("pod_scheduled", podUid, summary.scheduledAt, metadata);
|
|
150
|
+
if (summary.containerStartedAt)
|
|
151
|
+
emit("container_started", podUid, summary.containerStartedAt, metadata);
|
|
152
|
+
const events = await client.listPodEvents(pod.name, namespace, { signal });
|
|
153
|
+
for (const event of events) {
|
|
154
|
+
if (event.objectUid !== podUid || event.fieldPath !== "spec.containers{worker}")
|
|
155
|
+
continue;
|
|
156
|
+
const at = event.eventTime || event.firstTimestamp;
|
|
157
|
+
if (!at) continue;
|
|
158
|
+
if (event.reason === "Pulling")
|
|
159
|
+
emit("image_pull_started", podUid, at, metadata, "kubernetes_event");
|
|
160
|
+
if (event.reason === "Pulled")
|
|
161
|
+
emit(
|
|
162
|
+
event.message?.includes("already present") ? "image_cached" : "image_pull_finished",
|
|
163
|
+
podUid,
|
|
164
|
+
at,
|
|
165
|
+
metadata,
|
|
166
|
+
"kubernetes_event"
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (summary.phase === "Failed" || summary.phase === "Succeeded") stop();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
try {
|
|
174
|
+
await Promise.all([collectPvc().catch(reportFailure), collectPod().catch(reportFailure)]);
|
|
175
|
+
if (seen.has("container_started") && seen.has("pod_scheduled") && seen.has("pvc_bound") && (seen.has("image_cached") || seen.has("image_pull_started") && seen.has("image_pull_finished")))
|
|
176
|
+
stop();
|
|
177
|
+
} finally {
|
|
178
|
+
clearTimeout(timeout);
|
|
179
|
+
active = void 0;
|
|
180
|
+
if (!stopped) {
|
|
181
|
+
timer = setTimeout(() => void cycle(), 250);
|
|
182
|
+
timer.unref?.();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
timer = setTimeout(() => void cycle(), 0);
|
|
187
|
+
timer.unref?.();
|
|
188
|
+
return {
|
|
189
|
+
stop,
|
|
190
|
+
attachPod(input) {
|
|
191
|
+
pod = input;
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function sourcePrecision(timestamp) {
|
|
196
|
+
const digits = /\.(\d+)/.exec(timestamp)?.[1].length ?? 0;
|
|
197
|
+
return digits === 0 ? "seconds" : digits <= 3 ? "milliseconds" : "microseconds";
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/kubernetes-volume-pool.ts
|
|
201
|
+
import { createHash, randomUUID } from "crypto";
|
|
202
|
+
import { setTimeout as delay } from "timers/promises";
|
|
203
|
+
var POOL_LABEL = "leitwerk.dev/volume-pool";
|
|
204
|
+
var CLAIM_UID = "leitwerk.dev/preparation-claim-uid";
|
|
205
|
+
var CLAIM_NAME = "leitwerk.dev/preparation-claim-name";
|
|
206
|
+
var POLICY = "leitwerk.dev/original-reclaim-policy";
|
|
207
|
+
var TARGET_CLASS = "leitwerk.dev/target-storage-class";
|
|
208
|
+
var STATE = "leitwerk.dev/volume-pool-state";
|
|
209
|
+
var SIZE = "leitwerk.dev/preparation-size";
|
|
210
|
+
var MODES = "leitwerk.dev/preparation-access-modes";
|
|
211
|
+
function createKubernetesVolumePool(options) {
|
|
212
|
+
const { api, namespace } = options;
|
|
213
|
+
if (!Number.isSafeInteger(options.count) || options.count < 0)
|
|
214
|
+
throw new Error("Invalid volume pool count");
|
|
215
|
+
const poolId = createHash("sha256").update(`${namespace}/${options.storageClassName}`).digest("hex").slice(0, 16);
|
|
216
|
+
const selector = `${POOL_LABEL}=${poolId}`;
|
|
217
|
+
const labels = {
|
|
218
|
+
"leitwerk.dev/managed-by": "leitwerk",
|
|
219
|
+
[POOL_LABEL]: poolId,
|
|
220
|
+
"leitwerk.dev/component": "volume-preparation"
|
|
221
|
+
};
|
|
222
|
+
let controller;
|
|
223
|
+
let running;
|
|
224
|
+
function preparationPod(claim) {
|
|
225
|
+
return {
|
|
226
|
+
apiVersion: "v1",
|
|
227
|
+
kind: "Pod",
|
|
228
|
+
metadata: {
|
|
229
|
+
name: claim.metadata.name,
|
|
230
|
+
namespace,
|
|
231
|
+
labels,
|
|
232
|
+
ownerReferences: [
|
|
233
|
+
{
|
|
234
|
+
apiVersion: "v1",
|
|
235
|
+
kind: "PersistentVolumeClaim",
|
|
236
|
+
name: claim.metadata.name,
|
|
237
|
+
uid: claim.metadata.uid ?? ""
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
},
|
|
241
|
+
spec: {
|
|
242
|
+
restartPolicy: "Never",
|
|
243
|
+
serviceAccountName: "default",
|
|
244
|
+
automountServiceAccountToken: false,
|
|
245
|
+
nodeSelector: options.nodeSelector ?? {},
|
|
246
|
+
tolerations: options.tolerations ?? [],
|
|
247
|
+
imagePullSecrets: (options.imagePullSecrets ?? []).map((name) => ({ name })),
|
|
248
|
+
containers: [
|
|
249
|
+
{
|
|
250
|
+
name: "prepare",
|
|
251
|
+
image: options.image,
|
|
252
|
+
imagePullPolicy: options.imagePullPolicy ?? "IfNotPresent",
|
|
253
|
+
command: ["node", "-e", "process.exit(0)"],
|
|
254
|
+
resources: {
|
|
255
|
+
requests: { cpu: "1m", memory: "32Mi" },
|
|
256
|
+
limits: { cpu: "100m", memory: "64Mi" }
|
|
257
|
+
},
|
|
258
|
+
securityContext: {
|
|
259
|
+
allowPrivilegeEscalation: false,
|
|
260
|
+
readOnlyRootFilesystem: true,
|
|
261
|
+
capabilities: { drop: ["ALL"] }
|
|
262
|
+
},
|
|
263
|
+
volumeMounts: [{ name: "volume", mountPath: "/volume" }]
|
|
264
|
+
}
|
|
265
|
+
],
|
|
266
|
+
volumes: [{ name: "volume", persistentVolumeClaim: { claimName: claim.metadata.name } }]
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async function advanceVolume(pv, signal) {
|
|
271
|
+
const annotations = pv.metadata.annotations ?? {};
|
|
272
|
+
const uid = annotations[CLAIM_UID];
|
|
273
|
+
const name = annotations[CLAIM_NAME];
|
|
274
|
+
const policy = annotations[POLICY];
|
|
275
|
+
if (!uid || !name || !["Delete", "Retain"].includes(policy))
|
|
276
|
+
throw new Error("Invalid prepared-volume ownership");
|
|
277
|
+
if (pv.metadata.deletionTimestamp) return false;
|
|
278
|
+
const claim = pv.spec.claimRef;
|
|
279
|
+
if (claim && claim.uid !== uid) {
|
|
280
|
+
const remainingLabels = { ...pv.metadata.labels };
|
|
281
|
+
delete remainingLabels[POOL_LABEL];
|
|
282
|
+
await api.patchVolume(
|
|
283
|
+
pv,
|
|
284
|
+
[
|
|
285
|
+
{ op: "add", path: "/spec/persistentVolumeReclaimPolicy", value: policy },
|
|
286
|
+
{ op: "add", path: "/metadata/labels", value: remainingLabels }
|
|
287
|
+
],
|
|
288
|
+
signal
|
|
289
|
+
);
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
if (claim) {
|
|
293
|
+
if (pv.spec.persistentVolumeReclaimPolicy !== "Retain")
|
|
294
|
+
throw new Error("Staging PV must be retained before release");
|
|
295
|
+
const pod = await api.get("pods", namespace, name, signal);
|
|
296
|
+
if (pod) {
|
|
297
|
+
if (pod.metadata.labels?.[POOL_LABEL] !== poolId || !pod.metadata.ownerReferences?.some((owner) => owner.uid === uid))
|
|
298
|
+
throw new Error("Preparation Pod ownership mismatch");
|
|
299
|
+
await api.delete("pods", namespace, pod, signal);
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
const pvc = await api.get("persistentvolumeclaims", namespace, name, signal);
|
|
303
|
+
if (pvc) {
|
|
304
|
+
if (pvc.metadata.uid !== uid) throw new Error("Preparation PVC ownership mismatch");
|
|
305
|
+
await api.delete("persistentvolumeclaims", namespace, pvc, signal);
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
if (pv.status?.phase === "Released") {
|
|
309
|
+
await api.patchVolume(
|
|
310
|
+
pv,
|
|
311
|
+
[
|
|
312
|
+
{ op: "test", path: "/spec/claimRef/uid", value: uid },
|
|
313
|
+
{ op: "remove", path: "/spec/claimRef" }
|
|
314
|
+
],
|
|
315
|
+
signal
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
return true;
|
|
319
|
+
}
|
|
320
|
+
if (pv.status?.phase === "Available" && annotations[STATE] !== "ready") {
|
|
321
|
+
const stagingClass = await api.getStorageClass(name, signal);
|
|
322
|
+
if (stagingClass) {
|
|
323
|
+
if (stagingClass.metadata.labels?.[POOL_LABEL] !== poolId)
|
|
324
|
+
throw new Error("Staging StorageClass ownership mismatch");
|
|
325
|
+
await api.deleteStorageClass(stagingClass, signal);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
const targetClass = annotations[TARGET_CLASS];
|
|
329
|
+
if (!targetClass) throw new Error("Target StorageClass is missing");
|
|
330
|
+
await api.patchVolume(
|
|
331
|
+
pv,
|
|
332
|
+
[
|
|
333
|
+
{ op: "add", path: "/spec/persistentVolumeReclaimPolicy", value: policy },
|
|
334
|
+
{ op: "add", path: "/spec/storageClassName", value: targetClass },
|
|
335
|
+
{ op: "add", path: "/metadata/annotations", value: { ...annotations, [STATE]: "ready" } }
|
|
336
|
+
],
|
|
337
|
+
signal
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
return annotations[STATE] !== "ready" || annotations[SIZE] === options.size && annotations[MODES] === JSON.stringify(options.accessModes);
|
|
341
|
+
}
|
|
342
|
+
async function reconcile(signal) {
|
|
343
|
+
const volumes = await api.list("persistentvolumes", "", selector, signal);
|
|
344
|
+
const classes = await api.listStorageClasses(selector, signal);
|
|
345
|
+
const active = new Set(classes.map((value) => value.metadata.name));
|
|
346
|
+
for (const pv of volumes) {
|
|
347
|
+
if (await advanceVolume(pv, signal)) active.add(pv.metadata.annotations?.[CLAIM_NAME] ?? "");
|
|
348
|
+
}
|
|
349
|
+
const claims = await api.list("persistentvolumeclaims", namespace, selector, signal);
|
|
350
|
+
for (const claim of claims) {
|
|
351
|
+
active.add(claim.metadata.name);
|
|
352
|
+
if (!claim.metadata.uid || claim.spec.storageClassName !== claim.metadata.name)
|
|
353
|
+
throw new Error("Invalid staging PVC identity");
|
|
354
|
+
if (claim.metadata.deletionTimestamp || volumes.some((pv2) => pv2.metadata.annotations?.[CLAIM_UID] === claim.metadata.uid))
|
|
355
|
+
continue;
|
|
356
|
+
const pod = await api.get("pods", namespace, claim.metadata.name, signal);
|
|
357
|
+
if (!pod) {
|
|
358
|
+
await api.create("pods", namespace, preparationPod(claim), signal);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (pod.metadata.labels?.[POOL_LABEL] !== poolId || !pod.metadata.ownerReferences?.some((owner) => owner.uid === claim.metadata.uid))
|
|
362
|
+
throw new Error("Preparation Pod ownership mismatch");
|
|
363
|
+
if (pod.status?.phase === "Failed") {
|
|
364
|
+
await api.delete("pods", namespace, pod, signal);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (pod.status?.phase !== "Succeeded" || !claim.spec.volumeName) continue;
|
|
368
|
+
const pv = await api.get("persistentvolumes", "", claim.spec.volumeName, signal);
|
|
369
|
+
if (!pv || pv.spec.claimRef?.uid !== claim.metadata.uid || pv.metadata.ownerReferences?.length || pv.metadata.deletionTimestamp)
|
|
370
|
+
throw new Error("Cannot safely retain staging volume");
|
|
371
|
+
if (pv.metadata.labels?.[POOL_LABEL]) throw new Error("PV already belongs to a pool");
|
|
372
|
+
const policy = pv.spec.persistentVolumeReclaimPolicy;
|
|
373
|
+
if (!policy || !["Delete", "Retain"].includes(policy))
|
|
374
|
+
throw new Error("Unsupported reclaim policy");
|
|
375
|
+
await api.patchVolume(
|
|
376
|
+
pv,
|
|
377
|
+
[
|
|
378
|
+
{
|
|
379
|
+
op: "add",
|
|
380
|
+
path: "/metadata/labels",
|
|
381
|
+
value: { ...pv.metadata.labels, [POOL_LABEL]: poolId }
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
op: "add",
|
|
385
|
+
path: "/metadata/annotations",
|
|
386
|
+
value: {
|
|
387
|
+
...pv.metadata.annotations,
|
|
388
|
+
[CLAIM_UID]: claim.metadata.uid,
|
|
389
|
+
[CLAIM_NAME]: claim.metadata.name,
|
|
390
|
+
[POLICY]: policy,
|
|
391
|
+
[STATE]: "preparing",
|
|
392
|
+
[TARGET_CLASS]: options.storageClassName,
|
|
393
|
+
[SIZE]: claim.metadata.annotations?.[SIZE],
|
|
394
|
+
[MODES]: claim.metadata.annotations?.[MODES]
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
{ op: "add", path: "/spec/persistentVolumeReclaimPolicy", value: "Retain" }
|
|
398
|
+
],
|
|
399
|
+
signal
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
for (const stagingClass of classes) {
|
|
403
|
+
const name = stagingClass.metadata.name;
|
|
404
|
+
if (stagingClass.metadata.deletionTimestamp || claims.some((claim) => claim.metadata.name === name) || volumes.some((pv) => pv.metadata.annotations?.[CLAIM_NAME] === name))
|
|
405
|
+
continue;
|
|
406
|
+
await api.create(
|
|
407
|
+
"persistentvolumeclaims",
|
|
408
|
+
namespace,
|
|
409
|
+
{
|
|
410
|
+
apiVersion: "v1",
|
|
411
|
+
kind: "PersistentVolumeClaim",
|
|
412
|
+
metadata: {
|
|
413
|
+
name,
|
|
414
|
+
namespace,
|
|
415
|
+
labels,
|
|
416
|
+
annotations: {
|
|
417
|
+
[SIZE]: stagingClass.metadata.annotations?.[SIZE] ?? "",
|
|
418
|
+
[MODES]: stagingClass.metadata.annotations?.[MODES] ?? ""
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
spec: {
|
|
422
|
+
storageClassName: name,
|
|
423
|
+
accessModes: JSON.parse(stagingClass.metadata.annotations?.[MODES] ?? "null"),
|
|
424
|
+
volumeMode: "Filesystem",
|
|
425
|
+
resources: {
|
|
426
|
+
requests: {
|
|
427
|
+
storage: stagingClass.metadata.annotations?.[SIZE]
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
signal
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (active.size < options.count) {
|
|
436
|
+
const source = await api.getStorageClass(options.storageClassName, signal);
|
|
437
|
+
if (!source || ["kubernetes.io/no-provisioner", "manual"].includes(source.provisioner))
|
|
438
|
+
throw new Error("Volume pre-provisioning requires a dynamic StorageClass");
|
|
439
|
+
for (let i = active.size; i < options.count; i++) {
|
|
440
|
+
signal.throwIfAborted();
|
|
441
|
+
await api.createStorageClass(
|
|
442
|
+
{
|
|
443
|
+
apiVersion: "storage.k8s.io/v1",
|
|
444
|
+
kind: "StorageClass",
|
|
445
|
+
metadata: {
|
|
446
|
+
name: `lw-volume-${randomUUID()}`,
|
|
447
|
+
labels,
|
|
448
|
+
annotations: {
|
|
449
|
+
...Object.fromEntries(
|
|
450
|
+
Object.entries(source.metadata.annotations ?? {}).filter(
|
|
451
|
+
([key]) => ![
|
|
452
|
+
"storageclass.kubernetes.io/is-default-class",
|
|
453
|
+
"storageclass.beta.kubernetes.io/is-default-class",
|
|
454
|
+
"kubectl.kubernetes.io/last-applied-configuration"
|
|
455
|
+
].includes(key)
|
|
456
|
+
)
|
|
457
|
+
),
|
|
458
|
+
[SIZE]: options.size,
|
|
459
|
+
[MODES]: JSON.stringify(options.accessModes)
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
provisioner: source.provisioner,
|
|
463
|
+
parameters: source.parameters,
|
|
464
|
+
reclaimPolicy: source.reclaimPolicy,
|
|
465
|
+
mountOptions: source.mountOptions,
|
|
466
|
+
allowVolumeExpansion: source.allowVolumeExpansion,
|
|
467
|
+
volumeBindingMode: source.volumeBindingMode,
|
|
468
|
+
allowedTopologies: source.allowedTopologies
|
|
469
|
+
},
|
|
470
|
+
signal
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return {
|
|
476
|
+
poolId,
|
|
477
|
+
reconcile,
|
|
478
|
+
start() {
|
|
479
|
+
if (controller) return;
|
|
480
|
+
controller = new AbortController();
|
|
481
|
+
const { signal } = controller;
|
|
482
|
+
running = (async () => {
|
|
483
|
+
while (!signal.aborted) {
|
|
484
|
+
let interval = 2e3;
|
|
485
|
+
try {
|
|
486
|
+
await reconcile(signal);
|
|
487
|
+
} catch {
|
|
488
|
+
interval = 3e4;
|
|
489
|
+
if (!signal.aborted) options.onError?.();
|
|
490
|
+
}
|
|
491
|
+
try {
|
|
492
|
+
await delay(interval, void 0, { signal });
|
|
493
|
+
} catch {
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
})();
|
|
498
|
+
},
|
|
499
|
+
async stop() {
|
|
500
|
+
controller?.abort();
|
|
501
|
+
await running;
|
|
502
|
+
controller = void 0;
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/kubernetes-worker-runner.ts
|
|
508
|
+
var KUBERNETES_START_DIAGNOSTIC_MAX_LENGTH = 2048;
|
|
509
|
+
function boundedKubernetesStartDiagnostic(error, sensitiveValues) {
|
|
510
|
+
const detail = error instanceof Error ? error.message : "Kubernetes Pod admission failed";
|
|
511
|
+
return redactKubernetesDiagnostic(detail, sensitiveValues).slice(
|
|
512
|
+
0,
|
|
513
|
+
KUBERNETES_START_DIAGNOSTIC_MAX_LENGTH
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
function createKubernetesWorkerRunner(options) {
|
|
517
|
+
if (!options.serverUrl || !options.exporterImage || !options.helperRelays) {
|
|
518
|
+
throw new Error("Kubernetes transfer exporter is not configured");
|
|
519
|
+
}
|
|
520
|
+
const { client } = options;
|
|
521
|
+
const exporterServerUrl = options.serverUrl;
|
|
522
|
+
const exporterImage = options.exporterImage;
|
|
523
|
+
const helperRelays = options.helperRelays;
|
|
524
|
+
const serverCaFile = options.serverCaFile?.trim();
|
|
525
|
+
const namespaceForProcess = (instanceId) => kubernetesProcessNamespaceName(instanceId, options.processNamespacePrefix);
|
|
526
|
+
const delay2 = options.delay ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
527
|
+
const podDisappearanceTimeoutMs = options.podDisappearanceTimeoutMs ?? 3e4;
|
|
528
|
+
const podDisappearancePollIntervalMs = options.podDisappearancePollIntervalMs ?? 250;
|
|
529
|
+
const samplers = /* @__PURE__ */ new Map();
|
|
530
|
+
const volume = {
|
|
531
|
+
async ensure(instanceId, requirements, observer) {
|
|
532
|
+
const namespaceManifest = buildKubernetesProcessNamespaceManifest({
|
|
533
|
+
instanceId,
|
|
534
|
+
processNamespacePrefix: options.processNamespacePrefix
|
|
535
|
+
});
|
|
536
|
+
await client.ensureNamespace(namespaceManifest);
|
|
537
|
+
const copySecrets = (options.imagePullSecretCopies ?? []).map(async (copy) => {
|
|
538
|
+
const dockerConfigJson = await client.getDockerConfigJsonSecret(
|
|
539
|
+
copy.sourceName,
|
|
540
|
+
options.serverNamespace ?? "leitwerk-system"
|
|
541
|
+
);
|
|
542
|
+
await client.ensureDockerConfigJsonSecret(
|
|
543
|
+
buildKubernetesDockerConfigJsonSecretManifest({
|
|
544
|
+
instanceId,
|
|
545
|
+
namespace: namespaceManifest.metadata.name,
|
|
546
|
+
name: copy.targetName,
|
|
547
|
+
dockerConfigJson
|
|
548
|
+
})
|
|
549
|
+
);
|
|
550
|
+
});
|
|
551
|
+
const workerServiceAccount = options.pod?.workerServiceAccount?.trim();
|
|
552
|
+
const manifest = buildKubernetesProcessPvcManifest({
|
|
553
|
+
instanceId,
|
|
554
|
+
namespace: namespaceManifest.metadata.name,
|
|
555
|
+
volume: {
|
|
556
|
+
...options.volume,
|
|
557
|
+
size: requirements?.size ?? options.volume.size,
|
|
558
|
+
...requirements?.docker && options.docker ? { storageClassName: options.docker.processStorageClassName } : {}
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
await Promise.all([
|
|
562
|
+
...copySecrets,
|
|
563
|
+
...serverCaFile ? [
|
|
564
|
+
client.ensureConfigMap(
|
|
565
|
+
buildKubernetesServerCaConfigMapManifest({
|
|
566
|
+
instanceId,
|
|
567
|
+
namespace: namespaceManifest.metadata.name,
|
|
568
|
+
caPem: readFileSync(serverCaFile, "utf8")
|
|
569
|
+
})
|
|
570
|
+
)
|
|
571
|
+
] : [],
|
|
572
|
+
...workerServiceAccount && workerServiceAccount !== "default" ? [
|
|
573
|
+
client.ensureServiceAccount(workerServiceAccount, namespaceManifest.metadata.name, {
|
|
574
|
+
[WORKER_LABEL_MANAGED_BY]: WORKER_LABEL_MANAGED_BY_VALUE,
|
|
575
|
+
[WORKER_LABEL_COMPONENT]: "worker-service-account",
|
|
576
|
+
[WORKER_LABEL_INSTANCE_ID]: instanceId
|
|
577
|
+
})
|
|
578
|
+
] : [],
|
|
579
|
+
(async () => {
|
|
580
|
+
startupReceipt(observer, "pvc_requested", {
|
|
581
|
+
storageClass: manifest.spec.storageClassName
|
|
582
|
+
});
|
|
583
|
+
await client.ensurePersistentVolumeClaim(manifest);
|
|
584
|
+
startupReceipt(observer, "pvc_acknowledged", {
|
|
585
|
+
storageClass: manifest.spec.storageClassName
|
|
586
|
+
});
|
|
587
|
+
if (observer?.observe) {
|
|
588
|
+
samplers.get(instanceId)?.stop();
|
|
589
|
+
samplers.set(
|
|
590
|
+
instanceId,
|
|
591
|
+
sampleKubernetesStartup(
|
|
592
|
+
client,
|
|
593
|
+
observer,
|
|
594
|
+
manifest.metadata.namespace,
|
|
595
|
+
manifest.metadata.name
|
|
596
|
+
)
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
})()
|
|
600
|
+
]);
|
|
601
|
+
return volumeRefFromPvc({
|
|
602
|
+
instanceId,
|
|
603
|
+
pvcName: manifest.metadata.name,
|
|
604
|
+
mountPath: options.volume.mountPath,
|
|
605
|
+
namespace: manifest.metadata.namespace
|
|
606
|
+
});
|
|
607
|
+
},
|
|
608
|
+
async release(instanceId) {
|
|
609
|
+
await client.deletePersistentVolumeClaim(
|
|
610
|
+
kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
611
|
+
namespaceForProcess(instanceId)
|
|
612
|
+
);
|
|
613
|
+
},
|
|
614
|
+
async deleteProcessResources(instanceId) {
|
|
615
|
+
samplers.get(instanceId)?.stop();
|
|
616
|
+
samplers.delete(instanceId);
|
|
617
|
+
await client.deleteNamespace(namespaceForProcess(instanceId));
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
const exitNotifier = new UnitExitNotifier();
|
|
621
|
+
const unsubscribers = /* @__PURE__ */ new Map();
|
|
622
|
+
const disappearancePromises = /* @__PURE__ */ new Map();
|
|
623
|
+
function unitKey(namespace, unitId) {
|
|
624
|
+
return `${namespace}/${unitId}`;
|
|
625
|
+
}
|
|
626
|
+
function deletePodAndWait(name, namespace, gracePeriodSeconds) {
|
|
627
|
+
const key = unitKey(namespace, name);
|
|
628
|
+
const existing = disappearancePromises.get(key);
|
|
629
|
+
if (existing) return existing;
|
|
630
|
+
const operation = Promise.resolve().then(async () => {
|
|
631
|
+
const deadline = Date.now() + podDisappearanceTimeoutMs;
|
|
632
|
+
const timeoutError = new Error(
|
|
633
|
+
`Kubernetes worker Pod ${namespace}/${name} did not disappear within ${podDisappearanceTimeoutMs}ms; replacement was not started`
|
|
634
|
+
);
|
|
635
|
+
const controller = new AbortController();
|
|
636
|
+
const { signal } = controller;
|
|
637
|
+
let rejectTimeout;
|
|
638
|
+
const timedOut = new Promise((_resolve, reject) => {
|
|
639
|
+
rejectTimeout = reject;
|
|
640
|
+
});
|
|
641
|
+
const timer = setTimeout(() => {
|
|
642
|
+
controller.abort(timeoutError);
|
|
643
|
+
rejectTimeout(timeoutError);
|
|
644
|
+
}, podDisappearanceTimeoutMs);
|
|
645
|
+
const waitForDisappearance = async () => {
|
|
646
|
+
await client.deletePod(name, namespace, { gracePeriodSeconds, signal });
|
|
647
|
+
while (true) {
|
|
648
|
+
signal.throwIfAborted();
|
|
649
|
+
if (Date.now() >= deadline) throw timeoutError;
|
|
650
|
+
const pod = await client.getPod(name, namespace, { signal });
|
|
651
|
+
signal.throwIfAborted();
|
|
652
|
+
if (Date.now() >= deadline) throw timeoutError;
|
|
653
|
+
if (!pod) return;
|
|
654
|
+
await delay2(
|
|
655
|
+
Math.min(podDisappearancePollIntervalMs, Math.max(0, deadline - Date.now()))
|
|
656
|
+
);
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
try {
|
|
660
|
+
await Promise.race([waitForDisappearance(), timedOut]);
|
|
661
|
+
} catch (error) {
|
|
662
|
+
if (signal.aborted) throw timeoutError;
|
|
663
|
+
throw error;
|
|
664
|
+
} finally {
|
|
665
|
+
clearTimeout(timer);
|
|
666
|
+
}
|
|
667
|
+
}).finally(() => disappearancePromises.delete(key));
|
|
668
|
+
disappearancePromises.set(key, operation);
|
|
669
|
+
return operation;
|
|
670
|
+
}
|
|
671
|
+
function watchPod(unitId, namespace, sensitiveValues = []) {
|
|
672
|
+
const key = unitKey(namespace, unitId);
|
|
673
|
+
if (unsubscribers.has(key)) return;
|
|
674
|
+
const unsubscribe = client.onPodExit(
|
|
675
|
+
unitId,
|
|
676
|
+
namespace,
|
|
677
|
+
(info) => {
|
|
678
|
+
exitNotifier.fireExit(key, info);
|
|
679
|
+
unsubscribers.get(key)?.();
|
|
680
|
+
unsubscribers.delete(key);
|
|
681
|
+
},
|
|
682
|
+
{ sensitiveValues }
|
|
683
|
+
);
|
|
684
|
+
unsubscribers.set(key, unsubscribe);
|
|
685
|
+
}
|
|
686
|
+
const runner = {
|
|
687
|
+
async start(input, observer) {
|
|
688
|
+
observer?.report("preparing_runtime");
|
|
689
|
+
if (input.docker && !options.docker) {
|
|
690
|
+
throw new Error(
|
|
691
|
+
"Kubernetes Docker runtime is unavailable; configure kubernetes.docker completely"
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
const processNamespace = input.volume.namespace ?? namespaceForProcess(input.instanceId);
|
|
695
|
+
const manifest = buildKubernetesWorkerPodManifest(input, {
|
|
696
|
+
namespace: processNamespace,
|
|
697
|
+
...options.pod ?? {},
|
|
698
|
+
...input.docker && options.docker ? { docker: options.docker } : {},
|
|
699
|
+
...serverCaFile ? {
|
|
700
|
+
serverCaConfigMap: {
|
|
701
|
+
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
702
|
+
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
703
|
+
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH
|
|
704
|
+
}
|
|
705
|
+
} : {}
|
|
706
|
+
});
|
|
707
|
+
observer?.report("allocating_runtime");
|
|
708
|
+
try {
|
|
709
|
+
startupReceipt(observer, "pod_requested", {
|
|
710
|
+
image: input.image.reference,
|
|
711
|
+
...input.resources
|
|
712
|
+
});
|
|
713
|
+
await client.createPod(manifest);
|
|
714
|
+
startupReceipt(observer, "pod_acknowledged");
|
|
715
|
+
samplers.get(input.instanceId)?.attachPod({
|
|
716
|
+
name: manifest.metadata.name,
|
|
717
|
+
workerId: input.workerId,
|
|
718
|
+
instanceId: input.instanceId
|
|
719
|
+
});
|
|
720
|
+
} catch (error) {
|
|
721
|
+
samplers.get(input.instanceId)?.stop();
|
|
722
|
+
samplers.delete(input.instanceId);
|
|
723
|
+
throw new WorkerStartDiagnosticError(
|
|
724
|
+
boundedKubernetesStartDiagnostic(error, Object.values(input.env)),
|
|
725
|
+
error
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
observer?.report("starting_runtime");
|
|
729
|
+
const ref = {
|
|
730
|
+
instanceId: input.instanceId,
|
|
731
|
+
workerId: input.workerId,
|
|
732
|
+
unitId: manifest.metadata.name,
|
|
733
|
+
namespace: manifest.metadata.namespace
|
|
734
|
+
};
|
|
735
|
+
watchPod(ref.unitId, ref.namespace ?? processNamespace, Object.values(input.env));
|
|
736
|
+
return exitNotifier.wrapUnit(ref, unitKey(ref.namespace ?? processNamespace, ref.unitId), {
|
|
737
|
+
replacementHandoff: "stop-before-replacement"
|
|
738
|
+
});
|
|
739
|
+
},
|
|
740
|
+
async stop(ref, opts) {
|
|
741
|
+
samplers.get(ref.instanceId)?.stop();
|
|
742
|
+
samplers.delete(ref.instanceId);
|
|
743
|
+
await deletePodAndWait(
|
|
744
|
+
ref.unitId,
|
|
745
|
+
ref.namespace ?? namespaceForProcess(ref.instanceId),
|
|
746
|
+
Math.max(0, Math.ceil(opts.graceMs / 1e3))
|
|
747
|
+
);
|
|
748
|
+
},
|
|
749
|
+
async list() {
|
|
750
|
+
const namespaces = await client.listNamespaces(managedProcessNamespaceLabelSelector());
|
|
751
|
+
const descriptors = [];
|
|
752
|
+
for (const namespace of namespaces) {
|
|
753
|
+
if (!namespace.name.startsWith(options.processNamespacePrefix)) continue;
|
|
754
|
+
const pods = await client.listPods(namespace.name, managedWorkerLabelSelector());
|
|
755
|
+
for (const pod of pods) {
|
|
756
|
+
const identity = parseWorkerUnitIdentity(pod.labels);
|
|
757
|
+
if (!identity) continue;
|
|
758
|
+
descriptors.push({
|
|
759
|
+
instanceId: identity.instanceId,
|
|
760
|
+
workerId: identity.workerId,
|
|
761
|
+
unitId: pod.name,
|
|
762
|
+
namespace: pod.namespace,
|
|
763
|
+
observedState: pod.phase === "Failed" || pod.phase === "Succeeded" ? "terminal" : pod.phase === "Pending" ? "pending" : pod.phase === "Running" ? "running" : "unknown"
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return descriptors;
|
|
768
|
+
},
|
|
769
|
+
async adopt(descriptor, observer) {
|
|
770
|
+
const podNamespace = descriptor.namespace ?? namespaceForProcess(descriptor.instanceId);
|
|
771
|
+
const pod = await client.getPod(descriptor.unitId, podNamespace);
|
|
772
|
+
const identity = pod ? parseWorkerUnitIdentity(pod.labels) : null;
|
|
773
|
+
if (!identity || identity.instanceId !== descriptor.instanceId || identity.workerId !== descriptor.workerId) {
|
|
774
|
+
throw new Error(`Cannot adopt worker pod ${descriptor.unitId}: labels changed`);
|
|
775
|
+
}
|
|
776
|
+
if (!pod || pod.phase === "Failed" || pod.phase === "Succeeded") {
|
|
777
|
+
throw new Error(`Cannot adopt worker pod ${descriptor.unitId}: not running`);
|
|
778
|
+
}
|
|
779
|
+
const ref = {
|
|
780
|
+
instanceId: descriptor.instanceId,
|
|
781
|
+
workerId: descriptor.workerId,
|
|
782
|
+
unitId: descriptor.unitId,
|
|
783
|
+
namespace: podNamespace
|
|
784
|
+
};
|
|
785
|
+
if (observer?.observe && pod.pvcName) {
|
|
786
|
+
samplers.get(descriptor.instanceId)?.stop();
|
|
787
|
+
const sampler = sampleKubernetesStartup(client, observer, podNamespace, pod.pvcName);
|
|
788
|
+
sampler.attachPod({ name: ref.unitId, workerId: ref.workerId, instanceId: ref.instanceId });
|
|
789
|
+
samplers.set(descriptor.instanceId, sampler);
|
|
790
|
+
}
|
|
791
|
+
watchPod(ref.unitId, podNamespace);
|
|
792
|
+
return exitNotifier.wrapUnit(ref, unitKey(podNamespace, ref.unitId), {
|
|
793
|
+
replacementHandoff: "stop-before-replacement"
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
const exporter = createHelperProcessStateExporter({
|
|
798
|
+
volume,
|
|
799
|
+
helperRelays,
|
|
800
|
+
async launch(input) {
|
|
801
|
+
const namespace = input.volume.namespace ?? namespaceForProcess(input.instanceId);
|
|
802
|
+
const podName = kubernetesExportHelperPodName(input.instanceId, input.exportId);
|
|
803
|
+
const manifest = buildKubernetesExportHelperPodManifest(
|
|
804
|
+
{
|
|
805
|
+
instanceId: input.instanceId,
|
|
806
|
+
exportId: input.exportId,
|
|
807
|
+
image: exporterImage,
|
|
808
|
+
command: ["node", SESSION_TRANSFER_HELPER_ENTRY_PATH],
|
|
809
|
+
env: exportHelperEnvironment({
|
|
810
|
+
serverUrl: exporterServerUrl,
|
|
811
|
+
exportId: input.exportId,
|
|
812
|
+
credential: input.credential
|
|
813
|
+
}),
|
|
814
|
+
volume: { ...input.volume, mountPath: SESSION_TRANSFER_HELPER_MOUNT_PATH }
|
|
815
|
+
},
|
|
816
|
+
{
|
|
817
|
+
namespace,
|
|
818
|
+
...options.pod ?? {},
|
|
819
|
+
imagePullPolicy: options.exporterImagePullPolicy,
|
|
820
|
+
...serverCaFile ? {
|
|
821
|
+
serverCaConfigMap: {
|
|
822
|
+
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
823
|
+
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
824
|
+
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH
|
|
825
|
+
}
|
|
826
|
+
} : {}
|
|
827
|
+
}
|
|
828
|
+
);
|
|
829
|
+
await client.createPod(manifest);
|
|
830
|
+
const exit = new Promise((resolve) => {
|
|
831
|
+
client.onPodExit(
|
|
832
|
+
podName,
|
|
833
|
+
namespace,
|
|
834
|
+
(info) => resolve({ exitCode: info.exitCode, reason: info.reason })
|
|
835
|
+
);
|
|
836
|
+
});
|
|
837
|
+
return {
|
|
838
|
+
wait: () => exit,
|
|
839
|
+
remove: () => client.deletePod(podName, namespace, { gracePeriodSeconds: 0 })
|
|
840
|
+
};
|
|
841
|
+
},
|
|
842
|
+
async reconcileHelpers() {
|
|
843
|
+
const namespaces = await client.listNamespaces(managedProcessNamespaceLabelSelector());
|
|
844
|
+
for (const namespace of namespaces) {
|
|
845
|
+
const helpers = await client.listPods(namespace.name, managedExportHelperLabelSelector());
|
|
846
|
+
await Promise.all(
|
|
847
|
+
helpers.map(
|
|
848
|
+
(helper) => client.deletePod(helper.name, helper.namespace, { gracePeriodSeconds: 0 })
|
|
849
|
+
)
|
|
850
|
+
);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
let volumePool;
|
|
855
|
+
if (options.preProvision) {
|
|
856
|
+
if (!client.volumePool || !options.volume.storageClassName || !options.serverNamespace) {
|
|
857
|
+
throw new Error("Volume pre-provisioning requires an API, namespace and StorageClass");
|
|
858
|
+
}
|
|
859
|
+
volumePool = createKubernetesVolumePool({
|
|
860
|
+
api: client.volumePool,
|
|
861
|
+
namespace: options.serverNamespace,
|
|
862
|
+
storageClassName: options.volume.storageClassName,
|
|
863
|
+
size: options.volume.size,
|
|
864
|
+
accessModes: options.volume.accessModes,
|
|
865
|
+
...options.preProvision,
|
|
866
|
+
image: exporterImage,
|
|
867
|
+
imagePullPolicy: options.exporterImagePullPolicy,
|
|
868
|
+
imagePullSecrets: (options.pod?.imagePullSecrets ?? []).map(
|
|
869
|
+
(name) => options.imagePullSecretCopies?.find((copy) => copy.targetName === name)?.sourceName ?? name
|
|
870
|
+
),
|
|
871
|
+
nodeSelector: options.pod?.nodeSelector,
|
|
872
|
+
tolerations: options.pod?.tolerations
|
|
873
|
+
});
|
|
874
|
+
}
|
|
875
|
+
return { runner, volume, exporter, volumePool };
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
export {
|
|
879
|
+
createKubernetesWorkerRunner
|
|
880
|
+
};
|
|
881
|
+
//# sourceMappingURL=chunk-ISEDLYKQ.js.map
|