@@ -22,6 +22,7 @@ import (
2222	"encoding/json" 
2323	"errors" 
2424	"fmt" 
25+ 	"strings" 
2526	"text/template" 
2627	"time" 
2728
@@ -37,11 +38,28 @@ 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+ 	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+ 
4563// Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go 
4664func  Events (ctx  context.Context , client  * containerd.Client , options  types.SystemEventsOptions ) error  {
4765	eventsClient  :=  client .EventService ()
@@ -68,6 +86,7 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
6886		}
6987		if  e  !=  nil  {
7088			var  out  []byte 
89+ 			var  id  string 
7190			if  e .Event  !=  nil  {
7291				v , err  :=  typeurl .UnmarshalAny (e .Event )
7392				if  err  !=  nil  {
@@ -81,7 +100,18 @@ func Events(ctx context.Context, client *containerd.Client, options types.System
81100				}
82101			}
83102			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 )}
85115				var  b  bytes.Buffer 
86116				if  err  :=  tmpl .Execute (& b , out ); err  !=  nil  {
87117					return  err 
0 commit comments