@@ -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 ()
@@ -81,7 +98,19 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
81
98
}
82
99
}
83
100
if tmpl != nil {
84
- out := EventOut {e .Timestamp , e .Namespace , e .Topic , string (out )}
101
+ var data map [string ]interface {}
102
+ err := json .Unmarshal (out , & data )
103
+ if err != nil {
104
+ log .G (ctx ).WithError (err ).Warn ("cannot marshal Any into JSON" )
105
+ }
106
+
107
+ id := "unknown"
108
+ _ , ok := data ["container_id" ]
109
+ if ok {
110
+ id = data ["container_id" ].(string )
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