-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
602 lines (554 loc) · 17.4 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
// 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"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/gogo/protobuf/jsonpb"
authn "istio.io/api/authentication/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"
)
// 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)
s.MemRegistry.EDSUpdater = s
s.MemRegistry.ClusterID = "v2-debug"
sctl.AddRegistry(aggregate.Registry{
ClusterID: "v2-debug",
Name: serviceregistry.ServiceRegistry("memAdapter"),
ServiceDiscovery: s.MemRegistry,
ServiceAccounts: s.MemRegistry,
Controller: s.MemRegistry.controller,
})
mux.HandleFunc("/ready", s.ready)
mux.HandleFunc("/debug/edsz", s.edsz)
mux.HandleFunc("/debug/adsz", s.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/endpointShardz", s.endpointShardz)
mux.HandleFunc("/debug/workloadz", s.workloadz)
mux.HandleFunc("/debug/configz", s.configz)
mux.HandleFunc("/debug/authenticationz", s.authenticationz)
mux.HandleFunc("/debug/config_dump", s.ConfigDump)
mux.HandleFunc("/debug/push_status", s.PushStatusHandler)
}
// SyncStatus is the synchronization status between Pilot and a given Envoy
type SyncStatus struct {
ProxyID string `json:"proxy,omitempty"`
ProxyVersion string `json:"proxy_version,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) {
syncz := []SyncStatus{}
adsClientsMutex.RLock()
for _, con := range adsClients {
con.mu.RLock()
if con.modelNode != nil {
proxyVersion, _ := con.modelNode.GetProxyVersion()
syncz = append(syncz, SyncStatus{
ProxyID: con.modelNode.ID,
ProxyVersion: proxyVersion,
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)
}
// 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, "{}]")
}
// Dumps info about the endpoint shards, tracked using the new direct interface.
// Legacy registry provides are synced to the new data structure as well, during
// the full push.
func (s *DiscoveryServer) endpointShardz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
out, _ := json.MarshalIndent(s.EndpointShardsByService, " ", " ")
w.Write(out)
}
// Tracks info about workloads. Currently only K8S serviceregistry populates this, based
// on pod labels and annotations. This is used to detect label changes and push.
func (s *DiscoveryServer) workloadz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
s.mutex.RLock()
out, _ := json.MarshalIndent(s.WorkloadsByID, " ", " ")
s.mutex.RUnlock()
w.Write(out)
}
// 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 %s\n", ss.Hostname,
p.Name, svc.Endpoint.Family, svc.Endpoint.Address, svc.Endpoint.Port, svc.Labels,
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{}]")
}
type authProtocol int
const (
authnHTTP authProtocol = 1
authnMTls authProtocol = 2
authnPermissive authProtocol = authnHTTP | authnMTls
)
// Returns whether the given destination rule use (Istio) mutual TLS setting for given port.
// TODO: check subsets possibly conflicts between subsets.
func clientAuthProtocol(rule *networking.DestinationRule, port *model.Port) authProtocol {
if rule.TrafficPolicy == nil {
return authnHTTP
}
_, _, _, tls := networking_core.SelectTrafficPolicyComponents(rule.TrafficPolicy, port)
if tls != nil && tls.Mode == networking.TLSSettings_ISTIO_MUTUAL {
return authnMTls
}
return authnHTTP
}
func getServerAuthProtocol(mtls *authn.MutualTls) authProtocol {
if mtls == nil {
return authnHTTP
}
if mtls.Mode == authn.MutualTls_PERMISSIVE {
return authnPermissive
}
return authnMTls
}
// 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 authProtocolToString(protocol authProtocol) string {
switch protocol {
case authnHTTP:
return "HTTP"
case authnMTls:
return "mTLS"
case authnPermissive:
return "HTTP/mTLS"
default:
return "UNKNOWN"
}
}
// 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 != string(ss.Hostname) {
continue
}
for _, p := range ss.Ports {
info := AuthenticationDebug{
Host: string(ss.Hostname),
Port: p.Port,
}
authnConfig := s.Env.IstioConfigStore.AuthenticationPolicyByDestination(ss, p)
info.AuthenticationPolicyName = configName(authnConfig)
var serverProtocol, clientProtocol authProtocol
if authnConfig != nil {
policy := authnConfig.Spec.(*authn.Policy)
mtls := authn_plugin.GetMutualTLS(policy)
serverProtocol = getServerAuthProtocol(mtls)
} else {
serverProtocol = getServerAuthProtocol(nil)
}
info.ServerProtocol = authProtocolToString(serverProtocol)
destConfig := s.globalPushContext().DestinationRule(nil, ss)
info.DestinationRuleName = configName(destConfig)
if destConfig != nil {
rule := destConfig.Spec.(*networking.DestinationRule)
clientProtocol = clientAuthProtocol(rule, p)
} else {
clientProtocol = authnHTTP
}
info.ClientProtocol = authProtocolToString(clientProtocol)
if (clientProtocol & serverProtocol) == 0 {
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 (s *DiscoveryServer) adsz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
if req.Form.Get("push") != "" {
AdsPushAll(s)
adsClientsMutex.RLock()
fmt.Fprintf(w, "Pushed to %d servers", len(adsClients))
adsClientsMutex.RUnlock()
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 (s *DiscoveryServer) 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 := s.configDump(connections[mostRecent])
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"))
}
// PushStatusHandler dumps the last PushContext
func (s *DiscoveryServer) PushStatusHandler(w http.ResponseWriter, req *http.Request) {
if model.LastPushStatus == nil {
return
}
out, err := model.LastPushStatus.JSON()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "unable to marshal push information: %v", err)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(out)
w.WriteHeader(http.StatusOK)
}
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")
}
func (s *DiscoveryServer) ready(w http.ResponseWriter, req *http.Request) {
if s.ConfigController != nil {
if !s.ConfigController.HasSynced() {
w.WriteHeader(503)
return
}
}
w.WriteHeader(200)
}
// edsz implements a status and debug interface for EDS.
// It is mapped to /debug/edsz on the monitor port (9093).
func (s *DiscoveryServer) edsz(w http.ResponseWriter, req *http.Request) {
_ = req.ParseForm()
w.Header().Add("Content-Type", "application/json")
if req.Form.Get("push") != "" {
AdsPushAll(s)
}
edsClusterMutex.RLock()
comma := false
if len(edsClusters) > 0 {
fmt.Fprintln(w, "[")
for _, eds := range edsClusters {
for _, eds := range eds {
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.RUnlock()
}
// 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.LDSListeners {
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.CDSClusters {
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
}
}
}