Skip to content

Commit

Permalink
Merge pull request #2286 from allencloud/add-logpath
Browse files Browse the repository at this point in the history
feature: add LogPath in Inspect Response
  • Loading branch information
fuweid authored Sep 29, 2018
2 parents 8be6286 + b961e22 commit 85ae3b7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
State: c.State,
Config: c.Config,
HostConfig: c.HostConfig,
LogPath: c.LogPath,
Snapshotter: c.Snapshotter,
RestartCount: c.RestartCount,
GraphDriver: &types.GraphDriverData{
Expand Down
3 changes: 3 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ func (mgr *ContainerManager) createContainerdContainer(ctx context.Context, c *C
return errors.Wrap(err, "failed to open io")
}

// set container's LogPath
mgr.SetContainerLogPath(c)

runtime, err := mgr.getRuntime(c.HostConfig.Runtime)
if err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions daemon/mgr/container_logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mgr

import (
"path/filepath"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/containerio"
"github.com/alibaba/pouch/daemon/logger"
Expand Down Expand Up @@ -48,3 +50,18 @@ func (mgr *ContainerManager) convContainerToLoggerInfo(c *Container) logger.Info
DaemonName: "pouchd",
}
}

// SetContainerLogPath sets the log path of container.
// LogPath would be as a field in `Inspect` response.
func (mgr *ContainerManager) SetContainerLogPath(c *Container) {
if c.HostConfig.LogConfig == nil {
return
}

// If the logdriver is json-file, the LogPath should be like
// /var/lib/pouch/containers/5804ee42e505a5d9f30128848293fcb72d8cbc7517310bd24895e82a618fa454/json.log
if c.HostConfig.LogConfig.LogDriver == "json-file" {
c.LogPath = filepath.Join(mgr.Config.HomeDir, "containers", c.ID, "json.log")
}
return
}
21 changes: 18 additions & 3 deletions test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func (suite *PouchInspectSuite) SetUpSuite(c *check.C) {
func (suite *PouchInspectSuite) TearDownTest(c *check.C) {
}

// TestInspectFormat is to verify the format flag of inspect command.
func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
name := "inspect-format-print"
// TestInspectCreateAndStartedFormat is to verify the format flag of inspect command.
func (suite *PouchInspectSuite) TestInspectCreateAndStartedFormat(c *check.C) {
name := "TestInspectCreateAndStartedFormat"

// create a raw container
res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
Expand All @@ -55,6 +56,20 @@ func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
// inspect Memory
output = command.PouchRun("inspect", "-f", "{{.HostConfig.Memory}}", name).Stdout()
c.Assert(output, check.Equals, fmt.Sprintf("%d\n", result[0].HostConfig.Memory))

// Inspect LogPath, LogPath should be empty before container's start
output = command.PouchRun("inspect", "-f", "{{.LogPath}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "")

// start the created container
res = command.PouchRun("start", name)
res.Assert(c, icmd.Success)

// Inspect LogPath, LogPath should not be empty after container's start.
// by default, the container has log type of json-file.
output = command.PouchRun("inspect", "-f", "{{.LogPath}}", name).Stdout()
expectedLogPath := fmt.Sprintf("/var/lib/pouch/containers/%s/json.log", containerID)
c.Assert(strings.TrimSpace(output), check.Equals, expectedLogPath)
}

// TestInspectWrongFormat is to verify using wrong format flag of inspect command.
Expand Down

0 comments on commit 85ae3b7

Please sign in to comment.