Skip to content

Commit

Permalink
feat: add Status and ID as event attributes
Browse files Browse the repository at this point in the history
Signed-off-by: CodeChanning <chxgaddy@amazon.com>
  • Loading branch information
CodeChanning committed Jul 23, 2024
1 parent 0dfa3bb commit fea8219
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/cmd/system/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"text/template"
"time"

Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit fea8219

Please sign in to comment.