-
Notifications
You must be signed in to change notification settings - Fork 0
/
adsc.go
758 lines (654 loc) · 17.5 KB
/
adsc.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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adsc
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"sync"
"time"
xdsapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
"github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
ads "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
types "github.com/gogo/protobuf/types"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
mcp "istio.io/api/mcp/v1alpha1"
)
// AdsConfig for the ADS connection - information and metadata about the client.
type AdsConfig struct {
// Namespace defaults to 'default'
Namespace string
// Workload defaults to 'test'
Workload string
// Meta includes additional metadata for the node
Meta map[string]string
// NodeType defaults to sidecar. "ingress" and "router" are also supported.
NodeType string
// IP is currently the primary key used to locate inbound configs. It is sent by client,
// must match a known endpoint IP. Tests can use a ServiceEntry to register fake IPs.
IP string
}
// ADSC implements a basic client for ADS and MCP, for use in stress tests and tools
// or libraries that need to connect to Istio pilot or other ADS or MCP servers.
//
type ADSC struct {
// Stream is the GRPC connection stream, allowing direct GRPC send operations.
// Set after Dial is called.
stream ads.AggregatedDiscoveryService_StreamAggregatedResourcesClient
mcpStream mcp.ResourceSource_EstablishResourceStreamClient
conn *grpc.ClientConn
// NodeID is the node identity sent to Pilot.
nodeID string
done chan error
certDir string
url string
watchTime time.Time
// InitialLoad tracks the time to receive the initial configuration.
InitialLoad time.Duration
// HTTPListeners contains received listeners with a http_connection_manager filter.
HTTPListeners map[string]*xdsapi.Listener
// TCPListeners contains all listeners of type TCP (not-HTTP)
TCPListeners map[string]*xdsapi.Listener
// All received clusters of type EDS, keyed by name
EDSClusters map[string]*xdsapi.Cluster
// All received clusters of no-EDS type, keyed by name
Clusters map[string]*xdsapi.Cluster
// All received routes, keyed by route name
Routes map[string]*xdsapi.RouteConfiguration
// All received endpoints, keyed by cluster name
EDS map[string]*xdsapi.ClusterLoadAssignment
// AdsConfig from MCP server. Key is the type.
Config map[string]*mcp.Resources
// DumpCfg will print all received config
DumpCfg bool
// Metadata has the node metadata to send to pilot.
// If nil, the defaults will be used.
Metadata map[string]string
// Updates includes the type of the last update received from the server.
Updates chan string
VersionInfo map[string]string
mutex sync.Mutex
}
const (
typePrefix = "type.googleapis.com/envoy.api.v2."
// Constants used for XDS
// ClusterType is used for cluster discovery. Typically first request received
clusterType = typePrefix + "Cluster"
// EndpointType is used for EDS and ADS endpoint discovery. Typically second request.
endpointType = typePrefix + "ClusterLoadAssignment"
// ListenerType is sent after clusters and endpoints.
listenerType = typePrefix + "Listener"
// RouteType is sent after listeners.
routeType = typePrefix + "RouteConfiguration"
)
var (
// ErrTimeout is returned by Wait if no update is received in the given time.
ErrTimeout = errors.New("timeout")
)
// Dial connects to a ADS server, with optional MTLS authentication if a cert dir is specified.
func Dial(url string, certDir string, opts *AdsConfig) (*ADSC, error) {
adsc := &ADSC{
done: make(chan error),
Updates: make(chan string, 100),
VersionInfo: map[string]string{},
certDir: certDir,
url: url,
}
if opts.Namespace == "" {
opts.Namespace = "default"
}
if opts.NodeType == "" {
opts.NodeType = "sidecar"
}
if opts.IP == "" {
opts.IP = getPrivateIPIfAvailable().String()
}
if opts.Workload == "" {
opts.Workload = "test-1"
}
adsc.Metadata = opts.Meta
adsc.nodeID = fmt.Sprintf("sidecar~%s~%s.%s~%s.svc.cluster.local", opts.IP,
opts.Workload, opts.Namespace, opts.Namespace)
err := adsc.Run()
return adsc, err
}
func DialMCP(url string, certDir string, opts *AdsConfig) (*ADSC, error) {
adsc := &ADSC{
done: make(chan error),
Updates: make(chan string, 100),
VersionInfo: map[string]string{},
certDir: certDir,
url: url,
}
if opts.Namespace == "" {
opts.Namespace = "default"
}
if opts.NodeType == "" {
opts.NodeType = "sidecar"
}
if opts.IP == "" {
opts.IP = getPrivateIPIfAvailable().String()
}
if opts.Workload == "" {
opts.Workload = "test-1"
}
adsc.Metadata = opts.Meta
adsc.nodeID = fmt.Sprintf("sidecar~%s~%s.%s~%s.svc.cluster.local", opts.IP,
opts.Workload, opts.Namespace, opts.Namespace)
err := adsc.RunMCP()
return adsc, err
}
// Returns a private IP address, or unspecified IP (0.0.0.0) if no IP is available
func getPrivateIPIfAvailable() net.IP {
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
default:
continue
}
if !ip.IsLoopback() {
return ip
}
}
return net.IPv4zero
}
func tlsConfig(certDir string) (*tls.Config, error) {
clientCert, err := tls.LoadX509KeyPair(certDir+"/cert-chain.pem",
certDir+"/key.pem")
if err != nil {
return nil, err
}
serverCABytes, err := ioutil.ReadFile(certDir + "/root-cert.pem")
if err != nil {
return nil, err
}
serverCAs := x509.NewCertPool()
if ok := serverCAs.AppendCertsFromPEM(serverCABytes); !ok {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: serverCAs,
ServerName: "istio-pilot.istio-system",
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return nil
},
}, nil
}
// Close the stream.
func (a *ADSC) Close() {
a.mutex.Lock()
if a.stream != nil {
a.stream.CloseSend()
}
a.conn.Close()
a.mutex.Unlock()
}
// Run will run the ADS client.
func (a *ADSC) Run() error {
// TODO: pass version info, nonce properly
err := a.dial()
if err != nil {
return err
}
xds := ads.NewAggregatedDiscoveryServiceClient(a.conn)
edsstr, err := xds.StreamAggregatedResources(context.Background())
if err != nil {
return err
}
a.stream = edsstr
go a.handleRecv()
return nil
}
func (a *ADSC) dial() error {
var err error
if len(a.certDir) > 0 {
tlsCfg, err := tlsConfig(a.certDir)
if err != nil {
return err
}
creds := credentials.NewTLS(tlsCfg)
opts := []grpc.DialOption{
// Verify Pilot cert and service account
grpc.WithTransportCredentials(creds),
}
a.conn, err = grpc.Dial(a.url, opts...)
if err != nil {
return err
}
} else {
a.conn, err = grpc.Dial(a.url, grpc.WithInsecure())
if err != nil {
return err
}
}
return err
}
// Run will run the ADS client.
func (a *ADSC) RunMCP() error {
// TODO: pass version info, nonce properly
err := a.dial()
if err != nil {
return err
}
xds := mcp.NewResourceSourceClient(a.conn)
edsstr, err := xds.EstablishResourceStream(context.Background())
if err != nil {
return err
}
a.mcpStream = edsstr
go a.handleRecvMCP()
return nil
}
func (a *ADSC) update(m string) {
select {
case a.Updates <- m:
default:
}
}
func (a *ADSC) handleRecv() {
for {
msg, err := a.stream.Recv()
if err != nil {
log.Println("Connection closed ", err, a.nodeID)
a.Close()
a.WaitClear()
a.Updates <- "close"
return
}
listeners := []*xdsapi.Listener{}
clusters := []*xdsapi.Cluster{}
routes := []*xdsapi.RouteConfiguration{}
eds := []*xdsapi.ClusterLoadAssignment{}
for _, rsc := range msg.Resources { // Any
a.VersionInfo[rsc.TypeUrl] = msg.VersionInfo
valBytes := rsc.Value
if rsc.TypeUrl == listenerType {
ll := &xdsapi.Listener{}
proto.Unmarshal(valBytes, ll)
listeners = append(listeners, ll)
} else if rsc.TypeUrl == clusterType {
ll := &xdsapi.Cluster{}
proto.Unmarshal(valBytes, ll)
clusters = append(clusters, ll)
} else if rsc.TypeUrl == endpointType {
ll := &xdsapi.ClusterLoadAssignment{}
proto.Unmarshal(valBytes, ll)
eds = append(eds, ll)
} else if rsc.TypeUrl == routeType {
ll := &xdsapi.RouteConfiguration{}
proto.Unmarshal(valBytes, ll)
routes = append(routes, ll)
}
}
// TODO: add hook to inject nacks
a.mutex.Lock()
a.ack(msg)
a.mutex.Unlock()
if len(listeners) > 0 {
a.handleLDS(listeners)
}
if len(clusters) > 0 {
a.handleCDS(clusters)
}
if len(eds) > 0 {
a.handleEDS(eds)
}
if len(routes) > 0 {
a.handleRDS(routes)
}
}
}
func (a *ADSC) handleRecvMCP() {
for {
msg, err := a.mcpStream.Recv()
if err != nil {
log.Println("Connection closed ", err, a.nodeID)
a.Close()
a.WaitClear()
a.Updates <- "close"
return
}
a.VersionInfo[msg.Collection] = msg.SystemVersionInfo
// TODO: add hook to inject nacks
a.mutex.Lock()
a.ackMCP(msg)
a.mutex.Unlock()
}
}
func (a *ADSC) handleLDS(ll []*xdsapi.Listener) {
lh := map[string]*xdsapi.Listener{}
lt := map[string]*xdsapi.Listener{}
clusters := []string{}
routes := []string{}
ldsSize := 0
for _, l := range ll {
ldsSize += l.Size()
f0 := l.FilterChains[0].Filters[0]
if f0.Name == "mixer" {
f0 = l.FilterChains[0].Filters[1]
}
if f0.Name == "envoy.tcp_proxy" {
lt[l.Name] = l
c := f0.GetConfig().Fields["cluster"].GetStringValue()
clusters = append(clusters, c)
//log.Printf("TCP: %s -> %s", l.Name, c)
} else if f0.Name == "envoy.http_connection_manager" {
lh[l.Name] = l
// Getting from config is too painful..
port := l.Address.GetSocketAddress().GetPortValue()
if port == 15002 {
routes = append(routes, "http_proxy")
} else {
routes = append(routes, fmt.Sprintf("%d", port))
}
//log.Printf("HTTP: %s -> %d", l.Name, port)
} else if f0.Name == "envoy.mongo_proxy" {
// ignore for now
} else if f0.Name == "envoy.redis_proxy" {
// ignore for now
} else if f0.Name == "envoy.filters.network.mysql_proxy" {
// ignore for now
} else {
tm := &jsonpb.Marshaler{Indent: " "}
log.Println(tm.MarshalToString(l))
}
}
log.Println("LDS: http=", len(lh), "tcp=", len(lt), "size=", ldsSize)
if a.DumpCfg {
b, _ := json.MarshalIndent(ll, " ", " ")
log.Println(string(b))
}
a.mutex.Lock()
defer a.mutex.Unlock()
if len(routes) > 0 {
a.sendRsc(routeType, routes)
}
a.HTTPListeners = lh
a.TCPListeners = lt
select {
case a.Updates <- "lds":
default:
}
}
// compact representations, for simplified debugging/testing
// TCPListener extracts the core elements from envoy Listener.
type TCPListener struct {
// Address is the address, as expected by go Dial and Listen, including port
Address string
// LogFile is the access log address for the listener
LogFile string
// Target is the destination cluster.
Target string
}
type Target struct {
// Address is a go address, extracted from the mangled cluster name.
Address string
// Endpoints are the resolved endpoints from EDS or cluster static.
Endpoints map[string]Endpoint
}
type Endpoint struct {
// Weight extracted from EDS
Weight int
}
// Save will save the json configs to files, using the base directory
func (a *ADSC) Save(base string) error {
strResponse, err := json.MarshalIndent(a.TCPListeners, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_lds_tcp.json", strResponse, 0644)
if err != nil {
return err
}
strResponse, err = json.MarshalIndent(a.HTTPListeners, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_lds_http.json", strResponse, 0644)
if err != nil {
return err
}
strResponse, err = json.MarshalIndent(a.Routes, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_rds.json", strResponse, 0644)
if err != nil {
return err
}
strResponse, err = json.MarshalIndent(a.EDSClusters, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_ecds.json", strResponse, 0644)
if err != nil {
return err
}
strResponse, err = json.MarshalIndent(a.Clusters, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_cds.json", strResponse, 0644)
if err != nil {
return err
}
strResponse, err = json.MarshalIndent(a.EDS, " ", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(base+"_eds.json", strResponse, 0644)
if err != nil {
return err
}
return err
}
func (a *ADSC) handleCDS(ll []*xdsapi.Cluster) {
cn := []string{}
cdsSize := 0
edscds := map[string]*xdsapi.Cluster{}
cds := map[string]*xdsapi.Cluster{}
for _, c := range ll {
cdsSize += c.Size()
if !c.ClusterDiscoveryType.Equal(xdsapi.Cluster_EDS) {
cds[c.Name] = c
continue
}
cn = append(cn, c.Name)
edscds[c.Name] = c
}
log.Println("CDS: ", len(cn), "size=", cdsSize)
if len(cn) > 0 {
a.sendRsc(endpointType, cn)
}
if a.DumpCfg {
b, _ := json.MarshalIndent(ll, " ", " ")
log.Println(string(b))
}
a.mutex.Lock()
defer a.mutex.Unlock()
a.EDSClusters = edscds
a.Clusters = cds
select {
case a.Updates <- "cds":
default:
}
}
func (a *ADSC) node() *core.Node {
n := &core.Node{
Id: a.nodeID,
}
if a.Metadata == nil {
n.Metadata = &types.Struct{
Fields: map[string]*types.Value{
"ISTIO_PROXY_VERSION": &types.Value{Kind: &types.Value_StringValue{StringValue: "1.0"}},
}}
} else {
f := map[string]*types.Value{}
for k, v := range a.Metadata {
f[k] = &types.Value{Kind: &types.Value_StringValue{StringValue: v}}
}
n.Metadata = &types.Struct{
Fields: f,
}
}
return n
}
func (a *ADSC) Send(req *xdsapi.DiscoveryRequest) error {
req.Node = a.node()
req.ResponseNonce = time.Now().String()
return a.stream.Send(req)
}
func (a *ADSC) handleEDS(eds []*xdsapi.ClusterLoadAssignment) {
la := map[string]*xdsapi.ClusterLoadAssignment{}
edsSize := 0
ep := 0
for _, cla := range eds {
edsSize += cla.Size()
la[cla.ClusterName] = cla
ep += len(cla.Endpoints)
}
log.Println("EDS: ", len(eds), "size=", edsSize, "ep=", ep)
if a.DumpCfg {
b, _ := json.MarshalIndent(eds, " ", " ")
log.Println(string(b))
}
if a.InitialLoad == 0 {
// first load - Envoy loads listeners after endpoints
a.stream.Send(&xdsapi.DiscoveryRequest{
ResponseNonce: time.Now().String(),
Node: a.node(),
TypeUrl: listenerType,
})
}
a.mutex.Lock()
defer a.mutex.Unlock()
a.EDS = la
select {
case a.Updates <- "eds":
default:
}
}
func (a *ADSC) handleRDS(configurations []*xdsapi.RouteConfiguration) {
vh := 0
rcount := 0
size := 0
httpClusters := []string{}
rds := map[string]*xdsapi.RouteConfiguration{}
for _, r := range configurations {
for _, h := range r.VirtualHosts {
vh++
for _, rt := range h.Routes {
rcount++
// Example: match:<prefix:"/" > route:<cluster:"outbound|9154||load-se-154.local" ...
log.Println(h.Name, rt.Match.PathSpecifier, rt.GetRoute().GetCluster())
httpClusters = append(httpClusters, rt.GetRoute().GetCluster())
}
}
rds[r.Name] = r
size += r.Size()
}
if a.InitialLoad == 0 {
a.InitialLoad = time.Since(a.watchTime)
log.Println("RDS: ", len(configurations), "size=", size, "vhosts=", vh, "routes=", rcount, " time=", a.InitialLoad)
} else {
log.Println("RDS: ", len(configurations), "size=", size, "vhosts=", vh, "routes=", rcount)
}
if a.DumpCfg {
b, _ := json.MarshalIndent(configurations, " ", " ")
log.Println(string(b))
}
a.mutex.Lock()
a.Routes = rds
a.mutex.Unlock()
select {
case a.Updates <- "rds":
default:
}
}
// WaitClear will clear the waiting events, so next call to Wait will get
// the next push type.
func (a *ADSC) WaitClear() {
for {
select {
case <-a.Updates:
default:
return
}
}
}
// Wait for an update of the specified type. If type is empty, wait for next update.
func (a *ADSC) Wait(update string, to time.Duration) (string, error) {
t := time.NewTimer(to)
for {
select {
case t := <-a.Updates:
if len(update) == 0 || update == t {
return t, nil
}
case <-t.C:
return "", ErrTimeout
}
}
}
// EndpointsJSON returns the endpoints, formatted as JSON, for debugging.
func (a *ADSC) EndpointsJSON() string {
out, _ := json.MarshalIndent(a.EDS, " ", " ")
return string(out)
}
// Watch will start watching resources, starting with LDS. Based on the LDS response
// it will start watching RDS and CDS.
func (a *ADSC) Watch() {
a.watchTime = time.Now()
a.stream.Send(&xdsapi.DiscoveryRequest{
ResponseNonce: time.Now().String(),
Node: a.node(),
TypeUrl: clusterType,
})
}
func (a *ADSC) sendRsc(typeurl string, rsc []string) {
a.stream.Send(&xdsapi.DiscoveryRequest{
ResponseNonce: "",
Node: a.node(),
TypeUrl: typeurl,
ResourceNames: rsc,
})
}
func (a *ADSC) ack(msg *xdsapi.DiscoveryResponse) {
a.stream.Send(&xdsapi.DiscoveryRequest{
ResponseNonce: msg.Nonce,
TypeUrl: msg.TypeUrl,
Node: a.node(),
VersionInfo: msg.VersionInfo,
})
}
func (a *ADSC) ackMCP(msg *mcp.Resources) {
a.mcpStream.Send(&mcp.RequestResources{
ResponseNonce: msg.Nonce,
})
}