-
Notifications
You must be signed in to change notification settings - Fork 492
/
tunnel.go
294 lines (251 loc) · 6.8 KB
/
tunnel.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package kubernetes
import (
"context"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"time"
"github.com/juju/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/transport/spdy"
"github.com/juju/juju/caas/kubernetes/pod"
)
const (
// ForwardPortTimeout is the duration for waiting for a pod to be ready.
ForwardPortTimeout time.Duration = time.Minute * 10
)
// Tunnel represents an ssh like tunnel to a Kubernetes Pod or Service
type Tunnel struct {
client rest.Interface
config *rest.Config
Kind TunnelKind
LocalPort string
Namespace string
Out io.Writer
readyChan chan struct{}
RemotePort string
stopChan chan struct{}
Target string
}
type TunnelKind string
const (
TunnelKindPods = TunnelKind("pods")
TunnelKindServices = TunnelKind("services")
)
// Close disconnects a tunnel connection
func (t *Tunnel) Close() {
select {
case <-t.stopChan:
default:
close(t.stopChan)
}
}
// findSuitablePodForService when tunneling to a kubernetes service we need to
// introspection.
func (t *Tunnel) findSuitablePodForService() (*corev1.Pod, error) {
clientSet := kubernetes.New(t.client)
service, err := clientSet.CoreV1().Services(t.Namespace).
Get(context.TODO(), t.Target, meta.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, errors.NewNotFound(err, "can't find service "+t.Target)
} else if err != nil {
return nil, errors.Trace(err)
}
pods, err := clientSet.CoreV1().Pods(t.Namespace).
List(context.TODO(), meta.ListOptions{
LabelSelector: labels.SelectorFromSet(service.Spec.Selector).String(),
})
if err != nil {
return nil, errors.Trace(err)
}
podCount := len(pods.Items)
if podCount == 0 {
return nil, errors.NotFoundf("pods for service %s", t.Target)
} else if podCount == 1 {
return &pods.Items[0], nil
}
return &pods.Items[rand.Intn(podCount-1)], nil
}
func (t *Tunnel) ForwardPort() error {
if !t.IsValidTunnelKind() {
return fmt.Errorf("invalid tunnel kind %s", t.Kind)
}
ctx, cancelFunc := context.WithTimeout(context.Background(), ForwardPortTimeout)
defer cancelFunc()
podName := t.Target
if t.Kind == TunnelKindServices {
pod, err := t.findSuitablePodForService()
if err != nil {
return errors.Trace(err)
}
podName = pod.Name
}
err := t.waitForPodReady(ctx, podName)
if err != nil {
return errors.Annotatef(err, "waiting for pod %s to become ready for tunnel", podName)
}
u := t.client.Post().
Resource(string(TunnelKindPods)).
Namespace(t.Namespace).
Name(podName).
SubResource("portforward").URL()
transport, upgrader, err := spdy.RoundTripperFor(t.config)
if err != nil {
return err
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, u)
local, err := getAvailablePort()
if err != nil {
return fmt.Errorf("could not find an available port: %w", err)
}
t.LocalPort = local
ports := []string{fmt.Sprintf("%s:%s", t.LocalPort, t.RemotePort)}
pf, err := portforward.New(dialer, ports, t.stopChan, t.readyChan, t.Out, t.Out)
if err != nil {
return err
}
errChan := make(chan error, 1)
go func() {
errChan <- pf.ForwardPorts()
}()
select {
case <-ctx.Done():
close(t.stopChan)
return ctx.Err()
case err = <-errChan:
close(t.stopChan)
return fmt.Errorf("forwarding ports: %v", err)
case <-pf.Ready:
return nil
}
}
func getAvailablePort() (string, error) {
l, err := net.Listen("tcp", ":0")
if err != nil {
return "", err
}
defer l.Close()
_, p, err := net.SplitHostPort(l.Addr().String())
if err != nil {
return "", err
}
return p, err
}
// IsValidTunnelKind tests that the tunnel kind supplied to this tunnel is valid
func (t *Tunnel) IsValidTunnelKind() bool {
switch t.Kind {
case TunnelKindPods,
TunnelKindServices:
return true
}
return false
}
// NewTunnelForConfig constructs a new tunnel from the provided rest config
func NewTunnelForConfig(
c *rest.Config,
kind TunnelKind,
namespace,
target,
remotePort string) (*Tunnel, error) {
config := *c
gv := corev1.SchemeGroupVersion
config.GroupVersion = &gv
config.APIPath = "/api"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
client, err := rest.RESTClientFor(&config)
if err != nil {
return nil, fmt.Errorf("failed creating kubernetes rest client for tunnel: %w", err)
}
return NewTunnel(client, &config, kind, namespace, target, remotePort), nil
}
// NewTunnel constructs a new kubernetes tunnel for the provided information
func NewTunnel(
client rest.Interface,
c *rest.Config,
kind TunnelKind,
namespace,
target,
remotePort string) *Tunnel {
return &Tunnel{
client: client,
config: c,
Kind: kind,
Namespace: namespace,
Out: io.Discard,
readyChan: make(chan struct{}, 1),
RemotePort: remotePort,
stopChan: make(chan struct{}, 1),
Target: target,
}
}
// waitForPodReady waits for the provided pod name relative to this tunnels
// namespace to become fully ready in the pod conditions. This func will block
// until the pod is ready of the context dead line has fired.
func (t *Tunnel) waitForPodReady(ctx context.Context, podName string) error {
clientSet := kubernetes.New(t.client)
factory := informers.NewSharedInformerFactoryWithOptions(
clientSet,
time.Minute,
informers.WithNamespace(t.Namespace),
)
informer := factory.Core().V1().Pods().Informer()
stopChan := make(chan struct{})
eventChan := make(chan error)
defer close(stopChan)
defer close(eventChan)
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
objPod, valid := obj.(*corev1.Pod)
if !valid {
eventChan <- errors.New("expected valid pod for informer")
return
}
if objPod.Name == podName && pod.IsPodRunning(objPod) {
eventChan <- nil
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
objPod, valid := newObj.(*corev1.Pod)
if !valid {
eventChan <- errors.New("expected valid pod for informer")
return
}
if objPod.Name == podName && pod.IsPodRunning(objPod) {
eventChan <- nil
}
},
DeleteFunc: func(obj interface{}) {
pod, valid := obj.(*corev1.Pod)
if !valid {
eventChan <- errors.New("expected valid pod for informer")
return
}
if pod.Name == podName {
eventChan <- errors.Errorf("tunnel pod %s is being deleted", podName)
}
},
})
if err != nil {
return errors.Trace(err)
}
go informer.Run(stopChan)
select {
case <-ctx.Done():
return ctx.Err()
case err := <-eventChan:
return err
}
}