func (*defaultPortForwarder) ForwardPorts(req *client.Request, config *client.Config, ports []string, stopChan <-chan struct{}) error { fw, err := portforward.New(req, config, ports, stopChan) if err != nil { return err } return fw.ForwardPorts() }
// StartForwardingToPod starts forwarding requests to the given pod on the given target port // If no localPort has been defined on the tunnel, a random available port will be assigned // The tunnel is started in the background (using a goroutine), and will need to be stopped with Stop() // It returns an error if it can't start the tunnel. func (tunnel *Tunnel) StartForwardingToPod(podName string, namespace string, targetPort int, restClient *kclientapi.RESTClient, clientConfig *kclientapi.Config) error { req := restClient.Post(). Resource("pods"). Namespace(namespace). Name(podName). SubResource("portforward") if tunnel.LocalPort == 0 { port, err := getRandomAvailableLocalPort() if err != nil { return err } tunnel.LocalPort = port } port := fmt.Sprintf("%v:%v", tunnel.LocalPort, targetPort) ports := []string{port} fw, err := portforward.New(req, clientConfig, ports, tunnel.stopChan) if err != nil { return err } go func(localPort int) { err = fw.ForwardPorts() if err != nil { fmt.Printf("Failed to forward localPort %v to remotePort %v on pod %s: %v\n", localPort, targetPort, podName, err) } }(tunnel.LocalPort) return nil }