@leitwerk-dev/worker-runners 0.3.1 → 0.4.1
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-YDYI3BD2.js → chunk-FEIZN5GL.js} +53 -9
- package/dist/chunk-FEIZN5GL.js.map +1 -0
- package/dist/{chunk-ZGFD76I6.js → chunk-GF3H4C3J.js} +1 -1
- package/dist/chunk-GF3H4C3J.js.map +1 -0
- package/dist/{chunk-V7TTFLGE.js → chunk-GMZOYHQD.js} +414 -82
- package/dist/chunk-GMZOYHQD.js.map +1 -0
- package/dist/{chunk-ISH4647R.js → chunk-LZOVBKHR.js} +89 -89
- package/dist/chunk-LZOVBKHR.js.map +1 -0
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/{kubernetes-api-client-CEe7pHC4.d.ts → kubernetes-api-client-BMWAgyfW.d.ts} +192 -103
- package/dist/kubernetes-api-client.d.ts +3 -0
- package/dist/kubernetes-api-client.d.ts.map +1 -1
- package/dist/kubernetes-client-public.js +2 -2
- package/dist/kubernetes-http-client.d.ts.map +1 -1
- package/dist/kubernetes-http-client.js +2 -2
- package/dist/kubernetes-image-preload-api.d.ts +69 -0
- package/dist/kubernetes-image-preload-api.d.ts.map +1 -0
- package/dist/kubernetes-image-preload.d.ts +48 -0
- package/dist/kubernetes-image-preload.d.ts.map +1 -0
- package/dist/kubernetes-manifests.d.ts +10 -1
- package/dist/kubernetes-manifests.d.ts.map +1 -1
- package/dist/kubernetes-preparation-api.d.ts +26 -0
- package/dist/kubernetes-preparation-api.d.ts.map +1 -0
- package/dist/kubernetes-preparation.d.ts +21 -0
- package/dist/kubernetes-preparation.d.ts.map +1 -0
- package/dist/kubernetes-public.js +3 -3
- package/dist/kubernetes-volume-pool-api.d.ts +17 -14
- package/dist/kubernetes-volume-pool-api.d.ts.map +1 -1
- package/dist/kubernetes-volume-pool.d.ts +5 -5
- package/dist/kubernetes-volume-pool.d.ts.map +1 -1
- package/dist/kubernetes-volume-retirement.d.ts +20 -0
- package/dist/kubernetes-volume-retirement.d.ts.map +1 -0
- package/dist/kubernetes-worker-runner.d.ts +13 -3
- package/dist/kubernetes-worker-runner.d.ts.map +1 -1
- package/dist/kubernetes-worker-runner.js +3 -3
- package/dist/process-volume-retention.d.ts +4 -0
- package/dist/process-volume-retention.d.ts.map +1 -1
- package/dist/types-public.js +1 -1
- package/dist/types.d.ts +11 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/package.json +5 -5
- package/src/kubernetes-api-client.ts +3 -0
- package/src/kubernetes-http-client.ts +4 -0
- package/src/kubernetes-image-preload-api.ts +96 -0
- package/src/kubernetes-image-preload.ts +278 -0
- package/src/kubernetes-manifests.ts +75 -11
- package/src/kubernetes-preparation-api.ts +77 -0
- package/src/kubernetes-preparation.ts +100 -0
- package/src/kubernetes-volume-pool-api.ts +45 -105
- package/src/kubernetes-volume-pool.ts +28 -58
- package/src/kubernetes-volume-retirement.ts +139 -0
- package/src/kubernetes-worker-runner.ts +78 -28
- package/src/process-volume-retention.ts +27 -4
- package/src/types.ts +15 -1
- package/dist/chunk-ISH4647R.js.map +0 -1
- package/dist/chunk-V7TTFLGE.js.map +0 -1
- package/dist/chunk-YDYI3BD2.js.map +0 -1
- package/dist/chunk-ZGFD76I6.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
WorkerStartDiagnosticError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-GF3H4C3J.js";
|
|
4
4
|
import {
|
|
5
5
|
createHelperProcessStateExporter,
|
|
6
6
|
exportHelperEnvironment
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
kubernetesProcessPvcName,
|
|
28
28
|
redactKubernetesDiagnostic,
|
|
29
29
|
volumeRefFromPvc
|
|
30
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-FEIZN5GL.js";
|
|
31
31
|
import {
|
|
32
32
|
WORKER_LABEL_COMPONENT,
|
|
33
33
|
WORKER_LABEL_INSTANCE_ID,
|
|
@@ -42,6 +42,266 @@ import {
|
|
|
42
42
|
// src/kubernetes-worker-runner.ts
|
|
43
43
|
import { readFileSync } from "fs";
|
|
44
44
|
|
|
45
|
+
// src/kubernetes-image-preload.ts
|
|
46
|
+
import { createHash } from "crypto";
|
|
47
|
+
|
|
48
|
+
// src/kubernetes-preparation.ts
|
|
49
|
+
import { setTimeout as delay } from "timers/promises";
|
|
50
|
+
function buildPreparationPodSpec(options, volumeMounts) {
|
|
51
|
+
return {
|
|
52
|
+
restartPolicy: "Never",
|
|
53
|
+
serviceAccountName: "default",
|
|
54
|
+
automountServiceAccountToken: false,
|
|
55
|
+
nodeSelector: options.nodeSelector ?? {},
|
|
56
|
+
tolerations: options.tolerations ?? [],
|
|
57
|
+
imagePullSecrets: (options.imagePullSecrets ?? []).map((name) => ({ name })),
|
|
58
|
+
containers: [
|
|
59
|
+
{
|
|
60
|
+
name: "prepare",
|
|
61
|
+
image: options.image,
|
|
62
|
+
imagePullPolicy: options.imagePullPolicy ?? "IfNotPresent",
|
|
63
|
+
command: ["node", "-e", "process.exit(0)"],
|
|
64
|
+
resources: {
|
|
65
|
+
requests: { cpu: "1m", memory: "32Mi" },
|
|
66
|
+
limits: { cpu: "100m", memory: "64Mi" }
|
|
67
|
+
},
|
|
68
|
+
securityContext: {
|
|
69
|
+
allowPrivilegeEscalation: false,
|
|
70
|
+
readOnlyRootFilesystem: true,
|
|
71
|
+
capabilities: { drop: ["ALL"] }
|
|
72
|
+
},
|
|
73
|
+
...volumeMounts ? { volumeMounts } : {}
|
|
74
|
+
}
|
|
75
|
+
]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function createPreparationController(reconcile, intervalMs, onError, errorIntervalMs = intervalMs) {
|
|
79
|
+
let controller;
|
|
80
|
+
let running;
|
|
81
|
+
return {
|
|
82
|
+
start() {
|
|
83
|
+
if (controller) return;
|
|
84
|
+
controller = new AbortController();
|
|
85
|
+
const { signal } = controller;
|
|
86
|
+
running = (async () => {
|
|
87
|
+
while (!signal.aborted) {
|
|
88
|
+
let interval = intervalMs;
|
|
89
|
+
try {
|
|
90
|
+
await reconcile(signal);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
interval = errorIntervalMs;
|
|
93
|
+
if (!signal.aborted) onError?.(error);
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
await delay(interval, void 0, { signal });
|
|
97
|
+
} catch {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
})();
|
|
102
|
+
},
|
|
103
|
+
async stop() {
|
|
104
|
+
controller?.abort();
|
|
105
|
+
await running;
|
|
106
|
+
controller = void 0;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function createKubernetesPreparation(controllers) {
|
|
111
|
+
return {
|
|
112
|
+
/** @internal */
|
|
113
|
+
start() {
|
|
114
|
+
for (const controller of controllers) controller.start();
|
|
115
|
+
},
|
|
116
|
+
/** @internal */
|
|
117
|
+
async stop() {
|
|
118
|
+
await Promise.allSettled(controllers.map((controller) => controller.stop()));
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/kubernetes-image-preload.ts
|
|
124
|
+
var IMAGE_PRELOAD_LABEL = "leitwerk.dev/image-preload";
|
|
125
|
+
var NODE_UID = "leitwerk.dev/preparation-node-uid";
|
|
126
|
+
var NODE_NAME = "leitwerk.dev/preparation-node-name";
|
|
127
|
+
var IMAGE = "leitwerk.dev/preparation-image";
|
|
128
|
+
var INTERVAL = 3e4;
|
|
129
|
+
var hash = (value) => createHash("sha256").update(value).digest("hex");
|
|
130
|
+
function immutableWorkerImage(image) {
|
|
131
|
+
const match = /^([a-zA-Z0-9][a-zA-Z0-9._:/-]*)@(sha256:[0-9a-f]{64})$/.exec(image);
|
|
132
|
+
if (!match) throw new Error("Image preloading requires digest-qualified worker images");
|
|
133
|
+
return `${match[1].replace(/:[^/:]+$/, "")}@${match[2]}`;
|
|
134
|
+
}
|
|
135
|
+
function eligiblePreparationNode(node, selector, tolerations) {
|
|
136
|
+
if (!node.metadata.uid || node.metadata.deletionTimestamp || node.spec?.unschedulable)
|
|
137
|
+
return false;
|
|
138
|
+
if (!node.status?.conditions?.some(
|
|
139
|
+
(condition) => condition.type === "Ready" && condition.status === "True"
|
|
140
|
+
))
|
|
141
|
+
return false;
|
|
142
|
+
if (!Object.entries(selector).every(([key, value]) => node.metadata.labels?.[key] === value))
|
|
143
|
+
return false;
|
|
144
|
+
return (node.spec?.taints ?? []).every((taint) => {
|
|
145
|
+
if (!["NoSchedule", "NoExecute"].includes(taint.effect)) return true;
|
|
146
|
+
return tolerations.some((value) => {
|
|
147
|
+
if (!value || typeof value !== "object") return false;
|
|
148
|
+
const tolerance = value;
|
|
149
|
+
if (tolerance.effect && tolerance.effect !== taint.effect) return false;
|
|
150
|
+
if (tolerance.operator === "Exists") return !tolerance.key || tolerance.key === taint.key;
|
|
151
|
+
return (!tolerance.operator || tolerance.operator === "Equal") && tolerance.key === taint.key && (tolerance.value ?? "") === (taint.value ?? "");
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function createKubernetesImagePreload(options) {
|
|
156
|
+
const { api, namespace } = options;
|
|
157
|
+
const nodeSelectors = [options.nodeSelector ?? {}, ...options.additionalNodeSelectors ?? []];
|
|
158
|
+
const selectorForNode = (node) => nodeSelectors.find(
|
|
159
|
+
(selector2) => eligiblePreparationNode(node, selector2, options.tolerations ?? [])
|
|
160
|
+
);
|
|
161
|
+
const images = [...new Set(options.images.map(immutableWorkerImage))];
|
|
162
|
+
const preloadId = hash(namespace).slice(0, 16);
|
|
163
|
+
const selector = `${IMAGE_PRELOAD_LABEL}=${preloadId}`;
|
|
164
|
+
const labels = {
|
|
165
|
+
"leitwerk.dev/managed-by": "leitwerk",
|
|
166
|
+
"leitwerk.dev/component": "image-preparation",
|
|
167
|
+
[IMAGE_PRELOAD_LABEL]: preloadId
|
|
168
|
+
};
|
|
169
|
+
const podName = (uid, image) => `lw-image-${hash(`${uid}/${image}`).slice(0, 40)}`;
|
|
170
|
+
const reported = /* @__PURE__ */ new Map();
|
|
171
|
+
const failedSince = /* @__PURE__ */ new Map();
|
|
172
|
+
function owned(pod) {
|
|
173
|
+
const annotations = pod.metadata.annotations ?? {};
|
|
174
|
+
const uid = annotations[NODE_UID];
|
|
175
|
+
const name = annotations[NODE_NAME];
|
|
176
|
+
const image = annotations[IMAGE];
|
|
177
|
+
const containers = pod.spec.containers;
|
|
178
|
+
return pod.metadata.namespace === namespace && !!pod.metadata.uid && !!uid && !!name && !!image && Object.entries(labels).every(([key, value]) => pod.metadata.labels?.[key] === value) && pod.metadata.name === podName(uid, image) && pod.metadata.ownerReferences?.some(
|
|
179
|
+
(owner) => owner.apiVersion === "v1" && owner.kind === "Node" && owner.name === name && owner.uid === uid
|
|
180
|
+
) && pod.spec.nodeName === name && pod.spec.automountServiceAccountToken === false && containers?.length === 1 && containers[0].image === image && JSON.stringify(containers[0].command) === JSON.stringify(["node", "-e", "process.exit(0)"]);
|
|
181
|
+
}
|
|
182
|
+
function report(pod, outcome, reason) {
|
|
183
|
+
const uid = pod.metadata.uid ?? pod.metadata.name;
|
|
184
|
+
const state = `${outcome}/${reason ?? ""}`;
|
|
185
|
+
if (reported.get(uid) === state) return;
|
|
186
|
+
reported.set(uid, state);
|
|
187
|
+
options.onEvent?.({
|
|
188
|
+
component: "image-preparation",
|
|
189
|
+
outcome,
|
|
190
|
+
reason,
|
|
191
|
+
pod: pod.metadata.name,
|
|
192
|
+
node: pod.metadata.annotations?.[NODE_NAME],
|
|
193
|
+
nodeUid: pod.metadata.annotations?.[NODE_UID],
|
|
194
|
+
image: pod.metadata.annotations?.[IMAGE]
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
function manifest(node, image, nodeSelector) {
|
|
198
|
+
const uid = node.metadata.uid ?? "";
|
|
199
|
+
return {
|
|
200
|
+
apiVersion: "v1",
|
|
201
|
+
kind: "Pod",
|
|
202
|
+
metadata: {
|
|
203
|
+
name: podName(uid, image),
|
|
204
|
+
namespace,
|
|
205
|
+
labels,
|
|
206
|
+
annotations: { [NODE_UID]: uid, [NODE_NAME]: node.metadata.name, [IMAGE]: image },
|
|
207
|
+
ownerReferences: [{ apiVersion: "v1", kind: "Node", name: node.metadata.name, uid }]
|
|
208
|
+
},
|
|
209
|
+
spec: {
|
|
210
|
+
...buildPreparationPodSpec({
|
|
211
|
+
...options,
|
|
212
|
+
image,
|
|
213
|
+
imagePullPolicy: "IfNotPresent",
|
|
214
|
+
nodeSelector: { ...nodeSelector }
|
|
215
|
+
}),
|
|
216
|
+
nodeName: node.metadata.name,
|
|
217
|
+
securityContext: {
|
|
218
|
+
runAsNonRoot: true,
|
|
219
|
+
runAsUser: 1e3,
|
|
220
|
+
runAsGroup: 1e3,
|
|
221
|
+
seccompProfile: { type: "RuntimeDefault" }
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async function reconcile(signal) {
|
|
227
|
+
const nodes = await api.listNodes(signal);
|
|
228
|
+
const desired = /* @__PURE__ */ new Map();
|
|
229
|
+
for (const node of nodes) {
|
|
230
|
+
const nodeSelector = selectorForNode(node);
|
|
231
|
+
if (!nodeSelector) continue;
|
|
232
|
+
for (const image of images) {
|
|
233
|
+
const pod = manifest(node, image, nodeSelector);
|
|
234
|
+
desired.set(pod.metadata.name, pod);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const pods = await api.listPods(namespace, selector, signal);
|
|
238
|
+
for (const pod of pods) {
|
|
239
|
+
if (desired.has(pod.metadata.name) || !owned(pod) || pod.metadata.deletionTimestamp) continue;
|
|
240
|
+
try {
|
|
241
|
+
await api.deletePod(namespace, pod, signal);
|
|
242
|
+
} catch {
|
|
243
|
+
if (!signal.aborted) report(pod, "failed", "cleanup_failed");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const liveUids = new Set(pods.map((pod) => pod.metadata.uid ?? ""));
|
|
247
|
+
for (const uid of reported.keys())
|
|
248
|
+
if (!liveUids.has(uid)) {
|
|
249
|
+
reported.delete(uid);
|
|
250
|
+
failedSince.delete(uid);
|
|
251
|
+
}
|
|
252
|
+
for (const expected of desired.values()) {
|
|
253
|
+
try {
|
|
254
|
+
const pod = pods.find((value) => value.metadata.name === expected.metadata.name) ?? await api.getPod(namespace, expected.metadata.name, signal);
|
|
255
|
+
if (!pod) {
|
|
256
|
+
await api.createPod(namespace, expected, signal);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (!owned(pod)) {
|
|
260
|
+
report(expected, "failed", "ownership_mismatch");
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (pod.metadata.deletionTimestamp) continue;
|
|
264
|
+
if (pod.status?.phase === "Succeeded") {
|
|
265
|
+
report(pod, "succeeded");
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (pod.status?.phase === "Failed") {
|
|
269
|
+
report(pod, "failed", "pod_failed");
|
|
270
|
+
const uid = pod.metadata.uid ?? "";
|
|
271
|
+
const finished = Date.parse(
|
|
272
|
+
pod.status.containerStatuses?.[0]?.state?.terminated?.finishedAt ?? ""
|
|
273
|
+
);
|
|
274
|
+
const since = failedSince.get(uid) ?? (Number.isFinite(finished) ? finished : Date.now());
|
|
275
|
+
failedSince.set(uid, since);
|
|
276
|
+
if (Date.now() - since >= INTERVAL) {
|
|
277
|
+
await api.deletePod(namespace, pod, signal);
|
|
278
|
+
await api.createPod(namespace, expected, signal);
|
|
279
|
+
}
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
const reason = pod.status?.containerStatuses?.[0]?.state?.waiting?.reason;
|
|
283
|
+
if (reason === "ErrImagePull" || reason === "ImagePullBackOff" || reason === "InvalidImageName")
|
|
284
|
+
report(pod, "failed", reason);
|
|
285
|
+
} catch {
|
|
286
|
+
if (!signal.aborted) report(expected, "failed", "api_error");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
/** @internal */
|
|
292
|
+
reconcile,
|
|
293
|
+
...createPreparationController(
|
|
294
|
+
reconcile,
|
|
295
|
+
INTERVAL,
|
|
296
|
+
() => options.onEvent?.({
|
|
297
|
+
component: "image-preparation",
|
|
298
|
+
outcome: "failed",
|
|
299
|
+
reason: "discovery_failed"
|
|
300
|
+
})
|
|
301
|
+
)
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
45
305
|
// src/kubernetes-startup-sampler.ts
|
|
46
306
|
function startupReceipt(observer, milestone, metadata = {}) {
|
|
47
307
|
try {
|
|
@@ -198,8 +458,7 @@ function sourcePrecision(timestamp) {
|
|
|
198
458
|
}
|
|
199
459
|
|
|
200
460
|
// src/kubernetes-volume-pool.ts
|
|
201
|
-
import { createHash, randomUUID } from "crypto";
|
|
202
|
-
import { setTimeout as delay } from "timers/promises";
|
|
461
|
+
import { createHash as createHash2, randomUUID } from "crypto";
|
|
203
462
|
var POOL_LABEL = "leitwerk.dev/volume-pool";
|
|
204
463
|
var CLAIM_UID = "leitwerk.dev/preparation-claim-uid";
|
|
205
464
|
var CLAIM_NAME = "leitwerk.dev/preparation-claim-name";
|
|
@@ -212,15 +471,13 @@ function createKubernetesVolumePool(options) {
|
|
|
212
471
|
const { api, namespace } = options;
|
|
213
472
|
if (!Number.isSafeInteger(options.count) || options.count < 0)
|
|
214
473
|
throw new Error("Invalid volume pool count");
|
|
215
|
-
const poolId =
|
|
474
|
+
const poolId = createHash2("sha256").update(`${namespace}/${options.storageClassName}`).digest("hex").slice(0, 16);
|
|
216
475
|
const selector = `${POOL_LABEL}=${poolId}`;
|
|
217
476
|
const labels = {
|
|
218
477
|
"leitwerk.dev/managed-by": "leitwerk",
|
|
219
478
|
[POOL_LABEL]: poolId,
|
|
220
479
|
"leitwerk.dev/component": "volume-preparation"
|
|
221
480
|
};
|
|
222
|
-
let controller;
|
|
223
|
-
let running;
|
|
224
481
|
function preparationPod(claim) {
|
|
225
482
|
return {
|
|
226
483
|
apiVersion: "v1",
|
|
@@ -239,30 +496,8 @@ function createKubernetesVolumePool(options) {
|
|
|
239
496
|
]
|
|
240
497
|
},
|
|
241
498
|
spec: {
|
|
242
|
-
|
|
243
|
-
|
|
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
|
-
],
|
|
499
|
+
...buildPreparationPodSpec(options, [{ name: "volume", mountPath: "/volume" }]),
|
|
500
|
+
activeDeadlineSeconds: 900,
|
|
266
501
|
volumes: [{ name: "volume", persistentVolumeClaim: { claimName: claim.metadata.name } }]
|
|
267
502
|
}
|
|
268
503
|
};
|
|
@@ -346,6 +581,7 @@ function createKubernetesVolumePool(options) {
|
|
|
346
581
|
for (const pv of volumes) {
|
|
347
582
|
if (await advanceVolume(pv, signal)) active.add(pv.metadata.annotations?.[CLAIM_NAME] ?? "");
|
|
348
583
|
}
|
|
584
|
+
const failures = [];
|
|
349
585
|
const claims = await api.list("persistentvolumeclaims", namespace, selector, signal);
|
|
350
586
|
for (const claim of claims) {
|
|
351
587
|
active.add(claim.metadata.name);
|
|
@@ -360,8 +596,12 @@ function createKubernetesVolumePool(options) {
|
|
|
360
596
|
}
|
|
361
597
|
if (pod.metadata.labels?.[POOL_LABEL] !== poolId || !pod.metadata.ownerReferences?.some((owner) => owner.uid === claim.metadata.uid))
|
|
362
598
|
throw new Error("Preparation Pod ownership mismatch");
|
|
363
|
-
|
|
364
|
-
|
|
599
|
+
const created = Date.parse(claim.metadata.creationTimestamp ?? "");
|
|
600
|
+
const ageSeconds = Number.isFinite(created) ? Math.max(0, Math.floor(((options.now?.() ?? Date.now()) - created) / 1e3)) : void 0;
|
|
601
|
+
if (pod.status?.phase === "Failed" || pod.status?.phase !== "Succeeded" && ageSeconds !== void 0 && ageSeconds >= 900) {
|
|
602
|
+
failures.push(
|
|
603
|
+
`${namespace}/${pod.metadata.name}: age=${ageSeconds ?? "unknown"}s phase=${pod.status?.phase ?? "unknown"} reason=${pod.status?.reason ?? pod.status?.conditions?.find((condition) => condition.status === "False")?.reason ?? "inspect Pod events and CSI/Longhorn scheduling conditions"}`
|
|
604
|
+
);
|
|
365
605
|
continue;
|
|
366
606
|
}
|
|
367
607
|
if (pod.status?.phase !== "Succeeded" || !claim.spec.volumeName) continue;
|
|
@@ -389,6 +629,11 @@ function createKubernetesVolumePool(options) {
|
|
|
389
629
|
[CLAIM_NAME]: claim.metadata.name,
|
|
390
630
|
[POLICY]: policy,
|
|
391
631
|
[STATE]: "preparing",
|
|
632
|
+
"leitwerk.dev/preparation-pod-uid": pod.metadata.uid ?? "",
|
|
633
|
+
"leitwerk.dev/preparation-image": options.image,
|
|
634
|
+
"leitwerk.dev/preparation-completed-at": new Date(
|
|
635
|
+
options.now?.() ?? Date.now()
|
|
636
|
+
).toISOString(),
|
|
392
637
|
[TARGET_CLASS]: options.storageClassName,
|
|
393
638
|
[SIZE]: claim.metadata.annotations?.[SIZE],
|
|
394
639
|
[MODES]: claim.metadata.annotations?.[MODES]
|
|
@@ -471,43 +716,94 @@ function createKubernetesVolumePool(options) {
|
|
|
471
716
|
);
|
|
472
717
|
}
|
|
473
718
|
}
|
|
719
|
+
if (failures.length) throw new Error(`Volume pool preparation blocked: ${failures.join("; ")}`);
|
|
474
720
|
}
|
|
475
721
|
return {
|
|
476
722
|
/** @internal */
|
|
477
723
|
poolId,
|
|
478
724
|
/** @internal */
|
|
479
725
|
reconcile,
|
|
480
|
-
|
|
481
|
-
start() {
|
|
482
|
-
if (controller) return;
|
|
483
|
-
controller = new AbortController();
|
|
484
|
-
const { signal } = controller;
|
|
485
|
-
running = (async () => {
|
|
486
|
-
while (!signal.aborted) {
|
|
487
|
-
let interval = 2e3;
|
|
488
|
-
try {
|
|
489
|
-
await reconcile(signal);
|
|
490
|
-
} catch {
|
|
491
|
-
interval = 3e4;
|
|
492
|
-
if (!signal.aborted) options.onError?.();
|
|
493
|
-
}
|
|
494
|
-
try {
|
|
495
|
-
await delay(interval, void 0, { signal });
|
|
496
|
-
} catch {
|
|
497
|
-
break;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
})();
|
|
501
|
-
},
|
|
502
|
-
/** @internal */
|
|
503
|
-
async stop() {
|
|
504
|
-
controller?.abort();
|
|
505
|
-
await running;
|
|
506
|
-
controller = void 0;
|
|
507
|
-
}
|
|
726
|
+
...createPreparationController(reconcile, 2e3, options.onError, 3e4)
|
|
508
727
|
};
|
|
509
728
|
}
|
|
510
729
|
|
|
730
|
+
// src/kubernetes-volume-retirement.ts
|
|
731
|
+
async function retireKubernetesProcessVolume(input) {
|
|
732
|
+
const { api, namespace, name, instanceId, signal } = input;
|
|
733
|
+
const reclamation = input.reclaimBackingStorage;
|
|
734
|
+
if (typeof reclamation === "object" && (!["completed", "aborted"].includes(reclamation.lifecycleStatus) || !Number.isFinite(Date.parse(reclamation.closedAt)) || Date.parse(reclamation.closedAt) > Date.now()))
|
|
735
|
+
throw new Error("Backing-storage reclamation requires a closed terminal process");
|
|
736
|
+
if (reclamation === "process-deleted" && input.reason !== "process-deleted")
|
|
737
|
+
throw new Error("Explicit storage deletion requires a process deletion request");
|
|
738
|
+
const pvc = await api.get("persistentvolumeclaims", namespace, name, signal);
|
|
739
|
+
if (!pvc) return;
|
|
740
|
+
const labels = pvc.metadata.labels;
|
|
741
|
+
if (!pvc.metadata.uid || labels?.["leitwerk.dev/managed-by"] !== "leitwerk" || labels["leitwerk.dev/component"] !== "process-volume" || labels["leitwerk.dev/instance-id"] !== instanceId || pvc.metadata.ownerReferences?.length)
|
|
742
|
+
throw new Error("Cannot retire a foreign or ambiguous process PVC");
|
|
743
|
+
if (reclamation && !pvc.spec.volumeName)
|
|
744
|
+
throw new Error(
|
|
745
|
+
"PVC is still provisioning; preserve its identity until the backing PV can be audited"
|
|
746
|
+
);
|
|
747
|
+
const pods = await api.list("pods", namespace, "", signal);
|
|
748
|
+
if (pods.some(
|
|
749
|
+
(pod) => !["Succeeded", "Failed"].includes(pod.status?.phase ?? "") && Array.isArray(pod.spec.volumes) && pod.spec.volumes.some(
|
|
750
|
+
(volume) => volume.persistentVolumeClaim?.claimName === name
|
|
751
|
+
)
|
|
752
|
+
))
|
|
753
|
+
throw new Error("Cannot retire a process volume referenced by a live Pod");
|
|
754
|
+
if (pvc.spec.volumeName) {
|
|
755
|
+
const pv = await api.get("persistentvolumes", "", pvc.spec.volumeName, signal);
|
|
756
|
+
if (!pv || pv.metadata.labels?.["leitwerk.dev/managed-by"] !== void 0 && pv.metadata.labels["leitwerk.dev/managed-by"] !== "leitwerk" || pv.metadata.labels?.["leitwerk.dev/instance-id"] !== void 0 && pv.metadata.labels["leitwerk.dev/instance-id"] !== instanceId || pv.metadata.ownerReferences?.length || pv.metadata.deletionTimestamp || pv.spec.claimRef?.uid !== pvc.metadata.uid || pv.spec.claimRef?.name !== name || pv.spec.claimRef?.namespace !== namespace)
|
|
757
|
+
throw new Error("Cannot retire a volume with ambiguous claim ownership");
|
|
758
|
+
const csi = pv.spec.csi;
|
|
759
|
+
if (reclamation && (!csi?.driver || !csi.volumeHandle || pv.metadata.annotations?.["pv.kubernetes.io/provisioned-by"] !== csi.driver || !["Retain", "Delete"].includes(pv.spec.persistentVolumeReclaimPolicy ?? "")))
|
|
760
|
+
throw new Error(
|
|
761
|
+
"Backing-storage reclamation requires an owned dynamically provisioned CSI volume"
|
|
762
|
+
);
|
|
763
|
+
const volumeLabels = {
|
|
764
|
+
...pv.metadata.labels,
|
|
765
|
+
"leitwerk.dev/managed-by": "leitwerk",
|
|
766
|
+
"leitwerk.dev/component": "process-volume",
|
|
767
|
+
"leitwerk.dev/instance-id": instanceId,
|
|
768
|
+
...reclamation ? { "leitwerk.dev/reclamation": "terminal" } : {}
|
|
769
|
+
};
|
|
770
|
+
delete volumeLabels[POOL_LABEL];
|
|
771
|
+
await api.patchVolume(
|
|
772
|
+
pv,
|
|
773
|
+
[
|
|
774
|
+
{
|
|
775
|
+
op: "add",
|
|
776
|
+
path: "/metadata/labels",
|
|
777
|
+
value: volumeLabels
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
op: "add",
|
|
781
|
+
path: "/metadata/annotations",
|
|
782
|
+
value: {
|
|
783
|
+
...pv.metadata.annotations,
|
|
784
|
+
"leitwerk.dev/retirement": JSON.stringify({
|
|
785
|
+
instanceId,
|
|
786
|
+
namespace,
|
|
787
|
+
claimName: name,
|
|
788
|
+
claimUid: pvc.metadata.uid,
|
|
789
|
+
volumeUid: pv.metadata.uid,
|
|
790
|
+
reason: input.reason ?? "retention-expired",
|
|
791
|
+
...reclamation ? {
|
|
792
|
+
reclamation: typeof reclamation === "object" ? { policy: "terminal-process", ...reclamation } : { policy: "process-deletion" }
|
|
793
|
+
} : {}
|
|
794
|
+
})
|
|
795
|
+
}
|
|
796
|
+
},
|
|
797
|
+
// Arm CSI reclamation before deleting the claim. A crash after this
|
|
798
|
+
// patch is safe: retry either repeats it or CSI completes independently.
|
|
799
|
+
...reclamation ? [{ op: "add", path: "/spec/persistentVolumeReclaimPolicy", value: "Delete" }] : []
|
|
800
|
+
],
|
|
801
|
+
signal
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
await api.delete("persistentvolumeclaims", namespace, pvc, signal);
|
|
805
|
+
}
|
|
806
|
+
|
|
511
807
|
// src/kubernetes-worker-runner.ts
|
|
512
808
|
var KUBERNETES_START_DIAGNOSTIC_MAX_LENGTH = 2048;
|
|
513
809
|
function boundedKubernetesStartDiagnostic(error, sensitiveValues) {
|
|
@@ -526,6 +822,13 @@ function createKubernetesWorkerRunner(options) {
|
|
|
526
822
|
const exporterImage = options.exporterImage;
|
|
527
823
|
const helperRelays = options.helperRelays;
|
|
528
824
|
const serverCaFile = options.serverCaFile?.trim();
|
|
825
|
+
const serverCaConfig = serverCaFile ? {
|
|
826
|
+
serverCaConfigMap: {
|
|
827
|
+
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
828
|
+
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
829
|
+
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH
|
|
830
|
+
}
|
|
831
|
+
} : {};
|
|
529
832
|
const namespaceForProcess = (instanceId) => kubernetesProcessNamespaceName(instanceId, options.processNamespacePrefix);
|
|
530
833
|
const delay2 = options.delay ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
531
834
|
const podDisappearanceTimeoutMs = options.podDisappearanceTimeoutMs ?? 3e4;
|
|
@@ -536,7 +839,7 @@ function createKubernetesWorkerRunner(options) {
|
|
|
536
839
|
const namespaceManifest = buildKubernetesProcessNamespaceManifest({
|
|
537
840
|
instanceId,
|
|
538
841
|
processNamespacePrefix: options.processNamespacePrefix,
|
|
539
|
-
...requirements?.docker && options.docker?.
|
|
842
|
+
...requirements?.docker && options.docker?.isolation ? { extraLabels: { "pod-security.kubernetes.io/enforce": "privileged" } } : {}
|
|
540
843
|
});
|
|
541
844
|
if (requirements?.docker && options.docker) {
|
|
542
845
|
if (!client.getPersistentVolumeClaim)
|
|
@@ -623,7 +926,20 @@ function createKubernetesWorkerRunner(options) {
|
|
|
623
926
|
namespace: manifest.metadata.namespace
|
|
624
927
|
});
|
|
625
928
|
},
|
|
626
|
-
async release(instanceId) {
|
|
929
|
+
async release(instanceId, releaseOptions) {
|
|
930
|
+
if (client.volumePool) {
|
|
931
|
+
await retireKubernetesProcessVolume({
|
|
932
|
+
api: client.volumePool,
|
|
933
|
+
namespace: namespaceForProcess(instanceId),
|
|
934
|
+
name: kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
935
|
+
instanceId,
|
|
936
|
+
signal: AbortSignal.timeout(3e4),
|
|
937
|
+
reclaimBackingStorage: releaseOptions?.reclaimBackingStorage
|
|
938
|
+
});
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
if (releaseOptions?.reclaimBackingStorage)
|
|
942
|
+
throw new Error("Backing-storage reclamation requires Kubernetes volume inspection");
|
|
627
943
|
await client.deletePersistentVolumeClaim(
|
|
628
944
|
kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
629
945
|
namespaceForProcess(instanceId)
|
|
@@ -632,6 +948,17 @@ function createKubernetesWorkerRunner(options) {
|
|
|
632
948
|
async deleteProcessResources(instanceId) {
|
|
633
949
|
samplers.get(instanceId)?.stop();
|
|
634
950
|
samplers.delete(instanceId);
|
|
951
|
+
if (client.volumePool) {
|
|
952
|
+
await retireKubernetesProcessVolume({
|
|
953
|
+
api: client.volumePool,
|
|
954
|
+
namespace: namespaceForProcess(instanceId),
|
|
955
|
+
name: kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
956
|
+
instanceId,
|
|
957
|
+
reason: "process-deleted",
|
|
958
|
+
reclaimBackingStorage: options.reclaimOnClose ? "process-deleted" : void 0,
|
|
959
|
+
signal: AbortSignal.timeout(3e4)
|
|
960
|
+
});
|
|
961
|
+
}
|
|
635
962
|
await client.deleteNamespace(namespaceForProcess(instanceId));
|
|
636
963
|
}
|
|
637
964
|
};
|
|
@@ -714,13 +1041,7 @@ function createKubernetesWorkerRunner(options) {
|
|
|
714
1041
|
namespace: processNamespace,
|
|
715
1042
|
...options.pod ?? {},
|
|
716
1043
|
...input.docker && options.docker ? { docker: options.docker } : {},
|
|
717
|
-
...
|
|
718
|
-
serverCaConfigMap: {
|
|
719
|
-
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
720
|
-
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
721
|
-
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH
|
|
722
|
-
}
|
|
723
|
-
} : {}
|
|
1044
|
+
...serverCaConfig
|
|
724
1045
|
});
|
|
725
1046
|
observer?.report("allocating_runtime");
|
|
726
1047
|
try {
|
|
@@ -835,13 +1156,7 @@ function createKubernetesWorkerRunner(options) {
|
|
|
835
1156
|
namespace,
|
|
836
1157
|
...options.pod ?? {},
|
|
837
1158
|
imagePullPolicy: options.exporterImagePullPolicy,
|
|
838
|
-
...
|
|
839
|
-
serverCaConfigMap: {
|
|
840
|
-
name: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_NAME,
|
|
841
|
-
key: KUBERNETES_WORKER_SERVER_CA_CONFIG_MAP_KEY,
|
|
842
|
-
mountPath: KUBERNETES_WORKER_SERVER_CA_MOUNT_PATH
|
|
843
|
-
}
|
|
844
|
-
} : {}
|
|
1159
|
+
...serverCaConfig
|
|
845
1160
|
}
|
|
846
1161
|
);
|
|
847
1162
|
await client.createPod(manifest);
|
|
@@ -869,6 +1184,9 @@ function createKubernetesWorkerRunner(options) {
|
|
|
869
1184
|
}
|
|
870
1185
|
}
|
|
871
1186
|
});
|
|
1187
|
+
const preparationPullSecrets = (options.pod?.imagePullSecrets ?? []).map(
|
|
1188
|
+
(name) => options.imagePullSecretCopies?.find((copy) => copy.targetName === name)?.sourceName ?? name
|
|
1189
|
+
);
|
|
872
1190
|
let volumePool;
|
|
873
1191
|
if (options.preProvision) {
|
|
874
1192
|
if (!client.volumePool || !options.volume.storageClassName || !options.serverNamespace) {
|
|
@@ -883,17 +1201,31 @@ function createKubernetesWorkerRunner(options) {
|
|
|
883
1201
|
...options.preProvision,
|
|
884
1202
|
image: exporterImage,
|
|
885
1203
|
imagePullPolicy: options.exporterImagePullPolicy,
|
|
886
|
-
imagePullSecrets:
|
|
887
|
-
|
|
888
|
-
|
|
1204
|
+
imagePullSecrets: preparationPullSecrets,
|
|
1205
|
+
nodeSelector: options.pod?.nodeSelector,
|
|
1206
|
+
tolerations: options.pod?.tolerations
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
let imagePreload;
|
|
1210
|
+
if (options.imagePreload) {
|
|
1211
|
+
if (!client.imagePreload || !options.serverNamespace)
|
|
1212
|
+
throw new Error("Image preloading requires an API and server namespace");
|
|
1213
|
+
imagePreload = createKubernetesImagePreload({
|
|
1214
|
+
api: client.imagePreload,
|
|
1215
|
+
namespace: options.serverNamespace,
|
|
1216
|
+
...options.imagePreload,
|
|
1217
|
+
imagePullSecrets: preparationPullSecrets,
|
|
889
1218
|
nodeSelector: options.pod?.nodeSelector,
|
|
1219
|
+
additionalNodeSelectors: options.docker ? [{ ...options.pod?.nodeSelector, ...options.docker.nodeSelector }] : void 0,
|
|
890
1220
|
tolerations: options.pod?.tolerations
|
|
891
1221
|
});
|
|
892
1222
|
}
|
|
893
|
-
|
|
1223
|
+
const controllers = [volumePool, imagePreload].filter((value) => value !== void 0);
|
|
1224
|
+
const preparation = controllers.length ? createKubernetesPreparation(controllers) : void 0;
|
|
1225
|
+
return { runner, volume, exporter, preparation };
|
|
894
1226
|
}
|
|
895
1227
|
|
|
896
1228
|
export {
|
|
897
1229
|
createKubernetesWorkerRunner
|
|
898
1230
|
};
|
|
899
|
-
//# sourceMappingURL=chunk-
|
|
1231
|
+
//# sourceMappingURL=chunk-GMZOYHQD.js.map
|