Implement Kubernetes pod management for agents
All checks were successful
Build and Push Backend / build (push) Successful in 5s

- Add @kubernetes/client-node dependency
- Create K8s client utilities in src/lib/k8s.ts
- Implement createAgentPod and deleteAgentPod functions
- Update launchAgent to actually create pods in K8s
- Update unregisterAgent to delete pods from K8s
- Initialize K8s client on backend startup
- Add rollback logic if pod creation fails

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hector Ros
2026-01-20 17:34:10 +01:00
parent 8382f6645e
commit f104425b91
3 changed files with 231 additions and 11 deletions

199
src/lib/k8s.ts Normal file
View File

@@ -0,0 +1,199 @@
/**
* Kubernetes API client utilities
*/
import * as k8s from '@kubernetes/client-node'
let k8sClient: k8s.CoreV1Api | null = null
let k8sConfig: k8s.KubeConfig | null = null
/**
* Initialize Kubernetes client
*/
export function initK8sClient() {
if (k8sClient) return k8sClient
k8sConfig = new k8s.KubeConfig()
// Check if running in cluster
const inCluster = process.env.K8S_IN_CLUSTER === 'true'
if (inCluster) {
k8sConfig.loadFromCluster()
} else {
// Load from kubeconfig file
const configPath = process.env.K8S_CONFIG_PATH || process.env.KUBECONFIG || '~/.kube/config'
k8sConfig.loadFromFile(configPath)
}
k8sClient = k8sConfig.makeApiClient(k8s.CoreV1Api)
return k8sClient
}
/**
* Get Kubernetes client
*/
export function getK8sClient(): k8s.CoreV1Api {
if (!k8sClient) {
return initK8sClient()
}
return k8sClient
}
/**
* Create pod spec for agent
*/
export function createAgentPodSpec(podName: string, userId: string) {
return {
apiVersion: 'v1',
kind: 'Pod',
metadata: {
name: podName,
namespace: 'agents',
labels: {
app: 'claude-agent',
userId: userId,
'aiworker.io/agent': 'true',
},
},
spec: {
serviceAccountName: 'agent-sa',
imagePullSecrets: [
{
name: 'gitea-registry',
},
],
containers: [
{
name: 'agent',
image: 'git.fuq.tv/admin/aiworker-agent:latest',
imagePullPolicy: 'Always',
ports: [
{
containerPort: 7681,
name: 'terminal',
},
],
env: [
{
name: 'BACKEND_URL',
value: 'https://api.fuq.tv',
},
{
name: 'MCP_ENDPOINT',
value: 'https://api.fuq.tv/api/mcp',
},
{
name: 'GITEA_URL',
value: 'https://git.fuq.tv',
},
{
name: 'GITEA_TOKEN',
valueFrom: {
secretKeyRef: {
name: 'agent-secrets',
key: 'gitea-token',
},
},
},
{
name: 'POD_NAME',
valueFrom: {
fieldRef: {
fieldPath: 'metadata.name',
},
},
},
{
name: 'NAMESPACE',
valueFrom: {
fieldRef: {
fieldPath: 'metadata.namespace',
},
},
},
{
name: 'USER_ID',
value: userId,
},
],
resources: {
requests: {
cpu: '500m',
memory: '1Gi',
},
limits: {
cpu: '2000m',
memory: '4Gi',
},
},
volumeMounts: [
{
name: 'workspace',
mountPath: '/workspace',
},
],
},
],
volumes: [
{
name: 'workspace',
emptyDir: {},
},
],
},
}
}
/**
* Create agent pod in Kubernetes
*/
export async function createAgentPod(podName: string, userId: string): Promise<void> {
const client = getK8sClient()
const podSpec = createAgentPodSpec(podName, userId)
try {
await client.createNamespacedPod('agents', podSpec)
console.log(`✅ Pod ${podName} created successfully`)
} catch (error: any) {
console.error(`❌ Failed to create pod ${podName}:`, error.message)
throw error
}
}
/**
* Delete agent pod from Kubernetes
*/
export async function deleteAgentPod(podName: string): Promise<void> {
const client = getK8sClient()
try {
await client.deleteNamespacedPod(podName, 'agents')
console.log(`✅ Pod ${podName} deleted successfully`)
} catch (error: any) {
// Ignore 404 errors (pod already deleted)
if (error.statusCode === 404) {
console.log(`⚠️ Pod ${podName} not found (already deleted)`)
return
}
console.error(`❌ Failed to delete pod ${podName}:`, error.message)
throw error
}
}
/**
* Get pod status
*/
export async function getPodStatus(podName: string): Promise<string | null> {
const client = getK8sClient()
try {
const response = await client.readNamespacedPod(podName, 'agents')
return response.body.status?.phase || null
} catch (error: any) {
if (error.statusCode === 404) {
return null
}
throw error
}
}