Fix terminal proxy: use pod IP instead of non-existent DNS
All checks were successful
Build and Push Backend / build (push) Successful in 6s
All checks were successful
Build and Push Backend / build (push) Successful in 6s
- Add getPodIP() function to get pod IP from K8s API - Update terminal proxy to use pod IP directly - Add logging for proxy requests - Fixes terminal showing black screen issue
This commit is contained in:
23
src/index.ts
23
src/index.ts
@@ -9,7 +9,7 @@ import { handleAuthRoutes, handleProjectRoutes, handleTaskRoutes, handleAgentRou
|
|||||||
import { authenticateRequest } from './api/middleware/auth'
|
import { authenticateRequest } from './api/middleware/auth'
|
||||||
import { agents } from './db/schema'
|
import { agents } from './db/schema'
|
||||||
import { eq, and } from 'drizzle-orm'
|
import { eq, and } from 'drizzle-orm'
|
||||||
import { initK8sClient } from './lib/k8s'
|
import { initK8sClient, getPodIP } from './lib/k8s'
|
||||||
|
|
||||||
console.log('🚀 Starting AiWorker Backend...')
|
console.log('🚀 Starting AiWorker Backend...')
|
||||||
console.log(`Bun version: ${Bun.version}`)
|
console.log(`Bun version: ${Bun.version}`)
|
||||||
@@ -110,8 +110,21 @@ const server = Bun.serve({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxy to agent terminal
|
// Get pod IP
|
||||||
const agentUrl = `http://${agent.podName}.agents.svc.cluster.local:7681${url.pathname.replace(`/agent-terminal/${agentId}`, '')}${url.search}`
|
const podIP = await getPodIP(agent.podName)
|
||||||
|
if (!podIP) {
|
||||||
|
console.error(`Pod ${agent.podName} not found or has no IP`)
|
||||||
|
return Response.json(
|
||||||
|
{ success: false, message: 'Agent pod not ready' },
|
||||||
|
{ status: 503 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Proxy to agent terminal (ttyd on port 7681)
|
||||||
|
const agentPath = url.pathname.replace(`/agent-terminal/${agentId}`, '') || '/'
|
||||||
|
const agentUrl = `http://${podIP}:7681${agentPath}${url.search}`
|
||||||
|
|
||||||
|
console.log(`🔄 Proxying terminal request to ${agentUrl}`)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(agentUrl, {
|
const response = await fetch(agentUrl, {
|
||||||
@@ -121,8 +134,8 @@ const server = Bun.serve({
|
|||||||
})
|
})
|
||||||
|
|
||||||
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 }
|
||||||
|
|||||||
@@ -261,3 +261,23 @@ 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 {
|
||||||
|
const response = await client.readNamespacedPod({
|
||||||
|
name: podName,
|
||||||
|
namespace: 'agents'
|
||||||
|
})
|
||||||
|
return response.body.status?.podIP || null
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.statusCode === 404 || error.response?.statusCode === 404) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user