Build pods: classify pod ends (disruption, node loss, own fault), pending kinds and the disruption recreate cap

This commit is contained in:
aiongg committed 2026-11-22 20:07:38 +00:00
1 parent df3489a8a8
commit 90b20faa09
2 files changed
+86

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
// Pure rules for a build pod that ended or never started: whether the cluster took it away (disruption, node loss),
// whether the task caused it, and what to do with a pod no node takes. The launcher applies them.
import type { KPod } from "./kube-api.ts";
import { PENDING_LIMIT_MS } from "./pods.ts";
export type PodEnd = "disruption" | "node-lost" | "own-fault" | "ok";
export type PendingKind = "capacity-wait" | "replace" | "wait";
/** Disruptions of one task that are recreated on the same volume; further ones count as failures. */
export const MAX_DISRUPTIONS = 3;
const DISRUPTION_REASONS = new Set(["PreemptionByScheduler", "TerminationByKubelet", "EvictionByEvictionAPI", "DeletionByTaintManager"]);
/**
* `pod` is null when it vanished. `nodeReady` is the state of its node: true Ready, false NotReady, null missing or unknown.
*/
export function classifyPodEnd(pod: KPod | null, nodeReady: boolean | null): PodEnd {
if (!pod) return nodeReady === true ? "disruption" : "node-lost";
const st = pod.status;
if (st?.conditions?.some((c) => c.type === "DisruptionTarget" && c.status === "True" && DISRUPTION_REASONS.has(c.reason ?? ""))) return "disruption";
if (st?.reason === "Evicted") return "disruption";
const phase = st?.phase;
if (phase === "Succeeded") return "ok";
const statuses = [...(st?.initContainerStatuses ?? []), ...(st?.containerStatuses ?? [])];
const ownFault = st?.reason === "DeadlineExceeded" || statuses.some((s) => s.state?.terminated?.reason === "OOMKilled" || (s.state?.terminated && s.state.terminated.exitCode !== 0));
if (ownFault) return "own-fault";
if (phase === "Failed") return nodeReady === true ? "own-fault" : "node-lost";
if (phase === "Unknown") return nodeReady === true ? "ok" : "node-lost";
return "ok";
}
/**
* A pod still Pending. A stage pod pinned to its volume's node cannot move, so insufficient resources there is a wait
* with no time limit; an unpinned first pod unscheduled past the pending limit is deleted and placed again.
*/
export function pendingKind(pod: KPod, ageMs: number, pinned: boolean): PendingKind {
if (pod.status?.phase !== "Pending") return "wait";
const sched = pod.status.conditions?.find((c) => c.type === "PodScheduled");
if (sched?.status !== "False") return "wait";
if (pinned) return sched.reason === "Unschedulable" && /insufficient/i.test(sched.message ?? "") ? "capacity-wait" : "wait";
return ageMs > PENDING_LIMIT_MS ? "replace" : "wait";
}
/** A disruption recreates the stage without using an attempt, up to MAX_DISRUPTIONS per task; `disruptions` counts those so far. */
export function disruptionPolicy(disruptions: number): { recreate: boolean } {
return { recreate: disruptions < MAX_DISRUPTIONS };
}
+39
View File
@@ -0,0 +1,39 @@
// Pod end classification: cluster disruptions and lost nodes are told apart from the task's own failures, a pinned pod
// waits for capacity while an unpinned one is replaced, and disruption recreates are capped.
import assert from "node:assert/strict";
import { test } from "node:test";
import { classifyPodEnd, disruptionPolicy, pendingKind } from "../src/build/disruption.ts";
import type { KPod } from "../src/build/kube-api.ts";
import { PENDING_LIMIT_MS } from "../src/build/pods.ts";
const pod = (status: KPod["status"]): KPod => ({ metadata: { name: "p" }, status });
const term = (exitCode: number, reason?: string) => [{ name: "c", state: { terminated: { exitCode, reason } } }];
const unsched = (message: string): KPod => pod({ phase: "Pending", conditions: [{ type: "PodScheduled", status: "False", reason: "Unschedulable", message }] });
test("classifyPodEnd separates disruptions, node loss and the task's own faults", () => {
for (const reason of ["PreemptionByScheduler", "TerminationByKubelet", "EvictionByEvictionAPI", "DeletionByTaintManager"]) {
assert.equal(classifyPodEnd(pod({ phase: "Failed", conditions: [{ type: "DisruptionTarget", status: "True", reason }], containerStatuses: term(137) }), true), "disruption", reason);
}
assert.equal(classifyPodEnd(pod({ phase: "Failed", reason: "Evicted" }), true), "disruption");
assert.equal(classifyPodEnd(null, true), "disruption");
assert.equal(classifyPodEnd(null, false), "node-lost");
assert.equal(classifyPodEnd(null, null), "node-lost");
assert.equal(classifyPodEnd(pod({ phase: "Failed" }), false), "node-lost");
assert.equal(classifyPodEnd(pod({ phase: "Failed", containerStatuses: term(137, "OOMKilled") }), false), "own-fault");
assert.equal(classifyPodEnd(pod({ phase: "Failed", reason: "DeadlineExceeded" }), true), "own-fault");
assert.equal(classifyPodEnd(pod({ phase: "Failed", containerStatuses: term(2, "Error") }), true), "own-fault");
assert.equal(classifyPodEnd(pod({ phase: "Succeeded", containerStatuses: term(0, "Completed") }), true), "ok");
});
test("pendingKind holds a pinned pod for capacity and replaces an unpinned one after the pending limit", () => {
const full = unsched("0/5 nodes are available: 5 Insufficient cpu.");
assert.equal(pendingKind(full, PENDING_LIMIT_MS * 10, true), "capacity-wait");
assert.equal(pendingKind(unsched("0/5 nodes are available: 5 node(s) had untolerated taint"), PENDING_LIMIT_MS * 10, true), "wait");
assert.equal(pendingKind(full, PENDING_LIMIT_MS + 1, false), "replace");
assert.equal(pendingKind(full, PENDING_LIMIT_MS - 1, false), "wait");
assert.equal(pendingKind(pod({ phase: "Pending" }), PENDING_LIMIT_MS * 2, false), "wait");
});
test("disruptionPolicy recreates up to three times", () => {
assert.deepEqual([0, 1, 2, 3, 4].map((n) => disruptionPolicy(n).recreate), [true, true, true, false, false]);
});