-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
portforwarder.go
107 lines (91 loc) · 2.44 KB
/
portforwarder.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
package kube
import (
"bytes"
"context"
"fmt"
"net"
"net/http"
"os"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"github.com/argoproj/argo-cd/v2/util/io"
)
func PortForward(targetPort int, namespace string, overrides *clientcmd.ConfigOverrides, podSelectors ...string) (int, error) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig
clientConfig := clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, os.Stdin)
config, err := clientConfig.ClientConfig()
if err != nil {
return -1, err
}
if namespace == "" {
namespace, _, err = clientConfig.Namespace()
if err != nil {
return -1, err
}
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return -1, err
}
var pod *corev1.Pod
for _, podSelector := range podSelectors {
pods, err := clientSet.CoreV1().Pods(namespace).List(context.Background(), v1.ListOptions{
LabelSelector: podSelector,
})
if err != nil {
return -1, err
}
if len(pods.Items) > 0 {
pod = &pods.Items[0]
break
}
}
if pod == nil {
return -1, fmt.Errorf("cannot find pod with selector: %v", podSelectors)
}
url := clientSet.CoreV1().RESTClient().Post().
Resource("pods").
Namespace(pod.Namespace).
Name(pod.Name).
SubResource("portforward").URL()
transport, upgrader, err := spdy.RoundTripperFor(config)
if err != nil {
return -1, errors.Wrap(err, "Could not create round tripper")
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", url)
readyChan := make(chan struct{}, 1)
failedChan := make(chan error, 1)
out := new(bytes.Buffer)
errOut := new(bytes.Buffer)
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
return -1, err
}
port := ln.Addr().(*net.TCPAddr).Port
io.Close(ln)
forwarder, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", port, targetPort)}, context.Background().Done(), readyChan, out, errOut)
if err != nil {
return -1, err
}
go func() {
err = forwarder.ForwardPorts()
if err != nil {
failedChan <- err
}
}()
select {
case err = <-failedChan:
return -1, err
case <-readyChan:
}
if len(errOut.String()) != 0 {
return -1, fmt.Errorf(errOut.String())
}
return port, nil
}