-
Notifications
You must be signed in to change notification settings - Fork 0
/
port_forward.go
88 lines (76 loc) · 2.06 KB
/
port_forward.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package port_forward
import (
"fmt"
"io/ioutil"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"net/http"
"net/url"
"os"
)
func New(spec PortForwardingSpec) (PortForwarding, error) {
restConfig, err := clientcmd.BuildConfigFromFlags("", spec.KubeConfigPath)
if nil != err {
return PortForwarding{}, err
}
return PortForwarding{
restConfig: restConfig,
pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: spec.Pod.Name,
Namespace: spec.Pod.Namespace,
},
},
localPort: spec.Ports.Local,
podPort: spec.Ports.Remote,
stopCh: make(chan struct{}, 1),
readyCh: make(chan struct{}),
}, nil
}
func (self PortForwarding) getClusterHost() string {
parsed, err := url.Parse(self.restConfig.Host)
if nil != err {
// We consider that `restConfig' always contains a valid host name since the original config was
// parsed successfully before.
panic(err)
}
return parsed.Host
}
func (self PortForwarding) getPortForwardingApiPath() string {
return fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", self.pod.Namespace, self.pod.Name)
}
func (self PortForwarding) dial() (httpstream.Dialer, error) {
transport, upgrader, err := spdy.RoundTripperFor(self.restConfig)
if err != nil {
return nil, err
}
return spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost,
&url.URL{
Scheme: "https",
Path: self.getPortForwardingApiPath(),
Host: self.getClusterHost(),
},
), nil
}
func (self PortForwarding) Start() error {
dialer, err := self.dial()
if nil != err {
return err
}
ports := []string{fmt.Sprintf("%d:%d", self.localPort, self.podPort)}
fw, err := portforward.New(dialer, ports, self.stopCh, self.readyCh, ioutil.Discard, os.Stderr)
if err != nil {
return err
}
return fw.ForwardPorts()
}
func (self PortForwarding) WaitForReady() {
<-self.readyCh
}
func (self PortForwarding) Stop() {
close(self.stopCh)
}