github.com/argoproj/argo-events@v1.9.1/test/util/port_forward.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "os" 8 "strings" 9 10 "k8s.io/client-go/rest" 11 "k8s.io/client-go/tools/portforward" 12 "k8s.io/client-go/transport/spdy" 13 ) 14 15 func PodPortForward(config *rest.Config, namespace, podName string, localPort, remotePort int, stopCh <-chan struct{}) error { 16 readyCh := make(chan struct{}) 17 18 path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", 19 namespace, podName) 20 transport, upgrader, err := spdy.RoundTripperFor(config) 21 if err != nil { 22 return err 23 } 24 var scheme string 25 var host string 26 if strings.HasPrefix(config.Host, "https://") { 27 scheme = "https" 28 host = strings.TrimPrefix(config.Host, "https://") 29 } else { 30 scheme = "http" 31 host = strings.TrimPrefix(config.Host, "http://") 32 } 33 dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, &url.URL{Scheme: scheme, Path: path, Host: host}) 34 fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", localPort, remotePort)}, stopCh, readyCh, os.Stdout, os.Stderr) 35 if err != nil { 36 return err 37 } 38 39 go func() { 40 if err := fw.ForwardPorts(); err != nil { 41 panic(err) 42 } 43 }() 44 45 <-readyCh 46 47 return nil 48 }