@@ -17,79 +17,45 @@ export class KubernetesService {
17
17
}
18
18
19
19
/**
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
22
21
*/
23
22
async getWorkingExternalIp ( ) : Promise < string | null > {
24
23
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...' ) ;
29
25
26
+ const nodesResponse = await this . coreV1Api . listNode ( ) ;
30
27
const externalIps : string [ ] = [ ] ;
31
28
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 } ` ) ;
39
35
}
40
36
}
41
37
}
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
- }
50
38
}
51
39
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' ) ;
57
42
return null ;
58
43
}
59
44
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 ) ;
67
46
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 ;
70
51
71
52
} catch ( error ) {
72
- console . error ( 'Error querying Kubernetes:' , error ) ;
53
+ console . error ( 'Error querying Kubernetes nodes :' , error ) ;
73
54
return null ;
74
55
}
75
56
}
76
57
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
+
93
59
94
60
/**
95
61
* Get external IPs for a specific service
@@ -121,4 +87,43 @@ export class KubernetesService {
121
87
return [ ] ;
122
88
}
123
89
}
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
+ }
124
129
}
0 commit comments