@@ -369,6 +369,9 @@ fn is_kubernetes_service(endpoint: &str) -> bool {
369369///
370370/// List of pod endpoints (e.g., ["http://10.0.1.2:8080", "http://10.0.1.3:8080"])
371371async fn discover_k8s_service_endpoints ( service_endpoint : & str ) -> Result < Vec < String > , String > {
372+ use k8s_openapi:: api:: core:: v1:: Endpoints as K8sEndpoints ;
373+ use kube:: { Api , Client } ;
374+
372375 // Parse the service URL
373376 let url =
374377 url:: Url :: parse ( service_endpoint) . map_err ( |e| format ! ( "Invalid service URL: {}" , e) ) ?;
@@ -379,7 +382,7 @@ async fn discover_k8s_service_endpoints(service_endpoint: &str) -> Result<Vec<St
379382 let service_port = url
380383 . port_or_known_default ( )
381384 . ok_or_else ( || "No port in service URL" . to_string ( ) ) ?;
382- let _scheme = url. scheme ( ) ;
385+ let scheme = url. scheme ( ) ;
383386
384387 // Extract service name and namespace from hostname
385388 // Formats supported:
@@ -402,23 +405,58 @@ async fn discover_k8s_service_endpoints(service_endpoint: &str) -> Result<Vec<St
402405 "Discovering Kubernetes service endpoints"
403406 ) ;
404407
405- // In a real implementation, you would use the kube-rs library to query the Kubernetes API
406- // For now, we'll return a placeholder that falls back to the service URL
407- // TODO: Implement actual Kubernetes API integration using kube-rs
408+ // Create Kubernetes client
409+ let client = Client :: try_default ( )
410+ . await
411+ . map_err ( |e| format ! ( "Failed to create Kubernetes client: {}" , e) ) ?;
412+
413+ // Get the Endpoints resource for this service
414+ let endpoints_api: Api < K8sEndpoints > = Api :: namespaced ( client, namespace) ;
415+
416+ let endpoints = endpoints_api. get ( service_name) . await . map_err ( |e| {
417+ if e. to_string ( ) . contains ( "404" ) {
418+ format ! ( "Service {}.{} not found" , service_name, namespace)
419+ } else {
420+ format ! (
421+ "Failed to get endpoints for {}.{}: {}" ,
422+ service_name, namespace, e
423+ )
424+ }
425+ } ) ?;
426+
427+ // Extract pod IPs from the Endpoints resource
428+ let mut pod_endpoints = Vec :: new ( ) ;
429+
430+ if let Some ( subsets) = endpoints. subsets {
431+ for subset in subsets {
432+ // Only use ready addresses (healthy pods)
433+ if let Some ( addresses) = subset. addresses {
434+ for address in addresses {
435+ let pod_ip = & address. ip ;
436+ let endpoint_url = format ! ( "{}://{}:{}" , scheme, pod_ip, service_port) ;
437+ pod_endpoints. push ( endpoint_url) ;
438+ }
439+ }
440+ }
441+ }
408442
409- // Placeholder: In production, this would use kube-rs to:
410- // 1. Get the Endpoints resource for the service
411- // 2. Extract all pod IPs
412- // 3. Return list of "http://pod-ip:port" URLs
443+ if pod_endpoints. is_empty ( ) {
444+ warn ! (
445+ service_name = %service_name,
446+ namespace = %namespace,
447+ "No ready endpoints found for service - falling back to service URL"
448+ ) ;
449+ return Ok ( vec ! [ service_endpoint. to_string( ) ] ) ;
450+ }
413451
414- // For now, just return the original service endpoint as fallback
415- warn ! (
452+ info ! (
416453 service_name = %service_name,
417454 namespace = %namespace,
418- "Kubernetes service discovery not yet fully implemented - using service URL as fallback"
455+ endpoint_count = pod_endpoints. len( ) ,
456+ "Discovered Kubernetes service endpoints"
419457 ) ;
420458
421- Ok ( vec ! [ service_endpoint . to_string ( ) ] )
459+ Ok ( pod_endpoints )
422460}
423461
424462#[ cfg( test) ]
0 commit comments