forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
758 lines (694 loc) · 22.8 KB
/
debug.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 2017 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 v2
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
"github.com/gogo/protobuf/jsonpb"
authn "istio.io/api/authentication/v1alpha1"
meshconfig "istio.io/api/mesh/v1alpha1"
networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/model"
networking_core "istio.io/istio/pilot/pkg/networking/core/v1alpha3"
authn_plugin "istio.io/istio/pilot/pkg/networking/plugin/authn"
"istio.io/istio/pilot/pkg/serviceregistry"
"istio.io/istio/pilot/pkg/serviceregistry/aggregate"
)
// memregistry is based on mock/discovery - it is used for testing and debugging v2.
// In future (post 1.0) it may be used for representing remote pilots.
// InitDebug initializes the debug handlers and adds a debug in-memory registry.
func (s *DiscoveryServer) InitDebug(mux *http.ServeMux, sctl *aggregate.Controller) {
// For debugging and load testing v2 we add an memory registry.
s.MemRegistry = NewMemServiceDiscovery(
map[model.Hostname]*model.Service{ // mock.HelloService.Hostname: mock.HelloService,
}, 2)
sctl.AddRegistry(aggregate.Registry{
ClusterID: "v2-debug",
Name: serviceregistry.ServiceRegistry("memAdapter"),
ServiceDiscovery: s.MemRegistry,
ServiceAccounts: s.MemRegistry,
Controller: s.MemRegistry.controller,
})
mux.HandleFunc("/debug/edsz", edsz)
mux.HandleFunc("/debug/adsz", adsz)
mux.HandleFunc("/debug/cdsz", cdsz)
mux.HandleFunc("/debug/syncz", Syncz)
mux.HandleFunc("/debug/registryz", s.registryz)
mux.HandleFunc("/debug/endpointz", s.endpointz)
mux.HandleFunc("/debug/configz", s.configz)
mux.HandleFunc("/debug/authenticationz", s.authenticationz)
mux.HandleFunc("/debug/config_dump", ConfigDump)
}
// NewMemServiceDiscovery builds an in-memory MemServiceDiscovery
func NewMemServiceDiscovery(services map[model.Hostname]*model.Service, versions int) *MemServiceDiscovery {
return &MemServiceDiscovery{
services: services,
versions: versions,
controller: &memServiceController{},
instancesByPortNum: map[string][]*model.ServiceInstance{},
instancesByPortName: map[string][]*model.ServiceInstance{},
ip2instance: map[string][]*model.ServiceInstance{},
}
}
// SyncStatus is the synchronization status between Pilot and a given Envoy
type SyncStatus struct {
ProxyID string `json:"proxy,omitempty"`
ClusterSent string `json:"cluster_sent,omitempty"`
ClusterAcked string `json:"cluster_acked,omitempty"`
ListenerSent string `json:"listener_sent,omitempty"`
ListenerAcked string `json:"listener_acked,omitempty"`
RouteSent string `json:"route_sent,omitempty"`
RouteAcked string `json:"route_acked,omitempty"`
EndpointSent string `json:"endpoint_sent,omitempty"`
EndpointAcked string `json:"endpoint_acked,omitempty"`
EndpointPercent int `json:"endpoint_percent,omitempty"`
}
// Syncz dumps the synchronization status of all Envoys connected to this Pilot instance
func Syncz(w http.ResponseWriter, req *http.Request) {
adsClientsMutex.RLock()
defer adsClientsMutex.RUnlock()
syncz := []SyncStatus{}
adsClientsMutex.RLock()
for _, con := range adsClients {
con.mu.RLock()
if con.modelNode != nil {
syncz = append(syncz, SyncStatus{
ProxyID: con.modelNode.ID,
ClusterSent: con.ClusterNonceSent,
ClusterAcked: con.ClusterNonceAcked,
ListenerSent: con.ListenerNonceSent,
ListenerAcked: con.ListenerNonceAcked,
RouteSent: con.RouteNonceSent,
RouteAcked: con.RouteNonceAcked,
EndpointSent: con.EndpointNonceSent,
EndpointAcked: con.EndpointNonceAcked,
EndpointPercent: con.EndpointPercent,
})
}
con.mu.RUnlock()
}
adsClientsMutex.RUnlock()
out, err := json.MarshalIndent(&syncz, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "unable to marshal syncz information: %v", err)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(out)
w.WriteHeader(http.StatusOK)
}
// TODO: the mock was used for test setup, has no mutex. This will also be used for
// integration and load tests, will need to add mutex as we cleanup the code.
type memServiceController struct {
svcHandlers []func(*model.Service, model.Event)
instHandlers []func(*model.ServiceInstance, model.Event)
}
func (c *memServiceController) AppendServiceHandler(f func(*model.Service, model.Event)) error {
c.svcHandlers = append(c.svcHandlers, f)
return nil
}
func (c *memServiceController) AppendInstanceHandler(f func(*model.ServiceInstance, model.Event)) error {
c.instHandlers = append(c.instHandlers, f)
return nil
}
func (c *memServiceController) Run(<-chan struct{}) {}
// MemServiceDiscovery is a mock discovery interface
type MemServiceDiscovery struct {
services map[model.Hostname]*model.Service
// Endpoints table. Key is the fqdn of the service, ':', port
instancesByPortNum map[string][]*model.ServiceInstance
instancesByPortName map[string][]*model.ServiceInstance
ip2instance map[string][]*model.ServiceInstance
versions int
WantGetProxyServiceInstances []*model.ServiceInstance
ServicesError error
GetServiceError error
InstancesError error
GetProxyServiceInstancesError error
controller model.Controller
// Single mutex for now - it's for debug only.
mutex sync.Mutex
}
// ClearErrors clear errors used for mocking failures during model.MemServiceDiscovery interface methods
func (sd *MemServiceDiscovery) ClearErrors() {
sd.ServicesError = nil
sd.GetServiceError = nil
sd.InstancesError = nil
sd.GetProxyServiceInstancesError = nil
}
// AddService adds an in-memory service.
func (sd *MemServiceDiscovery) AddService(name model.Hostname, svc *model.Service) {
sd.mutex.Lock()
sd.services[name] = svc
sd.mutex.Unlock()
// TODO: notify listeners
}
// AddInstance adds an in-memory instance.
func (sd *MemServiceDiscovery) AddInstance(service model.Hostname, instance *model.ServiceInstance) {
// WIP: add enough code to allow tests and load tests to work
sd.mutex.Lock()
defer sd.mutex.Unlock()
svc := sd.services[service]
if svc == nil {
return
}
instance.Service = svc
sd.ip2instance[instance.Endpoint.Address] = []*model.ServiceInstance{instance}
key := fmt.Sprintf("%s:%d", service, instance.Endpoint.ServicePort.Port)
instanceList := sd.instancesByPortNum[key]
sd.instancesByPortNum[key] = append(instanceList, instance)
key = fmt.Sprintf("%s:%s", service, instance.Endpoint.ServicePort.Name)
instanceList = sd.instancesByPortName[key]
sd.instancesByPortName[key] = append(instanceList, instance)
}
// AddEndpoint adds an endpoint to a service.
func (sd *MemServiceDiscovery) AddEndpoint(service model.Hostname, servicePortName string, servicePort int, address string, port int) *model.ServiceInstance {
instance := &model.ServiceInstance{
Endpoint: model.NetworkEndpoint{
Address: address,
Port: port,
ServicePort: &model.Port{
Name: servicePortName,
Port: servicePort,
Protocol: model.ProtocolHTTP,
},
},
}
sd.AddInstance(service, instance)
return instance
}
// Services implements discovery interface
// Each call to Services() should return a list of new *model.Service
func (sd *MemServiceDiscovery) Services() ([]*model.Service, error) {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if sd.ServicesError != nil {
return nil, sd.ServicesError
}
out := make([]*model.Service, 0, len(sd.services))
for _, service := range sd.services {
// Make a new service out of the existing one
newSvc := *service
out = append(out, &newSvc)
}
return out, sd.ServicesError
}
// GetService implements discovery interface
// Each call to GetService() should return a new *model.Service
func (sd *MemServiceDiscovery) GetService(hostname model.Hostname) (*model.Service, error) {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if sd.GetServiceError != nil {
return nil, sd.GetServiceError
}
val := sd.services[hostname]
if val == nil {
return nil, errors.New("missing service")
}
// Make a new service out of the existing one
newSvc := *val
return &newSvc, sd.GetServiceError
}
// GetServiceAttributes implements discovery interface.
func (sd *MemServiceDiscovery) GetServiceAttributes(hostname model.Hostname) (*model.ServiceAttributes, error) {
return &model.ServiceAttributes{
Name: hostname.String(),
Namespace: model.IstioDefaultConfigNamespace}, nil
}
// Instances filters the service instances by labels. This assumes single port, as is
// used by EDS/ADS.
func (sd *MemServiceDiscovery) Instances(hostname model.Hostname, ports []string,
labels model.LabelsCollection) ([]*model.ServiceInstance, error) {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if sd.InstancesError != nil {
return nil, sd.InstancesError
}
if len(ports) != 1 {
adsLog.Warna("Unexpected ports ", ports)
return nil, nil
}
key := hostname.String() + ":" + ports[0]
instances, ok := sd.instancesByPortName[key]
if !ok {
return nil, nil
}
return instances, nil
}
// InstancesByPort filters the service instances by labels. This assumes single port, as is
// used by EDS/ADS.
func (sd *MemServiceDiscovery) InstancesByPort(hostname model.Hostname, port int,
labels model.LabelsCollection) ([]*model.ServiceInstance, error) {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if sd.InstancesError != nil {
return nil, sd.InstancesError
}
key := fmt.Sprintf("%s:%d", hostname.String(), port)
instances, ok := sd.instancesByPortNum[key]
if !ok {
return nil, nil
}
return instances, nil
}
// GetProxyServiceInstances returns service instances associated with a node, resulting in
// 'in' services.
func (sd *MemServiceDiscovery) GetProxyServiceInstances(node *model.Proxy) ([]*model.ServiceInstance, error) {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if sd.GetProxyServiceInstancesError != nil {
return nil, sd.GetProxyServiceInstancesError
}
if sd.WantGetProxyServiceInstances != nil {
return sd.WantGetProxyServiceInstances, nil
}
out := make([]*model.ServiceInstance, 0)
si, found := sd.ip2instance[node.IPAddress]
if found {
out = append(out, si...)
}
return out, sd.GetProxyServiceInstancesError
}
// ManagementPorts implements discovery interface
func (sd *MemServiceDiscovery) ManagementPorts(addr string) model.PortList {
sd.mutex.Lock()
defer sd.mutex.Unlock()
return model.PortList{{
Name: "http",
Port: 3333,
Protocol: model.ProtocolHTTP,
}, {
Name: "custom",
Port: 9999,
Protocol: model.ProtocolTCP,
}}
}
// WorkloadHealthCheckInfo implements discovery interface
func (sd *MemServiceDiscovery) WorkloadHealthCheckInfo(addr string) model.ProbeList {
return nil
}
// GetIstioServiceAccounts gets the Istio service accounts for a service hostname.
func (sd *MemServiceDiscovery) GetIstioServiceAccounts(hostname model.Hostname, ports []string) []string {
sd.mutex.Lock()
defer sd.mutex.Unlock()
if hostname == "world.default.svc.cluster.local" {
return []string{
"spiffe://cluster.local/ns/default/sa/serviceaccount1",
"spiffe://cluster.local/ns/default/sa/serviceaccount2",
}
}
return make([]string, 0)
}
// registryz providees debug support for registry - adding and listing model items.
// Can be combined with the push debug interface to reproduce changes.
func (s *DiscoveryServer) registryz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
svcName := req.Form.Get("svc")
if svcName != "" {
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return
}
svc := &model.Service{}
err = json.Unmarshal(data, svc)
if err != nil {
return
}
s.MemRegistry.AddService(model.Hostname(svcName), svc)
}
all, err := s.env.ServiceDiscovery.Services()
if err != nil {
return
}
fmt.Fprintln(w, "[")
for _, svc := range all {
b, err := json.MarshalIndent(svc, "", " ")
if err != nil {
return
}
_, _ = w.Write(b)
fmt.Fprintln(w, ",")
}
fmt.Fprintln(w, "{}]")
}
// Endpoint debugging
func (s *DiscoveryServer) endpointz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
svcName := req.Form.Get("svc")
if svcName != "" {
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return
}
svc := &model.ServiceInstance{}
err = json.Unmarshal(data, svc)
if err != nil {
return
}
s.MemRegistry.AddInstance(model.Hostname(svcName), svc)
}
brief := req.Form.Get("brief")
if brief != "" {
svc, _ := s.env.ServiceDiscovery.Services()
for _, ss := range svc {
for _, p := range ss.Ports {
all, err := s.env.ServiceDiscovery.InstancesByPort(ss.Hostname, p.Port, nil)
if err != nil {
return
}
for _, svc := range all {
fmt.Fprintf(w, "%s:%s %v %s:%d %v %v %s\n", ss.Hostname,
p.Name, svc.Endpoint.Family, svc.Endpoint.Address, svc.Endpoint.Port, svc.Labels,
svc.GetAZ(), svc.ServiceAccount)
}
}
}
return
}
svc, _ := s.env.ServiceDiscovery.Services()
fmt.Fprint(w, "[\n")
for _, ss := range svc {
for _, p := range ss.Ports {
all, err := s.env.ServiceDiscovery.InstancesByPort(ss.Hostname, p.Port, nil)
if err != nil {
return
}
fmt.Fprintf(w, "\n{\"svc\": \"%s:%s\", \"ep\": [\n", ss.Hostname, p.Name)
for _, svc := range all {
b, err := json.MarshalIndent(svc, " ", " ")
if err != nil {
return
}
_, _ = w.Write(b)
fmt.Fprint(w, ",\n")
}
fmt.Fprint(w, "\n{}]},")
}
}
fmt.Fprint(w, "\n{}]\n")
}
// Config debugging.
func (s *DiscoveryServer) configz(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, "\n[\n")
for _, typ := range s.env.IstioConfigStore.ConfigDescriptor() {
cfg, _ := s.env.IstioConfigStore.List(typ.Type, "")
for _, c := range cfg {
b, err := json.MarshalIndent(c, " ", " ")
if err != nil {
return
}
_, _ = w.Write(b)
fmt.Fprint(w, ",\n")
}
}
fmt.Fprint(w, "\n{}]")
}
// Returns whether the given destination rule use (Istio) mutual TLS setting for given port.
// TODO: check subsets possibly conflicts between subsets.
func isMTlsOn(mesh *meshconfig.MeshConfig, rule *networking.DestinationRule, port *model.Port) bool {
if rule == nil {
return mesh.AuthPolicy == meshconfig.MeshConfig_MUTUAL_TLS
}
if rule.TrafficPolicy == nil {
return false
}
_, _, _, tls := networking_core.SelectTrafficPolicyComponents(rule.TrafficPolicy, port)
return tls != nil && tls.Mode == networking.TLSSettings_ISTIO_MUTUAL
}
// AuthenticationDebug holds debug information for service authentication policy.
type AuthenticationDebug struct {
Host string `json:"host"`
Port int `json:"port"`
AuthenticationPolicyName string `json:"authentication_policy_name"`
DestinationRuleName string `json:"destination_rule_name"`
ServerProtocol string `json:"server_protocol"`
ClientProtocol string `json:"client_protocol"`
TLSConflictStatus string `json:"TLS_conflict_status"`
}
func configName(config *model.Config) string {
if config != nil {
return fmt.Sprintf("%s/%s", config.Name, config.Namespace)
}
return "-"
}
func mTLSModeToString(useTLS bool) string {
if useTLS {
return "mTLS"
}
return "HTTP"
}
// Authentication debugging
// This handler lists what authentication policy and destination rules is used for a service, and
// whether or not they have TLS setting conflicts (i.e authentication policy use mutual TLS, but
// destination rule doesn't use ISTIO_MUTUAL TLS mode). If service is not provided, (via request
// paramerter `svc`), it lists result for all services.
func (s *DiscoveryServer) authenticationz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
// This should be svc. However, use proxyID param for now so it can be used with
// `pilot-discovery debug` command
interestedSvc := req.Form.Get("proxyID")
fmt.Fprintf(w, "\n[\n")
svc, _ := s.env.ServiceDiscovery.Services()
for _, ss := range svc {
if interestedSvc != "" && interestedSvc != ss.Hostname.String() {
continue
}
for _, p := range ss.Ports {
info := AuthenticationDebug{
Host: string(ss.Hostname),
Port: p.Port,
}
authnConfig := s.env.IstioConfigStore.AuthenticationPolicyByDestination(ss.Hostname, p)
info.AuthenticationPolicyName = configName(authnConfig)
if authnConfig != nil {
policy := authnConfig.Spec.(*authn.Policy)
serverSideTLS, _ := authn_plugin.RequireTLS(policy, model.Sidecar)
info.ServerProtocol = mTLSModeToString(serverSideTLS)
} else {
info.ServerProtocol = mTLSModeToString(s.env.Mesh.AuthPolicy == meshconfig.MeshConfig_MUTUAL_TLS)
}
destConfig := s.env.IstioConfigStore.DestinationRule(ss.Hostname)
info.DestinationRuleName = configName(destConfig)
if destConfig != nil {
rule := destConfig.Spec.(*networking.DestinationRule)
info.ClientProtocol = mTLSModeToString(isMTlsOn(s.env.Mesh, rule, p))
} else {
info.ClientProtocol = mTLSModeToString(s.env.Mesh.AuthPolicy == meshconfig.MeshConfig_MUTUAL_TLS)
}
if info.ClientProtocol != info.ServerProtocol {
info.TLSConflictStatus = "CONFLICT"
} else {
info.TLSConflictStatus = "OK"
}
if b, err := json.MarshalIndent(info, " ", " "); err == nil {
_, _ = w.Write(b)
}
fmt.Fprintf(w, ",\n")
}
}
fmt.Fprint(w, "\n{}]")
}
// adsz implements a status and debug interface for ADS.
// It is mapped to /debug/adsz
func adsz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
if req.Form.Get("push") != "" {
adsPushAll()
fmt.Fprintf(w, "Pushed to %d servers", len(adsClients))
return
}
writeAllADS(w)
}
// ConfigDump returns information in the form of the Envoy admin API config dump for the specified proxy
// The dump will only contain dynamic listeners/clusters/routes and can be used to compare what an Envoy instance
// should look like according to Pilot vs what it currently does look like.
func ConfigDump(w http.ResponseWriter, req *http.Request) {
if proxyID := req.URL.Query().Get("proxyID"); proxyID != "" {
adsClientsMutex.RLock()
defer adsClientsMutex.RUnlock()
connections, ok := adsSidecarIDConnectionsMap[proxyID]
if !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Proxy not connected to this Pilot instance"))
return
}
jsonm := &jsonpb.Marshaler{Indent: " "}
mostRecent := ""
for key := range connections {
if mostRecent == "" || key > mostRecent {
mostRecent = key
}
}
dump, err := connections[mostRecent].configDump()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if err := jsonm.Marshal(w, dump); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("You must provide a proxyID in the query string"))
}
func writeAllADS(w io.Writer) {
adsClientsMutex.RLock()
defer adsClientsMutex.RUnlock()
// Dirty json generation - because standard json is dirty (struct madness)
// Unfortunately we must use the jsonbp to encode part of the json - I'm sure there are
// better ways, but this is mainly for debugging.
fmt.Fprint(w, "[\n")
comma := false
for _, c := range adsClients {
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
fmt.Fprintf(w, "\n\n {\"node\": \"%s\",\n \"addr\": \"%s\",\n \"connect\": \"%v\",\n \"listeners\":[\n", c.ConID, c.PeerAddr, c.Connect)
printListeners(w, c)
fmt.Fprint(w, "],\n")
fmt.Fprintf(w, "\"RDSRoutes\":[\n")
printRoutes(w, c)
fmt.Fprint(w, "],\n")
fmt.Fprintf(w, "\"clusters\":[\n")
printClusters(w, c)
fmt.Fprint(w, "]}\n")
}
fmt.Fprint(w, "]\n")
}
// edsz implements a status and debug interface for EDS.
// It is mapped to /debug/edsz on the monitor port (9093).
func edsz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
if req.Form.Get("push") != "" {
PushAll()
}
edsClusterMutex.Lock()
comma := false
if len(edsClusters) > 0 {
fmt.Fprintln(w, "[")
for _, eds := range edsClusters {
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
jsonm := &jsonpb.Marshaler{Indent: " "}
dbgString, _ := jsonm.MarshalToString(eds.LoadAssignment)
if _, err := w.Write([]byte(dbgString)); err != nil {
return
}
}
fmt.Fprintln(w, "]")
} else {
w.WriteHeader(404)
}
edsClusterMutex.Unlock()
}
// cdsz implements a status and debug interface for CDS.
// It is mapped to /debug/cdsz
func cdsz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
adsClientsMutex.RLock()
fmt.Fprint(w, "[\n")
comma := false
for _, c := range adsClients {
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
fmt.Fprintf(w, "\n\n {\"node\": \"%s\", \"addr\": \"%s\", \"connect\": \"%v\",\"Clusters\":[\n", c.ConID, c.PeerAddr, c.Connect)
printClusters(w, c)
fmt.Fprint(w, "]}\n")
}
fmt.Fprint(w, "]\n")
adsClientsMutex.RUnlock()
}
func printListeners(w io.Writer, c *XdsConnection) {
comma := false
for _, ls := range c.HTTPListeners {
if ls == nil {
adsLog.Errorf("INVALID LISTENER NIL")
continue
}
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
jsonm := &jsonpb.Marshaler{Indent: " "}
dbgString, _ := jsonm.MarshalToString(ls)
if _, err := w.Write([]byte(dbgString)); err != nil {
return
}
}
}
func printClusters(w io.Writer, c *XdsConnection) {
comma := false
for _, cl := range c.HTTPClusters {
if cl == nil {
adsLog.Errorf("INVALID Cluster NIL")
continue
}
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
jsonm := &jsonpb.Marshaler{Indent: " "}
dbgString, _ := jsonm.MarshalToString(cl)
if _, err := w.Write([]byte(dbgString)); err != nil {
return
}
}
}
func printRoutes(w io.Writer, c *XdsConnection) {
comma := false
for _, rt := range c.RouteConfigs {
if rt == nil {
adsLog.Errorf("INVALID ROUTE CONFIG NIL")
continue
}
if comma {
fmt.Fprint(w, ",\n")
} else {
comma = true
}
jsonm := &jsonpb.Marshaler{Indent: " "}
dbgString, _ := jsonm.MarshalToString(rt)
if _, err := w.Write([]byte(dbgString)); err != nil {
return
}
}
}