Skip to content

Commit 64130e7

Browse files
committed
fix: issue with resolution of external IPs
1 parent dc23c84 commit 64130e7

File tree

2 files changed

+78
-52
lines changed

2 files changed

+78
-52
lines changed

platforms/registry/src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "path";
55
import { AppDataSource } from "./config/database";
66
import { VaultService } from "./services/VaultService";
77
import { UriResolutionService } from "./services/UriResolutionService";
8+
import { KubernetesService } from "./services/KubernetesService";
89
import cors from "@fastify/cors";
910

1011
dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
@@ -36,6 +37,9 @@ const vaultService = new VaultService(AppDataSource.getRepository("Vault"));
3637
// Initialize UriResolutionService for health checks and Kubernetes fallbacks
3738
const uriResolutionService = new UriResolutionService();
3839

40+
// Initialize KubernetesService for debugging
41+
const kubernetesService = new KubernetesService();
42+
3943
// Middleware to check shared secret
4044
const checkSharedSecret = async (request: any, reply: any) => {
4145
const authHeader = request.headers.authorization;
@@ -127,6 +131,23 @@ server.get("/.well-known/jwks.json", async (request, reply) => {
127131
}
128132
});
129133

134+
// Debug endpoint for Kubernetes connectivity (remove in production)
135+
server.get("/debug/kubernetes", async (request, reply) => {
136+
try {
137+
const debugInfo = await kubernetesService.debugExternalIps();
138+
reply.send({
139+
status: "ok",
140+
timestamp: new Date().toISOString(),
141+
kubernetes: debugInfo
142+
});
143+
} catch (error) {
144+
reply.status(500).send({
145+
error: "Failed to get Kubernetes debug info",
146+
message: error instanceof Error ? error.message : String(error)
147+
});
148+
}
149+
});
150+
130151
// Resolve service from database based on w3id
131152
server.get("/resolve", async (request, reply) => {
132153
try {

platforms/registry/src/services/KubernetesService.ts

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,45 @@ export class KubernetesService {
1717
}
1818

1919
/**
20-
* Get a working external IP from Kubernetes services
21-
* This will look for services with LoadBalancer type or NodePort with external IPs
20+
* Get a working external IP from Kubernetes nodes
2221
*/
2322
async getWorkingExternalIp(): Promise<string | null> {
2423
try {
25-
console.log('Querying Kubernetes for external IPs...');
26-
27-
// Get all services across all namespaces
28-
const servicesResponse = await this.coreV1Api.listServiceForAllNamespaces();
24+
console.log('Querying Kubernetes for node external IPs...');
2925

26+
const nodesResponse = await this.coreV1Api.listNode();
3027
const externalIps: string[] = [];
3128

32-
for (const service of servicesResponse.body.items) {
33-
// Check for LoadBalancer services with external IPs
34-
if (service.spec?.type === 'LoadBalancer' && service.status?.loadBalancer?.ingress) {
35-
for (const ingress of service.status.loadBalancer.ingress) {
36-
if (ingress.ip) {
37-
externalIps.push(ingress.ip);
38-
console.log(`Found LoadBalancer external IP: ${ingress.ip}`);
29+
for (const node of nodesResponse.body.items) {
30+
if (node.status?.addresses) {
31+
for (const address of node.status.addresses) {
32+
if (address.type === 'ExternalIP' && address.address) {
33+
externalIps.push(address.address);
34+
console.log(`Found node external IP: ${address.address}`);
3935
}
4036
}
4137
}
42-
43-
// Check for services with external IPs
44-
if (service.spec?.externalIPs) {
45-
for (const externalIp of service.spec.externalIPs) {
46-
externalIps.push(externalIp);
47-
console.log(`Found service external IP: ${externalIp}`);
48-
}
49-
}
5038
}
5139

52-
// Remove duplicates
53-
const uniqueIps = [...new Set(externalIps)];
54-
55-
if (uniqueIps.length === 0) {
56-
console.log('No external IPs found in Kubernetes');
40+
if (externalIps.length === 0) {
41+
console.log('No external IPs found in Kubernetes nodes');
5742
return null;
5843
}
5944

60-
// Test each IP to find a working one
61-
for (const ip of uniqueIps) {
62-
if (await this.testIpConnectivity(ip)) {
63-
console.log(`Found working external IP: ${ip}`);
64-
return ip;
65-
}
66-
}
45+
console.log(`Found ${externalIps.length} external IPs:`, externalIps);
6746

68-
console.log('No working external IPs found');
69-
return null;
47+
// Just return the first external IP we find
48+
const workingIp = externalIps[0];
49+
console.log(`Using external IP: ${workingIp}`);
50+
return workingIp;
7051

7152
} catch (error) {
72-
console.error('Error querying Kubernetes:', error);
53+
console.error('Error querying Kubernetes nodes:', error);
7354
return null;
7455
}
7556
}
7657

77-
/**
78-
* Test if an IP is reachable
79-
*/
80-
private async testIpConnectivity(ip: string): Promise<boolean> {
81-
try {
82-
// Try to connect to port 80 (HTTP) as a basic connectivity test
83-
const response = await fetch(`http://${ip}:80`, {
84-
method: 'HEAD',
85-
signal: AbortSignal.timeout(3000) // 3 second timeout
86-
});
87-
return true;
88-
} catch (error) {
89-
console.log(`IP ${ip} is not reachable:`, error instanceof Error ? error.message : 'Unknown error');
90-
return false;
91-
}
92-
}
58+
9359

9460
/**
9561
* Get external IPs for a specific service
@@ -121,4 +87,43 @@ export class KubernetesService {
12187
return [];
12288
}
12389
}
90+
91+
/**
92+
* Get all node external IPs
93+
*/
94+
async getNodeExternalIps(): Promise<string[]> {
95+
try {
96+
const nodesResponse = await this.coreV1Api.listNode();
97+
const externalIps: string[] = [];
98+
99+
for (const node of nodesResponse.body.items) {
100+
if (node.status?.addresses) {
101+
for (const address of node.status.addresses) {
102+
if (address.type === 'ExternalIP' && address.address) {
103+
externalIps.push(address.address);
104+
}
105+
}
106+
}
107+
}
108+
109+
return externalIps;
110+
111+
} catch (error) {
112+
console.error('Error getting node external IPs:', error);
113+
return [];
114+
}
115+
}
116+
117+
/**
118+
* Debug method to show node external IPs
119+
*/
120+
async debugExternalIps(): Promise<{
121+
nodeExternalIps: string[];
122+
}> {
123+
const nodeExternalIps = await this.getNodeExternalIps();
124+
125+
return {
126+
nodeExternalIps,
127+
};
128+
}
124129
}

0 commit comments

Comments
 (0)