fix: shorten session PVC label values

This commit is contained in:
lyon
2026-06-20 11:05:27 +08:00
parent bbbc86594e
commit 9bbec1c1a8
2 changed files with 23 additions and 2 deletions
+17 -1
View File
@@ -1,4 +1,5 @@
import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
import type { AgentRunStore } from "./store.js";
import type { JsonRecord } from "../common/types.js";
import { AgentRunError } from "../common/errors.js";
@@ -37,6 +38,7 @@ const defaultStorageClassName = "local-path";
const defaultPvcSize = "1Gi";
const defaultSubdir = "sessions";
const defaultNamespace = "agentrun-v01";
const maxKubernetesLabelValueLength = 63;
export function sessionPvcNameFor(sessionId: string): string {
const sanitized = sanitizeSessionIdForPvc(sessionId);
@@ -47,6 +49,15 @@ export function sanitizeSessionIdForPvc(sessionId: string): string {
return sessionId.toLowerCase().replace(/[^a-z0-9-]+/gu, "-").replace(/^-+|-+$/gu, "");
}
export function sessionPvcLabelValueFor(sessionId: string): string {
const sanitized = sanitizeSessionIdForPvc(sessionId) || "default";
if (sanitized.length <= maxKubernetesLabelValueLength) return sanitized;
const hash = createHash("sha256").update(sessionId).digest("hex").slice(0, 12);
const prefixLength = maxKubernetesLabelValueLength - hash.length - 1;
const prefix = sanitized.slice(0, prefixLength).replace(/-+$/gu, "") || "session";
return `${prefix}-${hash}`;
}
function runtimeNamespace(): string {
const value = process.env.AGENTRUN_RUNTIME_NAMESPACE?.trim();
return value && value.length > 0 ? value : defaultNamespace;
@@ -96,7 +107,12 @@ export async function createSessionPvc(input: { store: AgentRunStore; sessionId:
const manifest: JsonRecord = {
apiVersion: "v1",
kind: "PersistentVolumeClaim",
metadata: { name: spec.pvcName, namespace: spec.namespace, labels: { "agentrun.pikastech.local/session-id": input.sessionId, "agentrun.pikastech.local/lane": runtimeLane() } },
metadata: {
name: spec.pvcName,
namespace: spec.namespace,
labels: { "agentrun.pikastech.local/session-id": sessionPvcLabelValueFor(input.sessionId), "agentrun.pikastech.local/lane": runtimeLane() },
annotations: { "agentrun.pikastech.local/session-id": input.sessionId },
},
spec: { accessModes: ["ReadWriteOnce"], storageClassName: spec.storageClassName, resources: { requests: { storage: spec.size } } },
};
const handler = resolveHandler(input.options);