From 209b439d2605e6dd3e4589e7f3434452f0059ea9 Mon Sep 17 00:00:00 2001 From: Hector Ros Date: Tue, 20 Jan 2026 18:50:08 +0100 Subject: [PATCH] Fix terminal proxy: use pod IP instead of non-existent DNS - 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 --- src/index.ts | 23 ++++++++++++++++++----- src/lib/k8s.ts | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index a510eb5..d45eb25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } diff --git a/src/lib/k8s.ts b/src/lib/k8s.ts index 519ca77..0b0fb3c 100644 --- a/src/lib/k8s.ts +++ b/src/lib/k8s.ts @@ -261,3 +261,23 @@ export async function getPodStatus(podName: string): Promise { throw error } } + +/** + * Get pod IP address + */ +export async function getPodIP(podName: string): Promise { + 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 + } +}