...
1 package kclient
2
3 import (
4 "io"
5 "net/http"
6
7 corev1 "k8s.io/api/core/v1"
8 "k8s.io/client-go/tools/portforward"
9 "k8s.io/client-go/transport/spdy"
10 )
11
12 func (c *Client) SetupPortForwarding(pod *corev1.Pod, portPairs []string, out io.Writer, errOut io.Writer, stopChan chan struct{}, address string) error {
13 if address == "" {
14 address = "localhost"
15 }
16 transport, upgrader, err := spdy.RoundTripperFor(c.GetClientConfig())
17 if err != nil {
18 return err
19 }
20
21 req := c.GeneratePortForwardReq(pod.Name)
22
23 dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
24
25
26 fw, err := portforward.NewOnAddresses(dialer, []string{address}, portPairs, stopChan, nil, out, errOut)
27 if err != nil {
28 return err
29 }
30
31
32 err = fw.ForwardPorts()
33 if err != nil {
34
35
36 return err
37 }
38
39 return nil
40 }
41
View as plain text