Slide 23
Slide 23 text
Port forward programmatically
func PortForwardAPod(req PortForwardAPodRequest) error {
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward",
req.Pod.Namespace, req.Pod.Name)
hostIP := strings.TrimLeft(req.RestConfig.Host, "htps:/")
transport, upgrader, err := spdy.RoundTripperFor(req.RestConfig)
if err != nil {
return err
}
dialer := spdy.NewDialer(
upgrader,
&http.Client{Transport: transport},
http.MethodPost,
&url.URL{Scheme: "https", Path: path, Host: hostIP})
fw, err := portforward.New(
dialer,
[]string{fmt.Sprintf("%d:%d", req.LocalPort, req.PodPort)},
req.StopCh,
req.ReadyCh,
req.Streams.Out,
req.Streams.ErrOut)
if err != nil {
return err
}
return fw.ForwardPorts()
}
Repository: gianarb/kube-port-forward