fix: enforce runner retention before job create

This commit is contained in:
lyon
2026-06-19 22:35:55 +08:00
parent 56ecb493ef
commit 1be0e8e02f
5 changed files with 587 additions and 15 deletions
+16 -5
View File
@@ -50,6 +50,8 @@ export interface RunnerJobRenderOptions {
runnerId?: string;
sourceCommit?: string;
serviceAccountName?: string;
jobNamePrefix?: string;
lane?: string;
imagePullPolicy?: string;
backoffLimit?: number;
ttlSecondsAfterFinished?: number;
@@ -152,9 +154,11 @@ export function renderRunnerJobManifest(options: RunnerJobRenderOptions): { mani
const runnerId = options.runnerId ?? `runner_${shortHash(`${options.run.id}:${attemptId}:${options.commandId}`)}`;
const sourceCommit = options.sourceCommit ?? process.env.AGENTRUN_SOURCE_COMMIT ?? "unknown";
const serviceAccountName = options.serviceAccountName ?? "agentrun-v01-runner";
const jobNamePrefix = normalizeJobNamePrefix(options.jobNamePrefix ?? serviceAccountName);
const lane = options.lane ?? process.env.AGENTRUN_LANE ?? "v0.1";
const ttlSecondsAfterFinished = options.ttlSecondsAfterFinished ?? 86_400;
const runnerIdleTimeoutMs = normalizeRunnerIdleTimeoutMs(options.runnerIdleTimeoutMs);
const jobName = `agentrun-v01-runner-${shortDnsHash(options.run.id, attemptId)}`;
const jobName = `${jobNamePrefix}-${shortDnsHash(options.run.id, attemptId)}`;
const secretRefs = credentialProjections(options.run, namespace);
const toolCredentials = toolCredentialProjections(options.run, namespace);
const sessionPvc = options.sessionPvc;
@@ -167,7 +171,7 @@ export function renderRunnerJobManifest(options: RunnerJobRenderOptions): { mani
metadata: {
name: jobName,
namespace,
labels: labels(options.run, jobName),
labels: labels(options.run, jobName, lane),
annotations: {
"agentrun.pikastech.local/run-id": options.run.id,
"agentrun.pikastech.local/command-id": options.commandId,
@@ -179,7 +183,7 @@ export function renderRunnerJobManifest(options: RunnerJobRenderOptions): { mani
ttlSecondsAfterFinished,
template: {
metadata: {
labels: labels(options.run, jobName),
labels: labels(options.run, jobName, lane),
annotations: {
"agentrun.pikastech.local/run-id": options.run.id,
"agentrun.pikastech.local/command-id": options.commandId,
@@ -483,17 +487,24 @@ function defaultRuntimeHome(profile: string): string {
return `/home/agentrun/.codex-${sanitizeVolumeName(profile)}`;
}
function labels(run: RunRecord, jobName: string): JsonRecord {
function labels(run: RunRecord, jobName: string, lane: string): JsonRecord {
return {
"app.kubernetes.io/name": "agentrun-runner",
"app.kubernetes.io/component": "runner",
"app.kubernetes.io/part-of": "agentrun",
"agentrun.pikastech.local/lane": "v0.1",
"agentrun.pikastech.local/lane": lane,
"agentrun.pikastech.local/run-hash": shortHash(run.id),
"job-name": jobName,
};
}
function normalizeJobNamePrefix(value: string): string {
const normalized = value.toLowerCase().replace(/[^a-z0-9-]+/gu, "-").replace(/^-+|-+$/gu, "");
if (!normalized) throw new Error("jobNamePrefix must contain at least one DNS label character");
if (normalized.length > 46) return normalized.slice(0, 46).replace(/-+$/u, "") || "agentrun-runner";
return normalized;
}
function shortDnsHash(...parts: string[]): string {
return shortHash(parts.join(":"));
}