@@ -22,6 +22,7 @@ import (
22
22
"encoding/json"
23
23
"errors"
24
24
"fmt"
25
+ "strings"
25
26
"text/template"
26
27
"time"
27
28
@@ -37,11 +38,27 @@ import (
37
38
// EventOut contains information about an event.
38
39
type EventOut struct {
39
40
Timestamp time.Time
41
+ ID string
40
42
Namespace string
41
43
Topic string
44
+ Status Status
42
45
Event string
43
46
}
44
47
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
+
45
62
// Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
46
63
func Events (ctx context.Context , client * containerd.Client , options types.SystemEventsOptions ) error {
47
64
eventsClient := client .EventService ()
@@ -68,6 +85,7 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
68
85
}
69
86
if e != nil {
70
87
var out []byte
88
+ var id string
71
89
if e .Event != nil {
72
90
v , err := typeurl .UnmarshalAny (e .Event )
73
91
if err != nil {
@@ -81,7 +99,18 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
81
99
}
82
100
}
83
101
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 )}
85
114
var b bytes.Buffer
86
115
if err := tmpl .Execute (& b , out ); err != nil {
87
116
return err
0 commit comments