diff --git a/pkg/cmd/system/events.go b/pkg/cmd/system/events.go index cb313eca2a7..08a890e3942 100644 --- a/pkg/cmd/system/events.go +++ b/pkg/cmd/system/events.go @@ -22,6 +22,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "text/template" "time" @@ -37,11 +38,28 @@ import ( // EventOut contains information about an event. type EventOut struct { Timestamp time.Time + ID string Namespace string Topic string + Status Status Event string } +type Status string + +const ( + START Status = "START" + UNKNOWN Status = "UNKNOWN" +) + +func TopicToStatus(topic string) Status { + if strings.Contains(strings.ToUpper(topic), string(START)) { + return START + } + + return UNKNOWN +} + // Events is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go func Events(ctx context.Context, client *containerd.Client, options types.SystemEventsOptions) error { eventsClient := client.EventService() @@ -68,6 +86,7 @@ func Events(ctx context.Context, client *containerd.Client, options types.System } if e != nil { var out []byte + var id string if e.Event != nil { v, err := typeurl.UnmarshalAny(e.Event) if err != nil { @@ -81,7 +100,18 @@ func Events(ctx context.Context, client *containerd.Client, options types.System } } if tmpl != nil { - out := EventOut{e.Timestamp, e.Namespace, e.Topic, string(out)} + var data map[string]interface{} + err := json.Unmarshal(out, &data) + if err != nil { + log.G(ctx).WithError(err).Warn("cannot marshal Any into JSON") + } else { + _, ok := data["container_id"] + if ok { + id = data["container_id"].(string) + } + } + + out := EventOut{e.Timestamp, id, e.Namespace, e.Topic, TopicToStatus(e.Topic), string(out)} var b bytes.Buffer if err := tmpl.Execute(&b, out); err != nil { return err