Skip to content

Commit

Permalink
feat: add event actor id to client api and events cmd
Browse files Browse the repository at this point in the history
Add the missing actor id on the event and a way to filter by it to the events cli command.

Related to #5499.

Signed-off-by: Utku Ozdemir <[email protected]>
  • Loading branch information
utkuozdemir committed Aug 12, 2022
1 parent 9baca49 commit 586e29d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/talosctl/cmd/talos/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var eventsCmdFlags struct {
tailEvents int32
tailDuration time.Duration
tailID string
actorID string
}

// eventsCmd represents the events command.
Expand All @@ -35,7 +36,7 @@ var eventsCmd = &cobra.Command{
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\tSOURCE\tMESSAGE")
fmt.Fprintln(w, "NODE\tID\tEVENT\tACTOR\tSOURCE\tMESSAGE")

opts := []client.EventsOptionFunc{}

Expand All @@ -51,13 +52,17 @@ var eventsCmd = &cobra.Command{
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\t%s\t%s\n"
format := "%s\t%s\t%s\n%s\t%s\t%s\n"

event, err := client.UnmarshalEvent(ev)
if err != nil {
Expand Down Expand Up @@ -104,7 +109,7 @@ var eventsCmd = &cobra.Command{
}
}

args = append([]interface{}{event.Node, event.ID, event.TypeURL}, args...)
args = append([]interface{}{event.Node, event.ID, event.TypeURL, event.ActorID}, args...)
fmt.Fprintf(w, format, args...)

return w.Flush()
Expand All @@ -118,4 +123,5 @@ func init() {
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)")
}
9 changes: 9 additions & 0 deletions pkg/machinery/client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ func WithTailDuration(dur time.Duration) EventsOptionFunc {
}
}

// WithActorID sets up Watcher to return events with the specified actor ID.
func WithActorID(actorID string) EventsOptionFunc {
return func(opts *machineapi.EventsRequest) {
opts.WithActorId = actorID
}
}

// Events implements the proto.OSClient interface.
func (c *Client) Events(ctx context.Context, opts ...EventsOptionFunc) (stream machineapi.MachineService_EventsClient, err error) {
var req machineapi.EventsRequest
Expand All @@ -63,6 +70,7 @@ type Event struct {
Node string
TypeURL string
ID string
ActorID string
Payload proto.Message
}

Expand Down Expand Up @@ -252,6 +260,7 @@ func UnmarshalEvent(event *machineapi.Event) (*Event, error) {
TypeURL: typeURL,
ID: event.Id,
Payload: msg,
ActorID: event.ActorId,
}

if event.Metadata != nil {
Expand Down
1 change: 1 addition & 0 deletions website/content/v1.2/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ talosctl events [flags]
### Options

```
--actor-id string filter events by the specified actor ID (default is no filter)
--duration duration show events for the past duration interval (one second resolution, default is to show no history)
-h, --help help for events
--since string show events after the specified event ID (default is to show no history)
Expand Down

0 comments on commit 586e29d

Please sign in to comment.