-
Notifications
You must be signed in to change notification settings - Fork 90
/
portforward.go
483 lines (414 loc) · 14.6 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package k8sutil
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"time"
"github.com/phayes/freeport"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/auth"
"github.com/replicatedhq/kots/pkg/logger"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/httpstream"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
)
func IsPortAvailable(clientset kubernetes.Interface, port int) (bool, error) {
// kube-proxy stopped opening ports on the host for NodePort services in recent versions of kubernetes,
// so net.Listen won't be able to detect if the port is used by a NodePort service or not, and port forwarding might hang or redirect to the wrong component.
// https://github.com/kubernetes/kubernetes/pull/108496
// so we check if the port is used by a NodePort service.
services, err := clientset.CoreV1().Services("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
// TODO: make this work with minimal RBAC
if !kuberneteserrors.IsForbidden(err) {
return false, errors.Wrap(err, "failed to list services")
}
}
for _, service := range services.Items {
if service.Spec.Type != corev1.ServiceTypeNodePort {
continue
}
for _, p := range service.Spec.Ports {
if p.NodePort > 0 && int(p.NodePort) == port {
return false, nil
}
}
}
// portforward explicitly listens on localhost
host := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
server, err := net.Listen("tcp4", host)
if err != nil {
return false, nil
}
server.Close()
return true, nil
}
func FindFreePort(clientset kubernetes.Interface) (int, error) {
return findFreePort(clientset, 10, 1)
}
func findFreePort(clientset kubernetes.Interface, maxAttempts int, currAttempt int) (int, error) {
if currAttempt > maxAttempts {
return 0, errors.New(fmt.Sprintf("Timed out after %d attempts", maxAttempts))
}
port, err := freeport.GetFreePort()
if err != nil {
return 0, errors.Wrap(err, "failed to get free port")
}
isPortAvailable, err := IsPortAvailable(clientset, port)
if err != nil {
return 0, errors.Wrap(err, "failed to check if port is available")
}
if !isPortAvailable {
return findFreePort(clientset, maxAttempts, currAttempt+1)
}
return port, nil
}
// PortForward starts a local port forward to a pod in the cluster
// if localport is set, it will attempt to use that port locally.
// always check the port number returned though, because a port conflict
// could cause a different port to be used
func PortForward(localPort int, remotePort int, namespace string, getPodName func() (string, error), pollForAdditionalPorts bool, stopCh <-chan struct{}, log *logger.CLILogger) (int, chan error, error) {
// This process is long lived, avoid creating too many clientsets here
// https://github.com/kubernetes/client-go/issues/803
clientset, err := GetClientset()
if err != nil {
return 0, nil, errors.Wrap(err, "failed to get clientset")
}
if localPort == 0 {
freePort, err := FindFreePort(clientset)
if err != nil {
return 0, nil, errors.Wrap(err, "failed to find free port")
}
localPort = freePort
} else {
isPortAvailable, err := IsPortAvailable(clientset, localPort)
if err != nil {
return 0, nil, errors.Wrap(err, "failed to check if port is available")
}
if !isPortAvailable {
freePort, err := FindFreePort(clientset)
if err != nil {
return 0, nil, errors.Wrap(err, "failed to find free port")
}
localPort = freePort
}
}
podName, err := getPodName()
if err != nil {
return 0, nil, errors.Wrap(err, "failed to get pod name for port forward")
}
// port forward
cfg, err := GetClusterConfig()
if err != nil {
return 0, nil, errors.Wrap(err, "failed to get cluster config")
}
dialer, err := createDialer(cfg, namespace, podName)
if err != nil {
return 0, nil, errors.Wrap(err, "failed to create dialer")
}
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 0, nil, errors.Wrap(err, "failed to create new portforward")
}
errChan := make(chan error, 2) // 2 go routines are writing to this channel
go func() {
for range readyChan {
}
if len(errOut.String()) != 0 {
errChan <- errors.Errorf("remote error: %s", errOut)
} else if len(out.String()) != 0 {
// fmt.Println(out.String())
}
}()
var forwardErr error
go func() {
for {
// Locks until stopChan is closed or connection to pod is lost.
// The main function may timeout before this returns an error
forwardErr = forwarder.ForwardPorts()
if !errors.Is(forwardErr, portforward.ErrLostConnectionToPod) {
errChan <- errors.Wrap(forwardErr, "forward ports")
} else {
// Connection to pod was lost or stopChan was closed
log.Info("lost connection to pod %s", podName)
success := false
delaySeconds := time.Duration(0)
for !success {
delaySeconds += 1
if delaySeconds > 5 {
delaySeconds = 5
}
time.Sleep(delaySeconds * time.Second)
log.Info("attempting to re-establish port-forward")
podName, err = getPodName()
if err != nil {
log.Error(errors.Wrap(err, "failed to get pod name"))
continue
}
dialer, err = createDialer(cfg, namespace, podName)
if err != nil {
log.Error(errors.Wrap(err, "failed to create dialer"))
continue
}
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 {
log.Error(errors.Wrap(err, "failed to create new portforward"))
continue
}
success = true
log.Info("re-established port-forward to %s", podName)
}
}
}
}()
// Block until the new service is responding, limited to (math) seconds
quickClient := &http.Client{
Timeout: time.Millisecond * 200,
}
start := time.Now()
for {
if forwardErr != nil {
return 0, nil, errors.Wrap(forwardErr, "failed to forward ports")
}
response, err := quickClient.Get(fmt.Sprintf("http://localhost:%d/healthz", localPort))
if err == nil && response.StatusCode == http.StatusOK {
break
}
if time.Now().Sub(start) > time.Duration(time.Second*10) {
if err == nil {
err = errors.Errorf("service responded with status %s", response.Status)
}
return 0, nil, errors.Wrap(err, "failed to query healthz")
}
time.Sleep(time.Millisecond * 100)
if quickClient.Timeout < time.Second {
quickClient.Timeout = quickClient.Timeout + time.Millisecond*100
}
}
if pollForAdditionalPorts {
type AdditionalPort struct {
ServiceName string `json:"serviceName"`
ServicePort int `json:"servicePort"`
LocalPort int `json:"localPort"`
}
forwardedAdditionalPorts := map[AdditionalPort]chan struct{}{}
keepPolling := true
go func() {
<-stopChan
keepPolling = false
}()
uri := fmt.Sprintf("http://localhost:%d/api/v1/kots/ports", localPort)
consecutiveErrorsLogged := struct {
read int
unmarshal int
}{
read: 0,
unmarshal: 0,
}
sleepTime := time.Second
go func() {
prevServiceForwardErrMap := map[string]error{}
for keepPolling {
time.Sleep(sleepTime)
sleepTime = time.Second * 5
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
log.Error(errors.Wrap(err, "failed to create request"))
continue
}
req.Header.Set("Accept", "application/json")
authSlug, err := auth.GetOrCreateAuthSlug(clientset, namespace)
if err != nil {
log.Error(errors.Wrap(err, "failed to get kotsadm auth slug"))
continue
}
req.Header.Add("Authorization", authSlug)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Info("failed execute http request while listing additional ports: %v", err)
continue
}
if resp.StatusCode != http.StatusOK {
// Don't log server side errors. This will happen when app has not been installed yet,
// for example, and relevant logs should be in the kotsadm pod.
continue
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
if consecutiveErrorsLogged.read == 0 {
log.Info("failed to read response body while listing additional ports: %v", err)
consecutiveErrorsLogged.read++
}
continue
}
consecutiveErrorsLogged.read = 0
response := struct {
DesiredAdditionalPorts []AdditionalPort `json:"ports"`
}{}
if err := json.Unmarshal(b, &response); err != nil {
if consecutiveErrorsLogged.unmarshal == 0 {
log.Info("failed to decode response while listing additional ports: %s", b)
consecutiveErrorsLogged.unmarshal++
}
continue
}
consecutiveErrorsLogged.unmarshal = 0
for _, desiredAdditionalPort := range response.DesiredAdditionalPorts {
alreadyForwarded := false
for forwardedAdditionalPort := range forwardedAdditionalPorts {
// Avoid port conflicts by only taking the first to claim a local port
if forwardedAdditionalPort.LocalPort == desiredAdditionalPort.LocalPort {
alreadyForwarded = true
break
}
}
if alreadyForwarded {
continue
}
serviceStopCh, err := ServiceForward(clientset, cfg, desiredAdditionalPort.LocalPort, desiredAdditionalPort.ServicePort, namespace, desiredAdditionalPort.ServiceName)
if err != nil {
prevServiceForwardErr := prevServiceForwardErrMap[desiredAdditionalPort.ServiceName]
if prevServiceForwardErr == nil || prevServiceForwardErr.Error() != err.Error() {
log.Error(errors.Wrap(err, fmt.Sprintf("failed to execute kubectl port-forward -n %s svc/%s %d:%d", namespace, desiredAdditionalPort.ServiceName, desiredAdditionalPort.LocalPort, desiredAdditionalPort.ServicePort)))
prevServiceForwardErrMap[desiredAdditionalPort.ServiceName] = err
}
continue // try again
}
if serviceStopCh == nil {
// we didn't do the port forwarding, probably because the pod isn't ready.
// try again next loop
// The API doesn't return ports that aren't ready, so this is possibly rbac?
err = errors.Errorf("failed to forward port. check that you have permission to run kubectl port-forward -n %s svc/%s %d:%d", namespace, desiredAdditionalPort.ServiceName, desiredAdditionalPort.LocalPort, desiredAdditionalPort.ServicePort)
prevServiceForwardErr := prevServiceForwardErrMap[desiredAdditionalPort.ServiceName]
if prevServiceForwardErr == nil || prevServiceForwardErr.Error() != err.Error() {
log.Error(err)
prevServiceForwardErrMap[desiredAdditionalPort.ServiceName] = err
}
continue // try again
}
forwardedAdditionalPorts[desiredAdditionalPort] = serviceStopCh
log.ActionWithoutSpinner("Go to http://localhost:%d to access the application", desiredAdditionalPort.LocalPort)
}
}
}()
}
return localPort, errChan, nil
}
func createDialer(cfg *rest.Config, namespace string, podName string) (httpstream.Dialer, error) {
roundTripper, upgrader, err := spdy.RoundTripperFor(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create roundtriper")
}
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", namespace, podName)
scheme := ""
hostIP := cfg.Host
u, err := url.Parse(cfg.Host)
if err != nil {
return nil, errors.Wrap(err, "failed to parse host")
}
if u.Scheme == "http" || u.Scheme == "https" {
scheme = u.Scheme
hostIP = u.Host
}
serverURL := url.URL{Scheme: scheme, Path: path, Host: hostIP}
return spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, &serverURL), nil
}
func ServiceForward(clientset *kubernetes.Clientset, cfg *rest.Config, localPort int, remotePort int, namespace string, serviceName string) (chan struct{}, error) {
isPortAvailable, err := IsPortAvailable(clientset, localPort)
if err != nil {
return nil, errors.Wrap(err, "failed to check if port is available")
}
if !isPortAvailable {
return nil, errors.Errorf("Unable to connect to cluster. There's another process using port %d.", localPort)
}
svc, err := clientset.CoreV1().Services(namespace).Get(context.TODO(), serviceName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrap(err, "failed to get service")
}
selector := labels.SelectorFromSet(svc.Spec.Selector)
podName, err := getFirstPod(clientset, namespace, selector.String())
if err != nil {
return nil, errors.Wrap(err, "failed to get first pod")
}
if podName == "" {
// not ready yet
return nil, nil
}
roundTripper, upgrader, err := spdy.RoundTripperFor(cfg)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", namespace, podName)
scheme := ""
hostIP := cfg.Host
u, err := url.Parse(cfg.Host)
if err != nil {
return nil, err
}
if u.Scheme == "http" || u.Scheme == "https" {
scheme = u.Scheme
hostIP = u.Host
}
serverURL := url.URL{Scheme: scheme, 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 {
}
if len(errOut.String()) != 0 {
panic(errOut.String())
} else if len(out.String()) != 0 {
// fmt.Println(out.String())
}
}()
go func() error {
if err = forwarder.ForwardPorts(); err != nil { // Locks until stopChan is closed.
panic(err)
}
return nil
}()
return stopChan, nil
}
func getFirstPod(clientset *kubernetes.Clientset, namespace string, selector string) (string, error) {
options := metav1.ListOptions{LabelSelector: selector}
podList, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), options)
if err != nil {
return "", errors.Wrap(err, "failed to list pods")
}
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
isNotReady := false
for _, status := range pod.Status.ContainerStatuses {
if !status.Ready {
isNotReady = true
}
}
if !isNotReady {
return pod.Name, nil
}
}
}
return "", nil
}