-
Notifications
You must be signed in to change notification settings - Fork 93
/
portforward.go
72 lines (59 loc) · 1.85 KB
/
portforward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package k8sutil
import (
"bytes"
"fmt"
"net/http"
"net/url"
"strings"
"time"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
func PortForward(config *restclient.Config, localPort int, remotePort int, namespace string, podName string) (chan struct{}, error) {
roundTripper, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", namespace, podName)
hostIP := strings.TrimLeft(config.Host, "htps:/")
serverURL := url.URL{Scheme: "http", Path: path, Host: hostIP}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, &serverURL)
stopChan, readyChan := make(chan struct{}, 1), make(chan struct{}, 1)
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
forwarder, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", localPort, remotePort)}, stopChan, readyChan, out, errOut)
if err != nil {
return nil, err
}
go func() {
for range readyChan { // Kubernetes will close this channel when it has something to tell us.
}
if errOut.String() != "" {
panic(errOut.String())
} else if out.String() != "" {
// fmt.Println(out.String())
}
}()
go func() error {
if err = forwarder.ForwardPorts(); err != nil { // Locks until stopChan is closed.
panic(err)
}
return nil
}()
// Block until the new service is responding, limited to (math) seconds
quickClient := &http.Client{
Timeout: time.Millisecond * 200,
}
start := time.Now()
for {
response, err := quickClient.Get(fmt.Sprintf("http://localhost:%d", localPort))
if err == nil && response.StatusCode == http.StatusOK {
break
}
if time.Now().Sub(start) > time.Duration(time.Second*5) {
return nil, err
}
time.Sleep(time.Millisecond * 100)
}
return stopChan, nil
}