-
Notifications
You must be signed in to change notification settings - Fork 2
/
portforward.go
178 lines (146 loc) · 3.49 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
package k8sapp
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/go-logr/logr"
"github.com/go-logr/zerologr"
"github.com/rs/zerolog"
corev1 "k8s.io/api/core/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"k8s.io/klog/v2"
)
type PortForwarder struct {
portForwarder *portforward.PortForwarder
stopChan chan struct{}
readyChan chan struct{}
errChan chan error
localPort int
}
func (p *PortForwarder) start() error {
go func() {
p.errChan <- p.portForwarder.ForwardPorts()
}()
readyTimer := time.NewTimer(5 * time.Second)
select {
case <-p.readyChan:
readyTimer.Stop()
ports, err := p.portForwarder.GetPorts()
if err != nil {
close(p.stopChan)
return err
}
for _, port := range ports {
p.localPort = int(port.Local)
return nil
}
close(p.stopChan)
return errors.New("No ports found for port forwarder")
case <-readyTimer.C:
close(p.stopChan)
return errors.New("Port forward failed to become ready in time")
}
}
type PortForward struct {
cfg *rest.Config
hostIP string
streams genericclioptions.IOStreams
}
type LogErrorWriter struct {
logger zerolog.Logger
}
func (l LogErrorWriter) Write(p []byte) (n int, err error) {
l.logger.Error().Msg(string(p))
return len(p), nil
}
func NewPortForward(cfg *rest.Config, logger zerolog.Logger) *PortForward {
errLog := LogErrorWriter{logger: logger}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
zerologr.NameFieldName = "logger"
zerologr.NameSeparator = "/"
zerologr.SetMaxV(1)
zl := logger
zl = zl.With().Caller().Timestamp().Logger()
var log logr.Logger = zerologr.New(&zl)
klog.LogToStderr(false)
klog.SetLogger(log)
pFwd := &PortForward{
cfg: cfg,
hostIP: strings.TrimLeft(cfg.Host, "htps:/"),
streams: genericiooptions.IOStreams{
In: os.Stdin,
Out: io.Discard,
ErrOut: errLog,
},
}
return pFwd
}
func (p *PortForward) ForwardPodPort(pod *corev1.Pod, podPort int) (int, func(), error) {
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", pod.Namespace, pod.Name)
podUrl := &url.URL{Scheme: "https", Path: path, Host: p.hostIP}
portMapping := fmt.Sprintf("%d:%d", 0, podPort)
pFwder, err := p.newPortForwarder(podUrl, portMapping)
if err != nil {
return 0, nil, err
}
err = pFwder.start()
if err != nil {
return 0, nil, err
}
go func() {
select {
case <-pFwder.stopChan:
break
case <-pFwder.errChan:
close(pFwder.stopChan)
break
}
}()
close := func() {
pFwder.stopChan <- struct{}{}
}
return pFwder.localPort, close, nil
}
func (p *PortForward) newPortForwarder(
fwdUrl *url.URL,
portMapping string,
) (*PortForwarder, error) {
transport, upgrader, err := spdy.RoundTripperFor(p.cfg)
if err != nil {
return nil, err
}
// Create the dialer for the pod URL
httpClient := &http.Client{Transport: transport}
dialer := spdy.NewDialer(upgrader, httpClient, http.MethodPost, fwdUrl)
stopChan := make(chan struct{}, 1)
readyChan := make(chan struct{})
errChan := make(chan error)
// Create the portforwarder itself
fw, err := portforward.New(
dialer,
[]string{portMapping},
stopChan,
readyChan,
p.streams.Out,
p.streams.ErrOut,
)
if err != nil {
return nil, err
}
pFwder := &PortForwarder{
portForwarder: fw,
stopChan: stopChan,
readyChan: readyChan,
errChan: errChan,
}
return pFwder, nil
}