Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Status and ID as event attributes #3232

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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