/
debug_events.go
145 lines (130 loc) · 3.99 KB
/
debug_events.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package observe
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
observerpb "github.com/cilium/cilium/api/v1/observer"
"github.com/cilium/hubble/cmd/common/config"
"github.com/cilium/hubble/cmd/common/conn"
"github.com/cilium/hubble/cmd/common/template"
"github.com/cilium/hubble/pkg/defaults"
"github.com/cilium/hubble/pkg/logger"
hubtime "github.com/cilium/hubble/pkg/time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
func newDebugEventsCommand(vp *viper.Viper) *cobra.Command {
debugEventsCmd := &cobra.Command{
Use: "debug-events",
Short: "Observe Cilium debug events",
RunE: func(cmd *cobra.Command, _ []string) error {
debug := vp.GetBool(config.KeyDebug)
if err := handleEventsArgs(cmd.OutOrStdout(), debug); err != nil {
return err
}
req, err := getDebugEventsRequest()
if err != nil {
return err
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
hubbleConn, err := conn.New(ctx, vp.GetString(config.KeyServer), vp.GetDuration(config.KeyTimeout))
if err != nil {
return err
}
defer hubbleConn.Close()
client := observerpb.NewObserverClient(hubbleConn)
logger.Logger.Debug("Sending GetDebugEvents request", "request", req)
if err := getDebugEvents(ctx, client, req); err != nil {
msg := err.Error()
// extract custom error message from failed grpc call
if s, ok := status.FromError(err); ok && s.Code() == codes.Unknown {
msg = s.Message()
}
return errors.New(msg)
}
return nil
},
}
flagSets := []*pflag.FlagSet{selectorFlags, formattingFlags, config.ServerFlags, otherFlags}
for _, fs := range flagSets {
debugEventsCmd.Flags().AddFlagSet(fs)
}
template.RegisterFlagSets(debugEventsCmd, flagSets...)
return debugEventsCmd
}
func getDebugEventsRequest() (*observerpb.GetDebugEventsRequest, error) {
// convert selectorOpts.since into a param for GetDebugEvents
var since, until *timestamppb.Timestamp
if selectorOpts.since != "" {
st, err := hubtime.FromString(selectorOpts.since)
if err != nil {
return nil, fmt.Errorf("failed to parse the since time: %v", err)
}
since = timestamppb.New(st)
if err := since.CheckValid(); err != nil {
return nil, fmt.Errorf("failed to convert `since` timestamp to proto: %v", err)
}
}
// Set the until field if --until option is specified and --follow
// is not specified. If --since is specified but --until is not, the server sets the
// --until option to the current timestamp.
if selectorOpts.until != "" && !selectorOpts.follow {
ut, err := hubtime.FromString(selectorOpts.until)
if err != nil {
return nil, fmt.Errorf("failed to parse the until time: %v", err)
}
until = timestamppb.New(ut)
if err := until.CheckValid(); err != nil {
return nil, fmt.Errorf("failed to convert `until` timestamp to proto: %v", err)
}
}
if since == nil && until == nil {
switch {
case selectorOpts.all:
// all is an alias for last=uint64_max
selectorOpts.last = ^uint64(0)
case selectorOpts.last == 0:
// no specific parameters were provided, just a vanilla `hubble events debug`
selectorOpts.last = defaults.EventsPrintCount
}
}
return &observerpb.GetDebugEventsRequest{
Number: selectorOpts.last,
Follow: selectorOpts.follow,
Since: since,
Until: until,
}, nil
}
func getDebugEvents(ctx context.Context, client observerpb.ObserverClient, req *observerpb.GetDebugEventsRequest) error {
b, err := client.GetDebugEvents(ctx, req)
if err != nil {
return err
}
defer printer.Close()
for {
resp, err := b.Recv()
switch err {
case io.EOF, context.Canceled:
return nil
case nil:
default:
if status.Code(err) == codes.Canceled {
return nil
}
return err
}
if err = printer.WriteProtoDebugEvent(resp); err != nil {
return err
}
}
}