@@ -114,44 +114,19 @@ func VerifyServicePodsRunning(ctx context.Context, client *kubernetes.Clientset,
114114}
115115
116116// StartPortForward starts port forwarding to a service
117+ // The ports parameter should be in format "localPort:servicePort" (e.g., "8080:80")
118+ // This function forwards directly to the service, letting Kubernetes handle the routing
117119func StartPortForward (ctx context.Context , client * kubernetes.Clientset , restConfig * rest.Config , namespace , service , ports string , verbose bool ) error {
118- // Parse ports (e.g., "8080:80" -> local=8080, remote =80)
120+ // Parse ports (e.g., "8080:80" -> local=8080, service =80)
119121 portParts := strings .Split (ports , ":" )
120122 if len (portParts ) != 2 {
121- return fmt .Errorf ("invalid port format: %s (expected format: localPort:remotePort )" , ports )
123+ return fmt .Errorf ("invalid port format: %s (expected format: localPort:servicePort )" , ports )
122124 }
123125 localPort := portParts [0 ]
124- remotePort := portParts [1 ]
125-
126- // Get the service to find a pod
127- svc , err := client .CoreV1 ().Services (namespace ).Get (ctx , service , metav1.GetOptions {})
128- if err != nil {
129- return fmt .Errorf ("failed to get service: %w" , err )
130- }
131-
132- // Get pods matching the service selector
133- var selectorParts []string
134- for key , value := range svc .Spec .Selector {
135- selectorParts = append (selectorParts , fmt .Sprintf ("%s=%s" , key , value ))
136- }
137- labelSelector := strings .Join (selectorParts , "," )
138-
139- pods , err := client .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
140- LabelSelector : labelSelector ,
141- FieldSelector : "status.phase=Running" ,
142- })
143- if err != nil {
144- return fmt .Errorf ("failed to list pods: %w" , err )
145- }
146-
147- if len (pods .Items ) == 0 {
148- return fmt .Errorf ("no running pods found for service %s" , service )
149- }
150-
151- podName := pods .Items [0 ].Name
126+ servicePort := portParts [1 ]
152127
153128 if verbose {
154- fmt .Printf ("[Helper] Starting port-forward to pod %s/%s (%s:%s)\n " , namespace , podName , localPort , remotePort )
129+ fmt .Printf ("[Helper] Starting port-forward to service %s/%s (%s:%s)\n " , namespace , service , localPort , servicePort )
155130 }
156131
157132 // Create SPDY transport
@@ -160,11 +135,11 @@ func StartPortForward(ctx context.Context, client *kubernetes.Clientset, restCon
160135 return fmt .Errorf ("failed to create SPDY transport: %w" , err )
161136 }
162137
163- // Build the URL for port forwarding
138+ // Build the URL for port forwarding to service
164139 url := client .CoreV1 ().RESTClient ().Post ().
165- Resource ("pods " ).
140+ Resource ("services " ).
166141 Namespace (namespace ).
167- Name (podName ).
142+ Name (service ).
168143 SubResource ("portforward" ).
169144 URL ()
170145
@@ -182,7 +157,7 @@ func StartPortForward(ctx context.Context, client *kubernetes.Clientset, restCon
182157 }
183158
184159 // Create port forwarder
185- forwarder , err := portforward .New (dialer , []string {fmt .Sprintf ("%s:%s" , localPort , remotePort )}, stopChan , readyChan , out , errOut )
160+ forwarder , err := portforward .New (dialer , []string {fmt .Sprintf ("%s:%s" , localPort , servicePort )}, stopChan , readyChan , out , errOut )
186161 if err != nil {
187162 return fmt .Errorf ("failed to create port forwarder: %w" , err )
188163 }
0 commit comments