Merge pull request #15463 from mheon/fix_15408

Events for containers in pods now include the pod's ID
This commit is contained in:
OpenShift Merge Robot
2022-09-23 20:39:56 +02:00
committed by GitHub
7 changed files with 32 additions and 5 deletions

View File

@ -34,6 +34,7 @@ func (c *Container) newContainerEvent(status events.Status) {
e.Details = events.Details{ e.Details = events.Details{
ID: e.ID, ID: e.ID,
PodID: c.PodID(),
Attributes: c.Labels(), Attributes: c.Labels(),
} }
@ -59,6 +60,7 @@ func (c *Container) newContainerExitedEvent(exitCode int32) {
e.Name = c.Name() e.Name = c.Name()
e.Image = c.config.RootfsImageName e.Image = c.config.RootfsImageName
e.Type = events.Container e.Type = events.Container
e.PodID = c.PodID()
e.ContainerExitCode = int(exitCode) e.ContainerExitCode = int(exitCode)
e.Details = events.Details{ e.Details = events.Details{

View File

@ -50,6 +50,8 @@ type Event struct {
type Details struct { type Details struct {
// ID is the event ID // ID is the event ID
ID string ID string
// PodID is the ID of the pod associated with the container.
PodID string `json:",omitempty"`
// Attributes can be used to describe specifics about the event // Attributes can be used to describe specifics about the event
// in the case of a container event, labels for example // in the case of a container event, labels for example
Attributes map[string]string Attributes map[string]string

View File

@ -76,7 +76,13 @@ func (e *Event) ToHumanReadable(truncate bool) string {
} }
switch e.Type { switch e.Type {
case Container, Pod: case Container, Pod:
humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s, health_status=%s", e.Time, e.Type, e.Status, id, e.Image, e.Name, e.HealthStatus) humanFormat = fmt.Sprintf("%s %s %s %s (image=%s, name=%s", e.Time, e.Type, e.Status, id, e.Image, e.Name)
if e.PodID != "" {
humanFormat += fmt.Sprintf(", pod_id=%s", e.PodID)
}
if e.HealthStatus != "" {
humanFormat += fmt.Sprintf(", health_status=%s", e.HealthStatus)
}
// check if the container has labels and add it to the output // check if the container has labels and add it to the output
if len(e.Attributes) > 0 { if len(e.Attributes) > 0 {
for k, v := range e.Attributes { for k, v := range e.Attributes {

View File

@ -50,6 +50,9 @@ func (e EventJournalD) Write(ee Event) error {
if ee.ContainerExitCode != 0 { if ee.ContainerExitCode != 0 {
m["PODMAN_EXIT_CODE"] = strconv.Itoa(ee.ContainerExitCode) m["PODMAN_EXIT_CODE"] = strconv.Itoa(ee.ContainerExitCode)
} }
if ee.PodID != "" {
m["PODMAN_POD_ID"] = ee.PodID
}
// If we have container labels, we need to convert them to a string so they // If we have container labels, we need to convert them to a string so they
// can be recorded with the event // can be recorded with the event
if len(ee.Details.Attributes) > 0 { if len(ee.Details.Attributes) > 0 {
@ -161,6 +164,7 @@ func newEventFromJournalEntry(entry *sdjournal.JournalEntry) (*Event, error) {
case Container, Pod: case Container, Pod:
newEvent.ID = entry.Fields["PODMAN_ID"] newEvent.ID = entry.Fields["PODMAN_ID"]
newEvent.Image = entry.Fields["PODMAN_IMAGE"] newEvent.Image = entry.Fields["PODMAN_IMAGE"]
newEvent.PodID = entry.Fields["PODMAN_POD_ID"]
if code, ok := entry.Fields["PODMAN_EXIT_CODE"]; ok { if code, ok := entry.Fields["PODMAN_EXIT_CODE"]; ok {
intCode, err := strconv.Atoi(code) intCode, err := strconv.Atoi(code)
if err != nil { if err != nil {
@ -179,7 +183,7 @@ func newEventFromJournalEntry(entry *sdjournal.JournalEntry) (*Event, error) {
// if we have labels, add them to the event // if we have labels, add them to the event
if len(labels) > 0 { if len(labels) > 0 {
newEvent.Details = Details{Attributes: labels} newEvent.Attributes = labels
} }
} }
newEvent.HealthStatus = entry.Fields["PODMAN_HEALTH_STATUS"] newEvent.HealthStatus = entry.Fields["PODMAN_HEALTH_STATUS"]

View File

@ -14,7 +14,7 @@ type Event struct {
// TODO: it would be nice to have full control over the types at some // TODO: it would be nice to have full control over the types at some
// point and fork such Docker types. // point and fork such Docker types.
dockerEvents.Message dockerEvents.Message
HealthStatus string HealthStatus string `json:",omitempty"`
} }
// ConvertToLibpodEvent converts an entities event to a libpod one. // ConvertToLibpodEvent converts an entities event to a libpod one.
@ -34,6 +34,7 @@ func ConvertToLibpodEvent(e Event) *libpodEvents.Event {
image := e.Actor.Attributes["image"] image := e.Actor.Attributes["image"]
name := e.Actor.Attributes["name"] name := e.Actor.Attributes["name"]
details := e.Actor.Attributes details := e.Actor.Attributes
podID := e.Actor.Attributes["podId"]
delete(details, "image") delete(details, "image")
delete(details, "name") delete(details, "name")
delete(details, "containerExitCode") delete(details, "containerExitCode")
@ -47,6 +48,7 @@ func ConvertToLibpodEvent(e Event) *libpodEvents.Event {
Type: t, Type: t,
HealthStatus: e.HealthStatus, HealthStatus: e.HealthStatus,
Details: libpodEvents.Details{ Details: libpodEvents.Details{
PodID: podID,
Attributes: details, Attributes: details,
}, },
} }
@ -61,6 +63,7 @@ func ConvertToEntitiesEvent(e libpodEvents.Event) *Event {
attributes["image"] = e.Image attributes["image"] = e.Image
attributes["name"] = e.Name attributes["name"] = e.Name
attributes["containerExitCode"] = strconv.Itoa(e.ContainerExitCode) attributes["containerExitCode"] = strconv.Itoa(e.ContainerExitCode)
attributes["podId"] = e.PodID
message := dockerEvents.Message{ message := dockerEvents.Message{
// Compatibility with clients that still look for deprecated API elements // Compatibility with clients that still look for deprecated API elements
Status: e.Status.String(), Status: e.Status.String(),

View File

@ -20,7 +20,7 @@ t GET "libpod/events?stream=false&since=$START" 200 \
t GET "libpod/events?stream=false&since=$START" 200 \ t GET "libpod/events?stream=false&since=$START" 200 \
'select(.status | contains("start")).Action=start' \ 'select(.status | contains("start")).Action=start' \
'select(.status | contains("start")).HealthStatus='\ 'select(.status | contains("start")).HealthStatus=null'\
# compat api, uses status=die (#12643) # compat api, uses status=die (#12643)
t GET "events?stream=false&since=$START" 200 \ t GET "events?stream=false&since=$START" 200 \

View File

@ -212,6 +212,16 @@ var _ = Describe("Podman events", func() {
Expect(result).Should(Exit(0)) Expect(result).Should(Exit(0))
Expect(result.OutputToStringArray()).To(HaveLen(1)) Expect(result.OutputToStringArray()).To(HaveLen(1))
Expect(result.OutputToString()).To(ContainSubstring("create")) Expect(result.OutputToString()).To(ContainSubstring("create"))
ctrName := "testCtr"
run := podmanTest.Podman([]string{"create", "--pod", id, "--name", ctrName, ALPINE, "top"})
run.WaitWithDefaultTimeout()
Expect(run).Should(Exit(0))
result2 := podmanTest.Podman([]string{"events", "--stream=false", "--filter", fmt.Sprintf("container=%s", ctrName), "--since", "30s"})
result2.WaitWithDefaultTimeout()
Expect(result2).Should(Exit(0))
Expect(result2.OutputToString()).To(ContainSubstring(fmt.Sprintf("pod_id=%s", id)))
}) })
It("podman events health_status generated", func() { It("podman events health_status generated", func() {
@ -229,7 +239,7 @@ var _ = Describe("Podman events", func() {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=health_status"}) result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", "event=health_status", "--since", "1m"})
result.WaitWithDefaultTimeout() result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0)) Expect(result).Should(Exit(0))
Expect(len(result.OutputToStringArray())).To(BeNumerically(">=", 1), "Number of health_status events") Expect(len(result.OutputToStringArray())).To(BeNumerically(">=", 1), "Number of health_status events")