@@ -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,28 @@ 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
+ UNKNOWN Status = "UNKNOWN"
53
+ )
54
+
55
+ func TopicToStatus (topic string ) Status {
56
+ if strings .Contains (strings .ToUpper (topic ), string (START )) {
57
+ return START
58
+ }
59
+
60
+ return UNKNOWN
61
+ }
62
+
45
63
// Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
46
64
func Events (ctx context.Context , client * containerd.Client , options types.SystemEventsOptions ) error {
47
65
eventsClient := client .EventService ()
@@ -68,6 +86,7 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
68
86
}
69
87
if e != nil {
70
88
var out []byte
89
+ var id string
71
90
if e .Event != nil {
72
91
v , err := typeurl .UnmarshalAny (e .Event )
73
92
if err != nil {
@@ -81,7 +100,18 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
81
100
}
82
101
}
83
102
if tmpl != nil {
84
- out := EventOut {e .Timestamp , e .Namespace , e .Topic , string (out )}
103
+ var data map [string ]interface {}
104
+ err := json .Unmarshal (out , & data )
105
+ if err != nil {
106
+ log .G (ctx ).WithError (err ).Warn ("cannot marshal Any into JSON" )
107
+ } else {
108
+ _ , ok := data ["container_id" ]
109
+ if ok {
110
+ id = data ["container_id" ].(string )
111
+ }
112
+ }
113
+
114
+ out := EventOut {e .Timestamp , id , e .Namespace , e .Topic , TopicToStatus (e .Topic ), string (out )}
85
115
var b bytes.Buffer
86
116
if err := tmpl .Execute (& b , out ); err != nil {
87
117
return err
0 commit comments