Repository navigation
Expand file tree
/
Copy pathedges.go
More file actions
450 lines (388 loc) · 12.8 KB
/
Copy pathedges.go
File metadata and controls
450 lines (388 loc) · 12.8 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
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
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/fatih/color"
pkgcmd "github.com/linkerd/linkerd2/pkg/cmd"
"github.com/linkerd/linkerd2/pkg/healthcheck"
"github.com/linkerd/linkerd2/pkg/k8s"
pb "github.com/linkerd/linkerd2/viz/metrics-api/gen/viz"
"github.com/linkerd/linkerd2/viz/metrics-api/util"
"github.com/linkerd/linkerd2/viz/pkg/api"
hc "github.com/linkerd/linkerd2/viz/pkg/healthcheck"
pkgUtil "github.com/linkerd/linkerd2/viz/pkg/util"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
)
var (
okStatus = color.New(color.FgGreen, color.Bold).SprintFunc()("\u221A") // √
)
type edgesOptions struct {
namespace string
outputFormat string
allNamespaces bool
}
func newEdgesOptions() *edgesOptions {
return &edgesOptions{
outputFormat: tableOutput,
allNamespaces: false,
}
}
type indexedEdgeResults struct {
ix int
rows []*pb.Edge
err error
}
// NewCmdEdges creates a new cobra command `edges` for edges functionality
func NewCmdEdges() *cobra.Command {
options := newEdgesOptions()
cmd := &cobra.Command{
Use: "edges [flags] (RESOURCETYPE)",
Short: "Display connections between resources, and Linkerd proxy identities",
Long: `Display connections between resources, and Linkerd proxy identities.
The RESOURCETYPE argument specifies the type of resource to display edges within.
Examples:
* cronjob
* deploy
* ds
* job
* po
* rc
* rs
* sts
Valid resource types include:
* cronjobs
* daemonsets
* deployments
* jobs
* pods
* replicasets
* replicationcontrollers
* statefulsets`,
Example: ` # Get all edges between pods that either originate from or terminate in the test namespace.
linkerd viz edges po -n test
# Get all edges between pods that either originate from or terminate in the default namespace.
linkerd viz edges po
# Get all edges between pods in all namespaces.
linkerd viz edges po --all-namespaces`,
Args: cobra.ExactArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// This command requires only one argument. If we already have
// one after requesting autocompletion i.e. [tab][tab]
// skip running validArgsFunction
if len(args) > 0 {
return []string{}, cobra.ShellCompDirectiveError
}
k8sAPI, err := k8s.NewAPI(kubeconfigPath, kubeContext, impersonate, impersonateGroup, 0)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
}
if options.allNamespaces {
options.namespace = v1.NamespaceAll
}
cc := k8s.NewCommandCompletion(k8sAPI, options.namespace)
results, err := cc.Complete(args, toComplete)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return results, cobra.ShellCompDirectiveDefault
},
RunE: func(cmd *cobra.Command, args []string) error {
if options.namespace == "" {
options.namespace = pkgcmd.GetDefaultNamespace(kubeconfigPath, kubeContext)
}
reqs, err := buildEdgesRequests(args, options)
if err != nil {
return fmt.Errorf("Error creating edges request: %w", err)
}
// The gRPC client is concurrency-safe, so we can reuse it in all the following goroutines
// https://github.com/grpc/grpc-go/issues/682
client := api.CheckClientOrExit(hc.VizOptions{
Options: &healthcheck.Options{
ControlPlaneNamespace: controlPlaneNamespace,
KubeConfig: kubeconfigPath,
Impersonate: impersonate,
ImpersonateGroup: impersonateGroup,
KubeContext: kubeContext,
APIAddr: apiAddr,
},
VizNamespaceOverride: vizNamespace,
})
c := make(chan indexedEdgeResults, len(reqs))
for num, req := range reqs {
go func(num int, req *pb.EdgesRequest) {
resp, err := requestEdgesFromAPI(client, req)
rows := edgesRespToRows(resp)
c <- indexedEdgeResults{num, rows, err}
}(num, req)
}
totalRows := make([]*pb.Edge, 0)
i := 0
for res := range c {
if res.err != nil {
fmt.Fprint(os.Stderr, res.err.Error())
os.Exit(1)
}
totalRows = append(totalRows, res.rows...)
if i++; i == len(reqs) {
close(c)
}
}
output := renderEdgeStats(totalRows, options)
_, err = fmt.Print(output)
return err
},
}
cmd.PersistentFlags().StringVarP(&options.namespace, "namespace", "n", options.namespace, "Namespace of the specified resource")
cmd.PersistentFlags().StringVarP(&options.outputFormat, "output", "o", options.outputFormat, "Output format; one of: \"table\" or \"json\" or \"wide\"")
cmd.PersistentFlags().BoolVarP(&options.allNamespaces, "all-namespaces", "A", options.allNamespaces, "If present, returns edges across all namespaces, ignoring the \"--namespace\" flag")
pkgcmd.ConfigureNamespaceFlagCompletion(
cmd, []string{"namespace"},
kubeconfigPath, impersonate, impersonateGroup, kubeContext)
return cmd
}
// validateEdgesRequestInputs ensures that the resource type and output format are both supported
// by the edges command, since the edges command does not support all k8s resource types.
func validateEdgesRequestInputs(targets []*pb.Resource, options *edgesOptions) error {
for _, target := range targets {
if target.Name != "" {
return fmt.Errorf("Edges cannot be returned for a specific resource name; remove %s from query", target.Name)
}
switch target.Type {
case "authority", "service", "all":
return fmt.Errorf("Resource type is not supported: %s", target.Type)
}
}
switch options.outputFormat {
case tableOutput, jsonOutput, wideOutput:
return nil
default:
return fmt.Errorf("--output supports %s, %s and %s", tableOutput, jsonOutput, wideOutput)
}
}
func buildEdgesRequests(resources []string, options *edgesOptions) ([]*pb.EdgesRequest, error) {
targets, err := pkgUtil.BuildResources(options.namespace, resources)
if err != nil {
return nil, err
}
err = validateEdgesRequestInputs(targets, options)
if err != nil {
return nil, err
}
requests := make([]*pb.EdgesRequest, 0)
for _, target := range targets {
requestParams := util.EdgesRequestParams{
ResourceType: target.Type,
Namespace: options.namespace,
AllNamespaces: options.allNamespaces,
}
req, err := util.BuildEdgesRequest(requestParams)
if err != nil {
return nil, err
}
requests = append(requests, req)
}
return requests, nil
}
func edgesRespToRows(resp *pb.EdgesResponse) []*pb.Edge {
rows := make([]*pb.Edge, 0)
if resp != nil {
rows = append(rows, resp.GetOk().Edges...)
}
return rows
}
func requestEdgesFromAPI(client pb.ApiClient, req *pb.EdgesRequest) (*pb.EdgesResponse, error) {
resp, err := client.Edges(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("Edges API error: %w", err)
}
if e := resp.GetError(); e != nil {
return nil, fmt.Errorf("Edges API response error: %s", e.Error)
}
return resp, nil
}
func renderEdgeStats(rows []*pb.Edge, options *edgesOptions) string {
var buffer bytes.Buffer
w := tabwriter.NewWriter(&buffer, 0, 0, padding, ' ', tabwriter.AlignRight)
writeEdgesToBuffer(rows, w, options)
w.Flush()
return renderEdges(buffer, options)
}
type edgeRow struct {
src string
srcNamespace string
dst string
dstNamespace string
client string
server string
msg string
}
const (
srcHeader = "SRC"
dstHeader = "DST"
srcNamespaceHeader = "SRC_NS"
dstNamespaceHeader = "DST_NS"
clientHeader = "CLIENT_ID"
serverHeader = "SERVER_ID"
msgHeader = "SECURED"
)
func writeEdgesToBuffer(rows []*pb.Edge, w *tabwriter.Writer, options *edgesOptions) {
maxSrcLength := len(srcHeader)
maxDstLength := len(dstHeader)
maxSrcNamespaceLength := len(srcNamespaceHeader)
maxDstNamespaceLength := len(dstNamespaceHeader)
maxClientLength := len(clientHeader)
maxServerLength := len(serverHeader)
maxMsgLength := len(msgHeader)
edgeRows := []edgeRow{}
for _, r := range rows {
clientID := r.ClientId
serverID := r.ServerId
msg := r.NoIdentityMsg
if msg == "" && options.outputFormat != jsonOutput {
msg = okStatus
}
if len(clientID) > 0 {
parts := strings.Split(clientID, ".")
clientID = parts[0] + "." + parts[1]
}
if len(serverID) > 0 {
parts := strings.Split(serverID, ".")
serverID = parts[0] + "." + parts[1]
}
row := edgeRow{
client: clientID,
server: serverID,
msg: msg,
src: r.Src.Name,
srcNamespace: r.Src.Namespace,
dst: r.Dst.Name,
dstNamespace: r.Dst.Namespace,
}
edgeRows = append(edgeRows, row)
if len(r.Src.Name) > maxSrcLength {
maxSrcLength = len(r.Src.Name)
}
if len(r.Src.Namespace) > maxSrcNamespaceLength {
maxSrcNamespaceLength = len(r.Src.Namespace)
}
if len(r.Dst.Name) > maxDstLength {
maxDstLength = len(r.Dst.Name)
}
if len(r.Dst.Namespace) > maxDstNamespaceLength {
maxDstNamespaceLength = len(r.Dst.Namespace)
}
if len(clientID) > maxClientLength {
maxClientLength = len(clientID)
}
if len(serverID) > maxServerLength {
maxServerLength = len(serverID)
}
if len(msg) > maxMsgLength {
maxMsgLength = len(msg)
}
}
// ordering the rows first by SRC/DST namespace, then by SRC/DST resource
sort.Slice(edgeRows, func(i, j int) bool {
keyI := edgeRows[i].srcNamespace + edgeRows[i].dstNamespace + edgeRows[i].src + edgeRows[i].dst
keyJ := edgeRows[j].srcNamespace + edgeRows[j].dstNamespace + edgeRows[j].src + edgeRows[j].dst
return keyI < keyJ
})
switch options.outputFormat {
case tableOutput, wideOutput:
if len(edgeRows) == 0 {
fmt.Fprintln(os.Stderr, "No edges found.")
os.Exit(0)
}
printEdgeTable(edgeRows, w, maxSrcLength, maxSrcNamespaceLength, maxDstLength, maxDstNamespaceLength, maxClientLength, maxServerLength, maxMsgLength, options.outputFormat)
case jsonOutput:
printEdgesJSON(edgeRows, w)
}
}
func printEdgeTable(edgeRows []edgeRow, w *tabwriter.Writer, maxSrcLength, maxSrcNamespaceLength, maxDstLength, maxDstNamespaceLength, maxClientLength, maxServerLength, maxMsgLength int, outputFormat string) {
srcTemplate := fmt.Sprintf("%%-%ds", maxSrcLength)
dstTemplate := fmt.Sprintf("%%-%ds", maxDstLength)
srcNamespaceTemplate := fmt.Sprintf("%%-%ds", maxSrcNamespaceLength)
dstNamespaceTemplate := fmt.Sprintf("%%-%ds", maxDstNamespaceLength)
msgTemplate := fmt.Sprintf("%%-%ds", maxMsgLength)
clientTemplate := fmt.Sprintf("%%-%ds", maxClientLength)
serverTemplate := fmt.Sprintf("%%-%ds", maxServerLength)
headers := []string{
fmt.Sprintf(srcTemplate, srcHeader),
fmt.Sprintf(dstTemplate, dstHeader),
fmt.Sprintf(srcNamespaceTemplate, srcNamespaceHeader),
fmt.Sprintf(dstNamespaceTemplate, dstNamespaceHeader),
}
if outputFormat == wideOutput {
headers = append(headers, fmt.Sprintf(clientTemplate, clientHeader), fmt.Sprintf(serverTemplate, serverHeader))
}
headers = append(headers, fmt.Sprintf(msgTemplate, msgHeader)+"\t")
fmt.Fprintln(w, strings.Join(headers, "\t"))
for _, row := range edgeRows {
values := []interface{}{
row.src,
row.dst,
row.srcNamespace,
row.dstNamespace,
}
templateString := fmt.Sprintf("%s\t%s\t%s\t%s\t", srcTemplate, dstTemplate, srcNamespaceTemplate, dstNamespaceTemplate)
if outputFormat == wideOutput {
templateString += fmt.Sprintf("%s\t%s\t", clientTemplate, serverTemplate)
values = append(values, row.client, row.server)
}
templateString += fmt.Sprintf("%s\t\n", msgTemplate)
values = append(values, row.msg)
fmt.Fprintf(w, templateString, values...)
}
}
func renderEdges(buffer bytes.Buffer, options *edgesOptions) string {
var out string
switch options.outputFormat {
case jsonOutput:
out = buffer.String()
default:
// strip left padding on the first column
out = string(buffer.Bytes()[padding:])
out = strings.ReplaceAll(out, "\n"+strings.Repeat(" ", padding), "\n")
}
return out
}
type edgesJSONStats struct {
Src string `json:"src"`
SrcNamespace string `json:"src_namespace"`
Dst string `json:"dst"`
DstNamespace string `json:"dst_namespace"`
Client string `json:"client_id"`
Server string `json:"server_id"`
Msg string `json:"no_tls_reason"`
}
func printEdgesJSON(edgeRows []edgeRow, w *tabwriter.Writer) {
// avoid nil initialization so that if there are not stats it gets marshalled as an empty array vs null
entries := []*edgesJSONStats{}
for _, row := range edgeRows {
entry := &edgesJSONStats{
Src: row.src,
SrcNamespace: row.srcNamespace,
Dst: row.dst,
DstNamespace: row.dstNamespace,
Client: row.client,
Server: row.server,
Msg: row.msg}
entries = append(entries, entry)
}
b, err := json.MarshalIndent(entries, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshalling JSON: %s\n", err)
return
}
fmt.Fprintf(w, "%s\n", b)
}