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 <[email protected]>
  • Loading branch information
CodeChanning committed Jul 18, 2024
1 parent 0dfa3bb commit 9ea709e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 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,27 @@ 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"
)

func TopicToStatus(topic string) Status {
if strings.Contains(strings.ToUpper(topic), string(START)) {
return START
}

return "Unsupported Status"
}

// 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 Down Expand Up @@ -81,7 +98,19 @@ 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")
}

id := ""
_, 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 9ea709e

Please sign in to comment.