@@ -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. 
3839type  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 
4663func  Events (ctx  context.Context , client  * containerd.Client , options  types.SystemEventsOptions ) error  {
4764	eventsClient  :=  client .EventService ()
@@ -81,7 +98,19 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
8198				}
8299			}
83100			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  :=  "" 
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 )}
85114				var  b  bytes.Buffer 
86115				if  err  :=  tmpl .Execute (& b , out ); err  !=  nil  {
87116					return  err 
0 commit comments