-
Notifications
You must be signed in to change notification settings - Fork 456
/
events.go
127 lines (107 loc) · 4.04 KB
/
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package talos
import (
"context"
"errors"
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/siderolabs/gen/xslices"
"github.com/spf13/cobra"
"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/client"
)
var eventsCmdFlags struct {
tailEvents int32
tailDuration time.Duration
tailID string
actorID string
}
// eventsCmd represents the events command.
var eventsCmd = &cobra.Command{
Use: "events",
Short: "Stream runtime events",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return WithClient(func(ctx context.Context, c *client.Client) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "NODE\tID\tEVENT\tACTOR\tSOURCE\tMESSAGE")
opts := []client.EventsOptionFunc{}
if eventsCmdFlags.tailEvents != 0 {
opts = append(opts, client.WithTailEvents(eventsCmdFlags.tailEvents))
}
if eventsCmdFlags.tailDuration != 0 {
opts = append(opts, client.WithTailDuration(eventsCmdFlags.tailDuration))
}
if eventsCmdFlags.tailID != "" {
opts = append(opts, client.WithTailID(eventsCmdFlags.tailID))
}
if eventsCmdFlags.actorID != "" {
opts = append(opts, client.WithActorID(eventsCmdFlags.actorID))
}
events, err := c.Events(ctx, opts...)
if err != nil {
return err
}
return helpers.ReadGRPCStream(events, func(ev *machine.Event, node string, multipleNodes bool) error {
format := "%s\t%s\t%s\n%s\t%s\t%s\n"
event, err := client.UnmarshalEvent(ev)
if err != nil {
if errors.Is(err, client.ErrEventNotSupported) {
return nil
}
return err
}
var args []interface{}
switch msg := event.Payload.(type) {
case *machine.SequenceEvent:
args = []interface{}{msg.GetSequence()}
if msg.Error != nil {
args = append(args, "error:"+" "+msg.GetError().GetMessage())
} else {
args = append(args, msg.GetAction().String())
}
case *machine.PhaseEvent:
args = []interface{}{msg.GetPhase(), msg.GetAction().String()}
case *machine.TaskEvent:
args = []interface{}{msg.GetTask(), msg.GetAction().String()}
case *machine.ServiceStateEvent:
args = []interface{}{msg.GetService(), fmt.Sprintf("%s: %s", msg.GetAction(), msg.GetMessage())}
case *machine.ConfigLoadErrorEvent:
args = []interface{}{"error", msg.GetError()}
case *machine.ConfigValidationErrorEvent:
args = []interface{}{"error", msg.GetError()}
case *machine.AddressEvent:
args = []interface{}{msg.GetHostname(), fmt.Sprintf("ADDRESSES: %s", strings.Join(msg.GetAddresses(), ","))}
case *machine.MachineStatusEvent:
args = []interface{}{
msg.GetStage().String(),
fmt.Sprintf("ready: %v, unmet conditions: %v",
msg.GetStatus().Ready,
xslices.Map(msg.GetStatus().GetUnmetConditions(),
func(c *machine.MachineStatusEvent_MachineStatus_UnmetCondition) string {
return c.Name
},
),
),
}
}
args = append([]interface{}{event.Node, event.ID, event.TypeURL, event.ActorID}, args...)
fmt.Fprintf(w, format, args...)
return w.Flush()
})
})
},
}
func init() {
addCommand(eventsCmd)
eventsCmd.Flags().Int32Var(&eventsCmdFlags.tailEvents, "tail", 0, "show specified number of past events (use -1 to show full history, default is to show no history)")
eventsCmd.Flags().DurationVar(&eventsCmdFlags.tailDuration, "duration", 0, "show events for the past duration interval (one second resolution, default is to show no history)")
eventsCmd.Flags().StringVar(&eventsCmdFlags.tailID, "since", "", "show events after the specified event ID (default is to show no history)")
eventsCmd.Flags().StringVar(&eventsCmdFlags.actorID, "actor-id", "", "filter events by the specified actor ID (default is no filter)")
}