Compare commits
9 Commits
e0c6884a7b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db09616a69 | ||
|
|
6864258810 | ||
|
|
209b439d26 | ||
|
|
3fef6030ea | ||
|
|
65b18d13b5 | ||
|
|
9eb9def85c | ||
|
|
697ee1b426 | ||
|
|
3bc59dc964 | ||
|
|
14ae28f13c |
@@ -8,7 +8,7 @@ import { agents, tasks } from '../../db/schema'
|
|||||||
import { eq, and } from 'drizzle-orm'
|
import { eq, and } from 'drizzle-orm'
|
||||||
import { randomUUID } from 'crypto'
|
import { randomUUID } from 'crypto'
|
||||||
import { authenticateRequest } from '../middleware/auth'
|
import { authenticateRequest } from '../middleware/auth'
|
||||||
import { createAgentPod, deleteAgentPod } from '../../lib/k8s'
|
import { createAgentPod, deleteAgentPod, createAgentService, deleteAgentService } from '../../lib/k8s'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle all agent routes
|
* Handle all agent routes
|
||||||
@@ -332,12 +332,13 @@ async function unregisterAgent(agentId: string, userId: string): Promise<Respons
|
|||||||
.where(eq(tasks.id, existing[0].currentTaskId))
|
.where(eq(tasks.id, existing[0].currentTaskId))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete K8s pod
|
// Delete K8s pod and service
|
||||||
try {
|
try {
|
||||||
await deleteAgentPod(existing[0].podName)
|
await deleteAgentPod(existing[0].podName)
|
||||||
|
await deleteAgentService(existing[0].podName)
|
||||||
} catch (k8sError) {
|
} catch (k8sError) {
|
||||||
console.error('Failed to delete pod, continuing...', k8sError)
|
console.error('Failed to delete pod/service, continuing...', k8sError)
|
||||||
// Continue even if pod deletion fails
|
// Continue even if deletion fails
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete agent from DB
|
// Delete agent from DB
|
||||||
@@ -402,11 +403,12 @@ async function launchAgent(userId: string, req: Request): Promise<Response> {
|
|||||||
|
|
||||||
await db.insert(agents).values(newAgent)
|
await db.insert(agents).values(newAgent)
|
||||||
|
|
||||||
// Create K8s pod
|
// Create K8s pod and service
|
||||||
try {
|
try {
|
||||||
await createAgentPod(podName, userId)
|
await createAgentPod(podName, userId, agentId)
|
||||||
|
await createAgentService(podName, agentId)
|
||||||
} catch (k8sError: any) {
|
} catch (k8sError: any) {
|
||||||
// If pod creation fails, rollback DB entry
|
// If pod/service creation fails, rollback DB entry
|
||||||
await db.delete(agents).where(eq(agents.id, agentId))
|
await db.delete(agents).where(eq(agents.id, agentId))
|
||||||
throw new Error(`Failed to create pod: ${k8sError.message}`)
|
throw new Error(`Failed to create pod: ${k8sError.message}`)
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/index.ts
14
src/index.ts
@@ -110,19 +110,23 @@ const server = Bun.serve({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxy to agent terminal
|
// Proxy to agent terminal via service DNS
|
||||||
const agentUrl = `http://${agent.podName}.agents.svc.cluster.local:7681${url.pathname.replace(`/agent-terminal/${agentId}`, '')}${url.search}`
|
// Service name: {podName}-terminal.agents.svc.cluster.local:7681
|
||||||
|
const agentPath = url.pathname.replace(`/agent-terminal/${agentId}`, '') || '/'
|
||||||
|
const serviceUrl = `http://${agent.podName}-terminal.agents.svc.cluster.local:7681${agentPath}${url.search}`
|
||||||
|
|
||||||
|
console.log(`🔄 Proxying terminal request to ${serviceUrl}`)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(agentUrl, {
|
const response = await fetch(serviceUrl, {
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
})
|
})
|
||||||
|
|
||||||
return response
|
return response
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
console.error('Terminal proxy error:', error)
|
console.error('Terminal proxy error:', error.message)
|
||||||
return Response.json(
|
return Response.json(
|
||||||
{ success: false, message: 'Failed to connect to agent terminal' },
|
{ success: false, message: 'Failed to connect to agent terminal' },
|
||||||
{ status: 502 }
|
{ status: 502 }
|
||||||
|
|||||||
170
src/lib/k8s.ts
170
src/lib/k8s.ts
@@ -3,6 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import * as k8s from '@kubernetes/client-node'
|
import * as k8s from '@kubernetes/client-node'
|
||||||
|
import * as https from 'https'
|
||||||
|
|
||||||
let k8sClient: k8s.CoreV1Api | null = null
|
let k8sClient: k8s.CoreV1Api | null = null
|
||||||
let k8sConfig: k8s.KubeConfig | null = null
|
let k8sConfig: k8s.KubeConfig | null = null
|
||||||
@@ -20,6 +21,33 @@ export function initK8sClient() {
|
|||||||
|
|
||||||
if (inCluster) {
|
if (inCluster) {
|
||||||
k8sConfig.loadFromCluster()
|
k8sConfig.loadFromCluster()
|
||||||
|
console.log('📦 Loaded K8s config from cluster')
|
||||||
|
|
||||||
|
// Skip TLS verification when in cluster
|
||||||
|
// This is needed because the cluster uses self-signed certificates
|
||||||
|
const cluster = k8sConfig.getCurrentCluster()
|
||||||
|
console.log('📦 Current cluster:', cluster)
|
||||||
|
|
||||||
|
if (cluster) {
|
||||||
|
cluster.skipTLSVerify = true
|
||||||
|
console.log('🔓 Set skipTLSVerify = true')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create custom HTTPS agent that ignores certificate errors
|
||||||
|
const httpsAgent = new https.Agent({
|
||||||
|
rejectUnauthorized: false
|
||||||
|
})
|
||||||
|
console.log('🔓 Created HTTPS agent with rejectUnauthorized: false')
|
||||||
|
|
||||||
|
// Apply custom agent to the config
|
||||||
|
try {
|
||||||
|
k8sConfig.applyToHTTPSOptions({
|
||||||
|
httpsAgent: httpsAgent
|
||||||
|
} as any)
|
||||||
|
console.log('✅ Applied custom HTTPS agent to K8s config')
|
||||||
|
} catch (applyError: any) {
|
||||||
|
console.error('❌ Failed to apply HTTPS options:', applyError.message)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Load from kubeconfig file
|
// Load from kubeconfig file
|
||||||
const configPath = process.env.K8S_CONFIG_PATH || process.env.KUBECONFIG || '~/.kube/config'
|
const configPath = process.env.K8S_CONFIG_PATH || process.env.KUBECONFIG || '~/.kube/config'
|
||||||
@@ -40,6 +68,23 @@ export function getK8sClient(): k8s.CoreV1Api {
|
|||||||
return k8sClient
|
return k8sClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Kubernetes client with custom request options
|
||||||
|
* This ensures the HTTPS agent is used for each request
|
||||||
|
*/
|
||||||
|
export function getK8sClientWithOptions(): { client: k8s.CoreV1Api, options: any } {
|
||||||
|
const client = getK8sClient()
|
||||||
|
|
||||||
|
// Create request options with custom HTTPS agent
|
||||||
|
const options = {
|
||||||
|
httpsAgent: new https.Agent({
|
||||||
|
rejectUnauthorized: false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return { client, options }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create pod spec for agent
|
* Create pod spec for agent
|
||||||
*/
|
*/
|
||||||
@@ -50,6 +95,7 @@ export function createAgentPodSpec(podName: string, userId: string): k8s.V1Pod {
|
|||||||
labels: {
|
labels: {
|
||||||
app: 'claude-agent',
|
app: 'claude-agent',
|
||||||
userId: userId,
|
userId: userId,
|
||||||
|
podName: podName,
|
||||||
'aiworker.io/agent': 'true',
|
'aiworker.io/agent': 'true',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -143,20 +189,96 @@ export function createAgentPodSpec(podName: string, userId: string): k8s.V1Pod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create agent pod in Kubernetes
|
* Create service for agent pod (for terminal access)
|
||||||
*/
|
*/
|
||||||
export async function createAgentPod(podName: string, userId: string): Promise<void> {
|
export async function createAgentService(podName: string, agentId: string): Promise<void> {
|
||||||
const client = getK8sClient()
|
const client = getK8sClient()
|
||||||
const podSpec = createAgentPodSpec(podName, userId)
|
|
||||||
|
const serviceSpec: k8s.V1Service = {
|
||||||
|
metadata: {
|
||||||
|
name: `${podName}-terminal`,
|
||||||
|
namespace: 'agents',
|
||||||
|
labels: {
|
||||||
|
app: 'claude-agent-terminal',
|
||||||
|
agentId: agentId,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
spec: {
|
||||||
|
selector: {
|
||||||
|
app: 'claude-agent',
|
||||||
|
podName: podName,
|
||||||
|
},
|
||||||
|
ports: [{
|
||||||
|
name: 'terminal',
|
||||||
|
port: 7681,
|
||||||
|
targetPort: 7681 as any,
|
||||||
|
protocol: 'TCP'
|
||||||
|
}],
|
||||||
|
type: 'ClusterIP'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await client.createNamespacedPod({
|
await client.createNamespacedService({
|
||||||
|
namespace: 'agents',
|
||||||
|
body: serviceSpec
|
||||||
|
})
|
||||||
|
console.log(`✅ Service ${podName}-terminal created`)
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`❌ Failed to create service:`, error.message)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete agent service
|
||||||
|
*/
|
||||||
|
export async function deleteAgentService(podName: string): Promise<void> {
|
||||||
|
const client = getK8sClient()
|
||||||
|
|
||||||
|
try {
|
||||||
|
await client.deleteNamespacedService({
|
||||||
|
name: `${podName}-terminal`,
|
||||||
|
namespace: 'agents'
|
||||||
|
})
|
||||||
|
console.log(`✅ Service ${podName}-terminal deleted`)
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.statusCode === 404 || error.response?.statusCode === 404) {
|
||||||
|
console.log(`⚠️ Service ${podName}-terminal not found`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.error(`❌ Error deleting service:`, error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create agent pod in Kubernetes
|
||||||
|
*/
|
||||||
|
export async function createAgentPod(podName: string, userId: string, agentId: string): Promise<void> {
|
||||||
|
const { client, options } = getK8sClientWithOptions()
|
||||||
|
const podSpec = createAgentPodSpec(podName, userId)
|
||||||
|
|
||||||
|
console.log(`🔧 Creating pod ${podName} for user ${userId}`)
|
||||||
|
console.log(`🔧 Using custom HTTPS agent with rejectUnauthorized: false`)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await client.createNamespacedPod({
|
||||||
namespace: 'agents',
|
namespace: 'agents',
|
||||||
body: podSpec
|
body: podSpec
|
||||||
})
|
}, undefined, undefined, undefined, undefined, options)
|
||||||
|
|
||||||
console.log(`✅ Pod ${podName} created successfully`)
|
console.log(`✅ Pod ${podName} created successfully`)
|
||||||
|
if (result?.body?.metadata?.uid) {
|
||||||
|
console.log(`✅ Pod UID: ${result.body.metadata.uid}`)
|
||||||
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error(`❌ Failed to create pod ${podName}:`, error.message)
|
console.error(`❌ Failed to create pod ${podName}`)
|
||||||
|
console.error(`❌ Error message:`, error.message)
|
||||||
|
console.error(`❌ Error code:`, error.code)
|
||||||
|
if (error.response) {
|
||||||
|
console.error(`❌ Response status:`, error.response.statusCode)
|
||||||
|
console.error(`❌ Response body:`, error.response.body)
|
||||||
|
}
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,7 +317,10 @@ export async function getPodStatus(podName: string): Promise<string | null> {
|
|||||||
name: podName,
|
name: podName,
|
||||||
namespace: 'agents'
|
namespace: 'agents'
|
||||||
})
|
})
|
||||||
return response.body.status?.phase || null
|
|
||||||
|
// Handle different response structures
|
||||||
|
const pod = response.body || response
|
||||||
|
return pod?.status?.phase || null
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.statusCode === 404 || error.response?.statusCode === 404) {
|
if (error.statusCode === 404 || error.response?.statusCode === 404) {
|
||||||
return null
|
return null
|
||||||
@@ -203,3 +328,34 @@ export async function getPodStatus(podName: string): Promise<string | null> {
|
|||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get pod IP address
|
||||||
|
*/
|
||||||
|
export async function getPodIP(podName: string): Promise<string | null> {
|
||||||
|
const client = getK8sClient()
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(`🔍 Getting IP for pod: ${podName}`)
|
||||||
|
const response = await client.readNamespacedPod({
|
||||||
|
name: podName,
|
||||||
|
namespace: 'agents'
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(`🔍 Response type: ${typeof response}`)
|
||||||
|
console.log(`🔍 Has body: ${'body' in response}`)
|
||||||
|
|
||||||
|
// Handle different response structures
|
||||||
|
const pod = response.body || response
|
||||||
|
const podIP = pod?.status?.podIP
|
||||||
|
|
||||||
|
console.log(`🔍 Pod IP: ${podIP}`)
|
||||||
|
return podIP || null
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`❌ Error getting pod IP for ${podName}:`, error.message)
|
||||||
|
if (error.statusCode === 404 || error.response?.statusCode === 404) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user