mirror of
https://github.com/containers/podman.git
synced 2025-10-13 09:15:20 +08:00
Merge pull request #26863 from openshift-cherrypick-robot/cherry-pick-26861-to-v5.6
[v5.6] podman events: show network create/remove event with journald
This commit is contained in:
@ -53,14 +53,8 @@ func (e EventJournalD) Write(ee Event) error {
|
|||||||
if ee.PodID != "" {
|
if ee.PodID != "" {
|
||||||
m["PODMAN_POD_ID"] = ee.PodID
|
m["PODMAN_POD_ID"] = ee.PodID
|
||||||
}
|
}
|
||||||
// If we have container labels, we need to convert them to a string so they
|
if err := addLabelsToJournal(m, ee.Details.Attributes); err != nil {
|
||||||
// can be recorded with the event
|
return err
|
||||||
if len(ee.Details.Attributes) > 0 {
|
|
||||||
b, err := json.Marshal(ee.Details.Attributes)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m["PODMAN_LABELS"] = string(b)
|
|
||||||
}
|
}
|
||||||
if ee.Status == HealthStatus {
|
if ee.Status == HealthStatus {
|
||||||
m["PODMAN_HEALTH_STATUS"] = ee.HealthStatus
|
m["PODMAN_HEALTH_STATUS"] = ee.HealthStatus
|
||||||
@ -75,6 +69,9 @@ func (e EventJournalD) Write(ee Event) error {
|
|||||||
case Network:
|
case Network:
|
||||||
m["PODMAN_ID"] = ee.ID
|
m["PODMAN_ID"] = ee.ID
|
||||||
m["PODMAN_NETWORK_NAME"] = ee.Network
|
m["PODMAN_NETWORK_NAME"] = ee.Network
|
||||||
|
if err := addLabelsToJournal(m, ee.Details.Attributes); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
case Volume:
|
case Volume:
|
||||||
m["PODMAN_NAME"] = ee.Name
|
m["PODMAN_NAME"] = ee.Name
|
||||||
}
|
}
|
||||||
@ -93,6 +90,35 @@ func (e EventJournalD) Write(ee Event) error {
|
|||||||
return journal.Send(ee.ToHumanReadable(false), prio, m)
|
return journal.Send(ee.ToHumanReadable(false), prio, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addLabelsToJournal(journalEntry, eventAttributes map[string]string) error {
|
||||||
|
// If we have container labels, we need to convert them to a string so they
|
||||||
|
// can be recorded with the event
|
||||||
|
if len(eventAttributes) > 0 {
|
||||||
|
b, err := json.Marshal(eventAttributes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
journalEntry["PODMAN_LABELS"] = string(b)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLabelsFromJournal(entry *sdjournal.JournalEntry, event *Event) error {
|
||||||
|
// we need to check for the presence of labels recorded to a container event
|
||||||
|
if stringLabels, ok := entry.Fields["PODMAN_LABELS"]; ok && len(stringLabels) > 0 {
|
||||||
|
labels := make(map[string]string, 0)
|
||||||
|
if err := json.Unmarshal([]byte(stringLabels), &labels); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we have labels, add them to the event
|
||||||
|
if len(labels) > 0 {
|
||||||
|
event.Attributes = labels
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Read reads events from the journal and sends qualified events to the event channel
|
// Read reads events from the journal and sends qualified events to the event channel
|
||||||
func (e EventJournalD) Read(ctx context.Context, options ReadOptions) (retErr error) {
|
func (e EventJournalD) Read(ctx context.Context, options ReadOptions) (retErr error) {
|
||||||
filterMap, err := generateEventFilters(options.Filters, options.Since, options.Until)
|
filterMap, err := generateEventFilters(options.Filters, options.Since, options.Until)
|
||||||
@ -224,18 +250,8 @@ func newEventFromJournalEntry(entry *sdjournal.JournalEntry) (*Event, error) {
|
|||||||
newEvent.ContainerExitCode = &intCode
|
newEvent.ContainerExitCode = &intCode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err := getLabelsFromJournal(entry, &newEvent); err != nil {
|
||||||
// we need to check for the presence of labels recorded to a container event
|
return nil, err
|
||||||
if stringLabels, ok := entry.Fields["PODMAN_LABELS"]; ok && len(stringLabels) > 0 {
|
|
||||||
labels := make(map[string]string, 0)
|
|
||||||
if err := json.Unmarshal([]byte(stringLabels), &labels); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we have labels, add them to the event
|
|
||||||
if len(labels) > 0 {
|
|
||||||
newEvent.Attributes = labels
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
newEvent.HealthStatus = entry.Fields["PODMAN_HEALTH_STATUS"]
|
newEvent.HealthStatus = entry.Fields["PODMAN_HEALTH_STATUS"]
|
||||||
if log, ok := entry.Fields["PODMAN_HEALTH_LOG"]; ok {
|
if log, ok := entry.Fields["PODMAN_HEALTH_LOG"]; ok {
|
||||||
@ -251,6 +267,9 @@ func newEventFromJournalEntry(entry *sdjournal.JournalEntry) (*Event, error) {
|
|||||||
case Network:
|
case Network:
|
||||||
newEvent.ID = entry.Fields["PODMAN_ID"]
|
newEvent.ID = entry.Fields["PODMAN_ID"]
|
||||||
newEvent.Network = entry.Fields["PODMAN_NETWORK_NAME"]
|
newEvent.Network = entry.Fields["PODMAN_NETWORK_NAME"]
|
||||||
|
if err := getLabelsFromJournal(entry, &newEvent); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case Image:
|
case Image:
|
||||||
newEvent.ID = entry.Fields["PODMAN_ID"]
|
newEvent.ID = entry.Fields["PODMAN_ID"]
|
||||||
if val, ok := entry.Fields["ERROR"]; ok {
|
if val, ok := entry.Fields["ERROR"]; ok {
|
||||||
|
@ -20,6 +20,8 @@ load helpers.network
|
|||||||
run_podman network ls -n
|
run_podman network ls -n
|
||||||
assert "$output" !~ "$heading" "network ls -n shows header anyway"
|
assert "$output" !~ "$heading" "network ls -n shows header anyway"
|
||||||
|
|
||||||
|
since=$(date --iso-8601=seconds)
|
||||||
|
|
||||||
# check deterministic list order
|
# check deterministic list order
|
||||||
local net1=net-a-$(safename)
|
local net1=net-a-$(safename)
|
||||||
local net2=net-b-$(safename)
|
local net2=net-b-$(safename)
|
||||||
@ -28,12 +30,22 @@ load helpers.network
|
|||||||
run_podman network create $net2
|
run_podman network create $net2
|
||||||
run_podman network create $net3
|
run_podman network create $net3
|
||||||
|
|
||||||
|
# Quick check that we generate events
|
||||||
|
run_podman events --filter type=network --since $since --stream=false
|
||||||
|
assert "$output" =~ "network create [0-9a-f]{64} \(name=$net1, type=bridge\)" "network1 create event"
|
||||||
|
assert "$output" =~ "network create [0-9a-f]{64} \(name=$net2, type=bridge\)" "network2 create event"
|
||||||
|
assert "$output" =~ "network create [0-9a-f]{64} \(name=$net3, type=bridge\)" "network3 create event"
|
||||||
|
|
||||||
run_podman network ls --quiet
|
run_podman network ls --quiet
|
||||||
# just check that the order of the created networks is correct
|
# just check that the order of the created networks is correct
|
||||||
# we cannot do an exact match since developer and CI systems could contain more networks
|
# we cannot do an exact match since developer and CI systems could contain more networks
|
||||||
is "$output" ".*$net1.*$net2.*$net3.*podman.*" "networks sorted alphabetically"
|
is "$output" ".*$net1.*$net2.*$net3.*podman.*" "networks sorted alphabetically"
|
||||||
|
|
||||||
run_podman network rm $net1 $net2 $net3
|
run_podman network rm $net1 $net2 $net3
|
||||||
|
run_podman events --filter type=network --since $since --stream=false
|
||||||
|
assert "$output" =~ "network remove [0-9a-f]{64} \(name=$net1, type=bridge\)" "network1 remove event"
|
||||||
|
assert "$output" =~ "network remove [0-9a-f]{64} \(name=$net2, type=bridge\)" "network2 remove event"
|
||||||
|
assert "$output" =~ "network remove [0-9a-f]{64} \(name=$net3, type=bridge\)" "network3 remove event"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Copied from tsweeney's https://github.com/containers/podman/issues/4827
|
# Copied from tsweeney's https://github.com/containers/podman/issues/4827
|
||||||
|
Reference in New Issue
Block a user