-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathserver.go
More file actions
374 lines (338 loc) · 8.96 KB
/
server.go
File metadata and controls
374 lines (338 loc) · 8.96 KB
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
package tap
import (
"context"
"fmt"
"io"
"net"
"strings"
"time"
common "github.com/runconduit/conduit/controller/gen/common"
pb "github.com/runconduit/conduit/controller/gen/controller/tap"
proxy "github.com/runconduit/conduit/controller/gen/proxy/tap"
public "github.com/runconduit/conduit/controller/gen/public"
"github.com/runconduit/conduit/controller/k8s"
"github.com/runconduit/conduit/controller/util"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/api/core/v1"
)
var tapInterval = 10 * time.Second
type (
server struct {
tapPort uint
// We use the Kubernetes API to find the IP addresses of pods to tap
replicaSets *k8s.ReplicaSetStore
pods *k8s.PodIndex
}
)
func (s *server) Tap(req *public.TapRequest, stream pb.Tap_TapServer) error {
// TODO: Allow a configurable aperture A.
// If the target contains more than A pods, select A of them at random.
var pods []*v1.Pod
var targetName string
switch target := req.Target.(type) {
case *public.TapRequest_Pod:
targetName = target.Pod
pod, err := s.pods.GetPod(target.Pod)
if err != nil {
return status.Errorf(codes.NotFound, err.Error())
}
pods = []*v1.Pod{pod}
case *public.TapRequest_Deployment:
targetName = target.Deployment
var err error
pods, err = (*s.pods).GetPodsByIndex(target.Deployment)
if err != nil {
return err
}
}
log.Printf("Tapping %d pods for target %s", len(pods), targetName)
events := make(chan *common.TapEvent)
go func() { // Stop sending back events if the request is cancelled
<-stream.Context().Done()
close(events)
}()
// divide the rps evenly between all pods to tap
rpsPerPod := req.MaxRps / float32(len(pods))
for _, pod := range pods {
// initiate a tap on the pod
match, err := makeMatch(req)
if err != nil {
return nil
}
go s.tapProxy(stream.Context(), rpsPerPod, match, pod.Status.PodIP, events)
}
// read events from the taps and send them back
for event := range events {
err := stream.Send(event)
if err != nil {
return err
}
}
return nil
}
func validatePort(port uint32) error {
if port > 65535 {
return fmt.Errorf("Port number of range: %d", port)
}
return nil
}
func makeMatch(req *public.TapRequest) (*proxy.ObserveRequest_Match, error) {
matches := make([]*proxy.ObserveRequest_Match, 0)
if req.FromIP != "" {
ip, err := util.ParseIPV4(req.FromIP)
if err != nil {
return nil, err
}
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Source{
Source: &proxy.ObserveRequest_Match_Tcp{
Match: &proxy.ObserveRequest_Match_Tcp_Netmask_{
Netmask: &proxy.ObserveRequest_Match_Tcp_Netmask{
Ip: ip,
Mask: 32,
},
},
},
},
})
}
if req.FromPort != 0 {
if err := validatePort(req.FromPort); err != nil {
return nil, err
}
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Source{
Source: &proxy.ObserveRequest_Match_Tcp{
Match: &proxy.ObserveRequest_Match_Tcp_Ports{
Ports: &proxy.ObserveRequest_Match_Tcp_PortRange{
Min: req.FromPort,
},
},
},
},
})
}
if req.ToIP != "" {
ip, err := util.ParseIPV4(req.ToIP)
if err != nil {
return nil, err
}
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Destination{
Destination: &proxy.ObserveRequest_Match_Tcp{
Match: &proxy.ObserveRequest_Match_Tcp_Netmask_{
Netmask: &proxy.ObserveRequest_Match_Tcp_Netmask{
Ip: ip,
Mask: 32,
},
},
},
},
})
}
if req.ToPort != 0 {
if err := validatePort(req.ToPort); err != nil {
return nil, err
}
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Destination{
Destination: &proxy.ObserveRequest_Match_Tcp{
Match: &proxy.ObserveRequest_Match_Tcp_Ports{
Ports: &proxy.ObserveRequest_Match_Tcp_PortRange{
Min: req.ToPort,
},
},
},
},
})
}
if req.Scheme != "" {
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Http_{
Http: &proxy.ObserveRequest_Match_Http{
Match: &proxy.ObserveRequest_Match_Http_Scheme{
Scheme: parseScheme(req.Scheme),
},
},
},
})
}
if req.Method != "" {
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Http_{
Http: &proxy.ObserveRequest_Match_Http{
Match: &proxy.ObserveRequest_Match_Http_Method{
Method: parseMethod(req.Method),
},
},
},
})
}
// exact match
if req.Authority != "" {
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Http_{
Http: &proxy.ObserveRequest_Match_Http{
Match: &proxy.ObserveRequest_Match_Http_Authority{
Authority: &proxy.ObserveRequest_Match_Http_StringMatch{
Match: &proxy.ObserveRequest_Match_Http_StringMatch_Exact{
Exact: req.Authority,
},
},
},
},
},
})
}
// prefix match
if req.Path != "" {
matches = append(matches, &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_Http_{
Http: &proxy.ObserveRequest_Match_Http{
Match: &proxy.ObserveRequest_Match_Http_Path{
Path: &proxy.ObserveRequest_Match_Http_StringMatch{
Match: &proxy.ObserveRequest_Match_Http_StringMatch_Prefix{
Prefix: req.Path,
},
},
},
},
},
})
}
return &proxy.ObserveRequest_Match{
Match: &proxy.ObserveRequest_Match_All{
All: &proxy.ObserveRequest_Match_Seq{
Matches: matches,
},
},
}, nil
}
// TODO: validate scheme
func parseScheme(scheme string) *common.Scheme {
value, ok := common.Scheme_Registered_value[strings.ToUpper(scheme)]
if ok {
return &common.Scheme{
Type: &common.Scheme_Registered_{
Registered: common.Scheme_Registered(value),
},
}
}
return &common.Scheme{
Type: &common.Scheme_Unregistered{
Unregistered: strings.ToUpper(scheme),
},
}
}
// TODO: validate method
func parseMethod(method string) *common.HttpMethod {
value, ok := common.HttpMethod_Registered_value[strings.ToUpper(method)]
if ok {
return &common.HttpMethod{
Type: &common.HttpMethod_Registered_{
Registered: common.HttpMethod_Registered(value),
},
}
}
return &common.HttpMethod{
Type: &common.HttpMethod_Unregistered{
Unregistered: strings.ToUpper(method),
},
}
}
// Tap a pod.
// This method will run continuously until an error is encountered or the
// request is cancelled via the context. Thus it should be called as a
// go-routine.
// To limit the rps to maxRps, this method calls Observe on the pod with a limit
// of maxRps * 10s at most once per 10s window. If this limit is reached in
// less than 10s, we sleep until the end of the window before calling Observe
// again.
func (s *server) tapProxy(ctx context.Context, maxRps float32, match *proxy.ObserveRequest_Match, addr string, events chan *common.TapEvent) {
tapAddr := fmt.Sprintf("%s:%d", addr, s.tapPort)
log.Printf("Establishing tap on %s", tapAddr)
conn, err := grpc.DialContext(ctx, tapAddr, grpc.WithInsecure())
if err != nil {
log.Println(err)
return
}
client := proxy.NewTapClient(conn)
req := &proxy.ObserveRequest{
Limit: uint32(maxRps * float32(tapInterval.Seconds())),
Match: match,
}
for { // Request loop
windowStart := time.Now()
windowEnd := windowStart.Add(tapInterval)
rsp, err := client.Observe(ctx, req)
if err != nil {
log.Println(err)
return
}
for { // Stream loop
event, err := rsp.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Println(err)
return
}
events <- event
}
if time.Now().Before(windowEnd) {
time.Sleep(time.Until(windowEnd))
}
}
}
func NewServer(addr string, tapPort uint, kubeconfig string) (*grpc.Server, net.Listener, error) {
clientSet, err := k8s.NewClientSet(kubeconfig)
if err != nil {
return nil, nil, err
}
replicaSets, err := k8s.NewReplicaSetStore(clientSet)
if err != nil {
return nil, nil, err
}
err = replicaSets.Run()
if err != nil {
return nil, nil, err
}
// index pods by deployment
deploymentIndex := func(obj interface{}) ([]string, error) {
pod, ok := obj.(*v1.Pod)
if !ok {
return nil, fmt.Errorf("object is not a Pod")
}
deployment, err := replicaSets.GetDeploymentForPod(pod)
if err != nil {
log.Debugf("Cannot get deployment for pod %s: %s", pod.Name, err)
return []string{}, nil
}
return []string{deployment}, nil
}
pods, err := k8s.NewPodIndex(clientSet, deploymentIndex)
if err != nil {
return nil, nil, err
}
err = pods.Run()
if err != nil {
return nil, nil, err
}
lis, err := net.Listen("tcp", addr)
if err != nil {
return nil, nil, err
}
s := util.NewGrpcServer()
srv := server{
tapPort: tapPort,
replicaSets: replicaSets,
pods: pods,
}
pb.RegisterTapServer(s, &srv)
// TODO: register shutdown hook to call pods.Stop() and replicatSets.Stop()
return s, lis, nil
}