/
portforward.go
212 lines (181 loc) · 5.35 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package utils
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
meshkitkube "github.com/khulnasoft/meshkit/utils/kubernetes"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
// PortForward provides a port-forward connection into a Kubernetes cluster.
type PortForward struct {
method string
url *url.URL
host string
namespace string
podName string
localPort int
remotePort int
emitLogs bool
stopCh chan struct{}
readyCh chan struct{}
config *rest.Config
}
// NewPortForward returns an instance of the PortForward struct that can be used
// to establish a port-forward connection to a pod in the deployment that's
// specified by namespace and deployName. If localPort is 0, it will use a
// random ephemeral port.
// Note that the connection remains open for the life of the process, as this
// function is typically called by the CLI.
func NewPortForward(
ctx context.Context,
client *meshkitkube.Client,
namespace, deployName string,
host string, localPort, remotePort int,
emitLogs bool,
) (*PortForward, error) {
timeoutSeconds := int64(30)
podList, err := client.KubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{TimeoutSeconds: &timeoutSeconds})
if err != nil {
return nil, err
}
podName := ""
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
if deployName == GetCleanPodName(pod.Name) {
podName = pod.Name
break
}
}
}
if podName == "" {
return nil, fmt.Errorf("no running pods found for %s", deployName)
}
return newPortForward(client, namespace, podName, host, localPort, remotePort, emitLogs)
}
func newPortForward(
client *meshkitkube.Client,
namespace, podName string,
host string, localPort, remotePort int,
emitLogs bool,
) (*PortForward, error) {
restClient := client.KubeClient.CoreV1().RESTClient()
req := restClient.Post().
Resource("pods").
Namespace(namespace).
Name(podName).
SubResource("portforward")
var err error
if localPort == 0 {
if host != "localhost" {
return nil, fmt.Errorf("local port must be specified when host is not localhost")
}
localPort, err = GetEphemeralPort()
if err != nil {
return nil, err
}
}
return &PortForward{
method: "POST",
url: req.URL(),
host: host,
namespace: namespace,
podName: podName,
localPort: localPort,
remotePort: remotePort,
emitLogs: emitLogs,
stopCh: make(chan struct{}, 1),
readyCh: make(chan struct{}),
config: &client.RestConfig,
}, nil
}
// run creates and runs the port-forward connection.
// When the connection is established it blocks until Stop() is called.
func (pf *PortForward) run() error {
transport, upgrader, err := spdy.RoundTripperFor(pf.config)
if err != nil {
return err
}
out := io.Discard
errOut := io.Discard
if pf.emitLogs {
out = os.Stdout
errOut = os.Stderr
}
ports := []string{fmt.Sprintf("%d:%d", pf.localPort, pf.remotePort)}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, pf.method, pf.url)
fw, err := portforward.NewOnAddresses(dialer, []string{pf.host}, ports, pf.stopCh, pf.readyCh, out, errOut)
if err != nil {
return err
}
err = fw.ForwardPorts()
if err != nil {
err = fmt.Errorf("%s for %s/%s", err, pf.namespace, pf.podName)
return err
}
return nil
}
// Init creates and runs a port-forward connection.
// This function blocks until the connection is established, in which case it returns nil.
// It's the caller's responsibility to call Stop() to finish the connection.
func (pf *PortForward) Init() error {
log.Debugf("Starting port forward to %s %d:%d", pf.url, pf.localPort, pf.remotePort)
failure := make(chan error)
go func() {
if err := pf.run(); err != nil {
failure <- err
}
}()
// The `select` statement below depends on one of two outcomes from `pf.run()`:
// 1) Succeed and block, causing a receive on `<-pf.readyCh`
// 2) Return an err, causing a receive `<-failure`
select {
case <-pf.readyCh:
log.Debug("Port forward initialized")
case err := <-failure:
log.Debugf("Port forward failed: %v", err)
return err
}
return nil
}
// Stop terminates the port-forward connection.
// It is the caller's responsibility to call Stop even in case of errors
func (pf *PortForward) Stop() {
close(pf.stopCh)
}
// GetStop returns the stopCh.
// Receiving on stopCh will block until the port forwarding stops.
func (pf *PortForward) GetStop() <-chan struct{} {
return pf.stopCh
}
// URLFor returns the URL for the port-forward connection.
func (pf *PortForward) URLFor(path string) string {
return fmt.Sprintf("http://%s:%d%s", pf.host, pf.localPort, path)
}
// AddressAndPort returns the address and port for the port-forward connection.
func (pf *PortForward) AddressAndPort() string {
return fmt.Sprintf("%s:%d", pf.host, pf.localPort)
}
// GetEphemeralPort selects a port for the port-forwarding. It binds to a free
// ephemeral port and returns the port number.
func GetEphemeralPort() (int, error) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
return 0, err
}
defer ln.Close()
// get port
tcpAddr, ok := ln.Addr().(*net.TCPAddr)
if !ok {
return 0, fmt.Errorf("invalid listen address: %s", ln.Addr())
}
return tcpAddr.Port, nil
}