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

bugfix: distinct init process exit and exec process exit #2347

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ paths:
description: |
Stream real-time events from the server.
Report various object events of pouchd when something happens to them.
Containers report these events: create`, `destroy`, `die`, `oom`, `pause`, `rename`, `resize`, `restart`, `start`, `stop`, `top`, `unpause`, and `update`
Containers report these events: create`, `destroy`, `die`, `oom`, `pause`, `rename`, `resize`, `restart`, `start`, `stop`, `top`, `unpause`, `update` and `exec_die`
Images report these events: `pull`, `untag`
Volumes report these events: `create`, `destroy`
Networks report these events: `create`, `connect`, `disconnect`, `destroy`
Expand Down
9 changes: 7 additions & 2 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,14 @@ func (c *Client) collectContainerdEvents() {
logrus.Warnf("failed to parse %s event: %#v", TaskExitEventTopic, out)
continue
}
action = "die"
if exitEvent.ID == exitEvent.ContainerID {
action = "die"
} else {
action = "exec_die"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In swagger.yml, we defined the /events API like the following:

  /events:
    get:
      summary: "Subscribe pouchd events to users"
      description: |
        Stream real-time events from the server.
        Report various object events of pouchd when something happens to them.
        Containers report these events: create`, `destroy`, `die`, `oom`, `pause`, `rename`, `resize`, `restart`, `start`, `stop`, `top`, `unpause`, and `update`
        Images report these events: `pull`, `untag`
        Volumes report these events: `create`, `destroy`
        Networks report these events: `create`, `connect`, `disconnect`, `destroy`

But here, I a afraid that we are adding another event type for container : exec's exec_die, right?
While this has not been collected in to API illustration. If we need to add this in swagger.yml, we should fix that. @HusterWan
And maybe we could have a very thorough thinking for the all workflow of the change: API, daemon, containerd, doc, test and some others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great advise, i will update this or soon

attributes["execID"] = exitEvent.ID
}
containerID = exitEvent.ContainerID
attributes["exitcode"] = strconv.Itoa(int(exitEvent.ExitStatus))
attributes["exitCode"] = strconv.Itoa(int(exitEvent.ExitStatus))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to make this exitcode uppercase? Will this bring some compatible issues for some early adopters? @HusterWan

Copy link
Contributor Author

@HusterWan HusterWan Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have confirmed with other adopters that used the events policy of pouchd, they had done compability with the exitCode.

So in order to stay with moby, i change the exitcode to exitCode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add the event document later

case TaskOOMEventTopic:
oomEvent, ok := out.(*eventstypes.TaskOOM)
if !ok {
Expand Down
93 changes: 93 additions & 0 deletions test/cli_events_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -51,3 +52,95 @@ func (suite *PouchEventsSuite) TestEventsWorks(c *check.C) {
c.Errorf("unexpected output %s: should contains create and start events\n", out)
}
}

func delEmptyStrInSlice(strSlice []string) []string {
if len(strSlice) == 0 {
return strSlice
}

newSlice := []string{}
for _, v := range strSlice {
if v != "" {
newSlice = append(newSlice, v)
}
}

return newSlice
}

// TestExecDieEventWorks tests exec_die event work.
func (suite *PouchEventsSuite) TestExecDieEventWorks(c *check.C) {
name := "test-exec-die-event-works"

res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

// only works when test case run on the same machine with pouchd
time.Sleep(1100 * time.Millisecond)
start := time.Now()
command.PouchRun("exec", name, "echo", "test").Assert(c, icmd.Success)
time.Sleep(1100 * time.Millisecond)
end := time.Now()

since, until := start.Format(time.RFC3339), end.Format(time.RFC3339)
res = command.PouchRun("events", "--since", since, "--until", until)
output := res.Combined()

// check output contains exec_die event
lines := delEmptyStrInSlice(strings.Split(output, "\n"))
if len(lines) != 1 {
c.Errorf("unexpected output %s: should just contains 1 line", output)
}

if err := checkContainerEvent(lines[0], "exec_die"); err != nil {
c.Errorf("exec_die event check error: %v", err)
}
}

// TestDieEventWorks tests container die event work.
func (suite *PouchEventsSuite) TestDieEventWorks(c *check.C) {
name := "test-die-event-works"

res := command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

// only works when test case run on the same machine with pouchd
time.Sleep(1100 * time.Millisecond)
start := time.Now()
command.PouchRun("stop", name).Assert(c, icmd.Success)
time.Sleep(1100 * time.Millisecond)
end := time.Now()

since, until := start.Format(time.RFC3339), end.Format(time.RFC3339)
res = command.PouchRun("events", "--since", since, "--until", until)
output := res.Combined()

// check events when stop a container
lines := delEmptyStrInSlice(strings.Split(output, "\n"))
if len(lines) != 2 {
c.Errorf("unexpected output %s: should contains 2 event line when stop a container", output)
}

if err := checkContainerEvent(lines[0], "die"); err != nil {
c.Errorf("die event check error: %v", err)
}

if err := checkContainerEvent(lines[1], "stop"); err != nil {
c.Errorf("exec_die event check error: %v", err)
}
}

func checkContainerEvent(eventStr, eventType string) error {
strSlice := strings.Split(eventStr, " ")
if len(strSlice) < 4 {
return fmt.Errorf("unexpected output %s: output line may not be container event", eventStr)
}

if strSlice[1] != "container" || strSlice[2] != eventType {
return fmt.Errorf("unexpected output %s: should be %s events", eventStr, eventType)
}

return nil
}