Fix terminal proxy: use pod IP instead of non-existent DNS
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:
Hector Ros
2026-01-20 18:50:08 +01:00
parent 3fef6030ea
commit 209b439d26
2 changed files with 38 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ import { handleAuthRoutes, handleProjectRoutes, handleTaskRoutes, handleAgentRou
import { authenticateRequest } from './api/middleware/auth'
import { agents } from './db/schema'
import { eq, and } from 'drizzle-orm'
import { initK8sClient } from './lib/k8s'
import { initK8sClient, getPodIP } from './lib/k8s'
console.log('🚀 Starting AiWorker Backend...')
console.log(`Bun version: ${Bun.version}`)
@@ -110,8 +110,21 @@ const server = Bun.serve({
)
}
// Proxy to agent terminal
const agentUrl = `http://${agent.podName}.agents.svc.cluster.local:7681${url.pathname.replace(`/agent-terminal/${agentId}`, '')}${url.search}`
// Get pod IP
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 {
const response = await fetch(agentUrl, {
@@ -121,8 +134,8 @@ const server = Bun.serve({
})
return response
} catch (error) {
console.error('Terminal proxy error:', error)
} catch (error: any) {
console.error('Terminal proxy error:', error.message)
return Response.json(
{ success: false, message: 'Failed to connect to agent terminal' },
{ status: 502 }

View File

@@ -261,3 +261,23 @@ export async function getPodStatus(podName: string): Promise<string | null> {
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
}
}