Skip to content

Commit 265bfbd

Browse files
committed
feat: add Status and ID as event attributes
Signed-off-by: CodeChanning <[email protected]>
1 parent 0dfa3bb commit 265bfbd

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

pkg/cmd/system/events.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"errors"
2424
"fmt"
25+
"strings"
2526
"text/template"
2627
"time"
2728

@@ -37,11 +38,27 @@ import (
3738
// EventOut contains information about an event.
3839
type EventOut struct {
3940
Timestamp time.Time
41+
ID string
4042
Namespace string
4143
Topic string
44+
Status Status
4245
Event string
4346
}
4447

48+
type Status string
49+
50+
const (
51+
START Status = "START"
52+
)
53+
54+
func TopicToStatus(topic string) Status {
55+
if strings.Contains(strings.ToUpper(topic), string(START)) {
56+
return START
57+
}
58+
59+
return "Unsupported Status"
60+
}
61+
4562
// Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
4663
func Events(ctx context.Context, client *containerd.Client, options types.SystemEventsOptions) error {
4764
eventsClient := client.EventService()
@@ -68,6 +85,7 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
6885
}
6986
if e != nil {
7087
var out []byte
88+
var id string
7189
if e.Event != nil {
7290
v, err := typeurl.UnmarshalAny(e.Event)
7391
if err != nil {
@@ -81,7 +99,18 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
8199
}
82100
}
83101
if tmpl != nil {
84-
out := EventOut{e.Timestamp, e.Namespace, e.Topic, string(out)}
102+
var data map[string]interface{}
103+
err := json.Unmarshal(out, &data)
104+
if err != nil {
105+
log.G(ctx).WithError(err).Warn("cannot marshal Any into JSON")
106+
} else {
107+
_, ok := data["container_id"]
108+
if ok {
109+
id = data["container_id"].(string)
110+
}
111+
}
112+
113+
out := EventOut{e.Timestamp, id, e.Namespace, e.Topic, TopicToStatus(e.Topic), string(out)}
85114
var b bytes.Buffer
86115
if err := tmpl.Execute(&b, out); err != nil {
87116
return err

0 commit comments

Comments
 (0)