Merge pull request #2519 from pikasTech/fix/1710-nc01-memory-free-4g
Pipelines as Code CI / hwlab-web-probe-sentinel-nc01- Success
Pipelines as Code CI / platform-infra-gitea-nc01- Success
Pipelines as Code CI / unidesk-host- Success

修复 AgentRun 会话 PVC 受控回收
This commit is contained in:
Lyon
2026-07-18 17:03:27 +08:00
committed by GitHub
2 changed files with 42 additions and 13 deletions
+1
View File
@@ -640,6 +640,7 @@ controlPlane:
sessionPvcRetention:
enabled: true
prefixes:
- agentrun-v01-session-
- agentrun-release-session-
- agentrun-nc01-release-session-
maxDeletePerRun: 1000
+41 -13
View File
@@ -4,12 +4,30 @@ function runJson(args) {
return JSON.parse(execFileSync("kubectl", args, { encoding: "utf8", maxBuffer: 32 * 1024 * 1024 }));
}
function duBytes(path) {
if (!path || !path.startsWith("/var/lib/rancher/k3s/storage/")) return null;
const result = spawnSync("du", ["-sb", path], { encoding: "utf8", timeout: 8000 });
if (result.status !== 0) return null;
const value = Number(result.stdout.trim().split(/\s+/u)[0]);
return Number.isFinite(value) ? value : null;
function duBytesByPath(paths) {
const allowed = [...new Set(paths.filter((path) => path?.startsWith("/var/lib/rancher/k3s/storage/")))];
if (allowed.length === 0) {
return { sizes: new Map(), summary: { requested: 0, measured: 0, timedOut: false, status: 0 } };
}
const result = spawnSync("du", ["-sb", ...allowed], {
encoding: "utf8",
timeout: 15000,
maxBuffer: 8 * 1024 * 1024,
});
const sizes = new Map();
for (const line of result.stdout?.split(/\r?\n/u) || []) {
const match = line.match(/^(\d+)\s+(.+)$/u);
if (match) sizes.set(match[2], Number(match[1]));
}
return {
sizes,
summary: {
requested: allowed.length,
measured: sizes.size,
timedOut: result.error?.code === "ETIMEDOUT",
status: result.status,
},
};
}
const namespace = process.env.NAMESPACE;
@@ -41,8 +59,8 @@ for (const pod of podData.items || []) {
}
}
const candidates = [];
const protectedRows = [];
const candidateFacts = [];
const protectedFacts = [];
for (const pvc of pvcData.items || []) {
const name = pvc.metadata?.name || "";
const matchedPrefix = prefixes.find((prefix) => name.startsWith(prefix));
@@ -63,15 +81,24 @@ for (const pvc of pvcData.items || []) {
reclaimPolicy,
activeMountCount: activeMountPods.length,
activeMountPods: activeMountPods.slice(0, 5),
estimatedBytes: duBytes(hostPath),
hostPath,
};
if (activeMountPods.length > 0 || storageClass !== "local-path" || reclaimPolicy !== "Delete") {
protectedRows.push({ ...row, reason: activeMountPods.length > 0 ? "active-mount" : "not-local-path-delete" });
protectedFacts.push({ ...row, reason: activeMountPods.length > 0 ? "active-mount" : "not-local-path-delete" });
} else {
candidates.push(row);
candidateFacts.push(row);
}
}
const sizeScan = duBytesByPath(candidateFacts.map((item) => item.hostPath));
const candidates = candidateFacts.map(({ hostPath, ...item }) => ({
...item,
estimatedBytes: sizeScan.sizes.get(hostPath) ?? null,
}));
const protectedRows = protectedFacts.map(({ hostPath, ...item }) => ({
...item,
estimatedBytes: sizeScan.sizes.get(hostPath) ?? null,
}));
candidates.sort((a, b) => (b.estimatedBytes || 0) - (a.estimatedBytes || 0));
const selected = candidates.slice(0, limit);
const result = {
@@ -84,9 +111,10 @@ const result = {
candidatePvcCount: candidates.length,
selectedPvcCount: selected.length,
protectedPvcCount: protectedRows.length,
sizeScan: sizeScan.summary,
estimatedReclaimBytes: selected.reduce((sum, item) => sum + (item.estimatedBytes || 0), 0),
selectedPreview: selected.slice(0, 12),
protectedPreview: protectedRows.slice(0, 12),
selectedPreview: selected.slice(0, 5),
protectedPreview: protectedRows.slice(0, 5),
deletedPvcCount: 0,
valuesPrinted: false,
};