diff --git a/src/mgr/session-pvc.ts b/src/mgr/session-pvc.ts index e9d75f3..2063d79 100644 --- a/src/mgr/session-pvc.ts +++ b/src/mgr/session-pvc.ts @@ -36,6 +36,7 @@ export interface SessionPvcOptions { const defaultStorageClassName = "local-path"; const defaultPvcSize = "1Gi"; const defaultSubdir = "sessions"; +const defaultNamespace = "agentrun-v01"; export function sessionPvcNameFor(sessionId: string): string { const sanitized = sanitizeSessionIdForPvc(sessionId); @@ -45,8 +46,19 @@ export function sessionPvcNameFor(sessionId: string): string { export function sanitizeSessionIdForPvc(sessionId: string): string { return sessionId.toLowerCase().replace(/[^a-z0-9-]+/gu, "-").replace(/^-+|-+$/gu, ""); } + +function runtimeNamespace(): string { + const value = process.env.AGENTRUN_RUNTIME_NAMESPACE?.trim(); + return value && value.length > 0 ? value : defaultNamespace; +} + +function runtimeLane(): string { + const value = process.env.AGENTRUN_LANE?.trim(); + return value && value.length > 0 ? value : "v0.1"; +} + export function buildSessionPvcSpec(input: { sessionId: string; namespace?: string; options: SessionPvcOptions }): SessionPvcSpec { - const namespace = input.namespace ?? "agentrun-v01"; + const namespace = input.namespace ?? runtimeNamespace(); return { pvcName: sessionPvcNameFor(input.sessionId), namespace, @@ -84,7 +96,7 @@ 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": "v0.1" } }, + metadata: { name: spec.pvcName, namespace: spec.namespace, labels: { "agentrun.pikastech.local/session-id": input.sessionId, "agentrun.pikastech.local/lane": runtimeLane() } }, spec: { accessModes: ["ReadWriteOnce"], storageClassName: spec.storageClassName, resources: { requests: { storage: spec.size } } }, }; const handler = resolveHandler(input.options); @@ -221,7 +233,7 @@ export async function runSessionStorageGc(input: { store: AgentRunStore; options if (session.activeRunId || session.activeCommandId) { skipped++; continue; } if (session.storageKind !== "pvc" || !session.storagePvcName) { skipped++; continue; } try { - await handler({ args: ["delete", "pvc", session.storagePvcName, "-n", session.storageNamespace ?? "agentrun-v01", "--ignore-not-found"] }); + await handler({ args: ["delete", "pvc", session.storagePvcName, "-n", session.storageNamespace ?? runtimeNamespace(), "--ignore-not-found"] }); await input.store.markSessionStorageEvicted({ sessionId: session.sessionId, pvcName: session.storagePvcName }); deleted++; deletedPvcNames.push(session.storagePvcName); diff --git a/src/selftest/cases/10-mgr-session-pvc.ts b/src/selftest/cases/10-mgr-session-pvc.ts index 60e1caf..28e2142 100644 --- a/src/selftest/cases/10-mgr-session-pvc.ts +++ b/src/selftest/cases/10-mgr-session-pvc.ts @@ -2,7 +2,7 @@ import assert from "node:assert/strict"; import { startManagerServer } from "../../mgr/server.js"; import { MemoryAgentRunStore } from "../../mgr/store.js"; import { ManagerClient } from "../../mgr/client.js"; -import { createSessionPvc, deleteSessionPvc, getSessionPvcSummary, refreshSessionPvcSummary, runSessionStorageGc, sessionPvcNameFor, sanitizeSessionIdForPvc } from "../../mgr/session-pvc.js"; +import { buildSessionPvcSpec, createSessionPvc, deleteSessionPvc, getSessionPvcSummary, refreshSessionPvcSummary, runSessionStorageGc, sessionPvcNameFor, sanitizeSessionIdForPvc } from "../../mgr/session-pvc.js"; import type { KubectlHandler, SessionPvcOptions } from "../../mgr/session-pvc.js"; import type { SelfTestCase } from "../harness.js"; @@ -35,6 +35,16 @@ const selfTest: SelfTestCase = async () => { assert.equal(sessionPvcNameFor("sess_with_underscores_001"), "agentrun-v01-session-sess-with-underscores-001"); assert.equal(sessionPvcNameFor("Sess.UPPER.001"), "agentrun-v01-session-sess-upper-001"); assert.equal(sessionPvcNameFor("---"), "agentrun-v01-session-default"); + const previousRuntimeNamespace = process.env.AGENTRUN_RUNTIME_NAMESPACE; + try { + delete process.env.AGENTRUN_RUNTIME_NAMESPACE; + assert.equal(buildSessionPvcSpec({ sessionId: "sess_default_namespace", options: opts }).namespace, "agentrun-v01"); + process.env.AGENTRUN_RUNTIME_NAMESPACE = "agentrun-v02"; + assert.equal(buildSessionPvcSpec({ sessionId: "sess_runtime_namespace", options: opts }).namespace, "agentrun-v02"); + } finally { + if (previousRuntimeNamespace === undefined) delete process.env.AGENTRUN_RUNTIME_NAMESPACE; + else process.env.AGENTRUN_RUNTIME_NAMESPACE = previousRuntimeNamespace; + } const after = await store.getSession(sessionId); assert.equal(after?.storageKind, "pvc"); assert.equal(after?.storagePvcName, summary.pvcName);