Skip to content
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
6 changes: 5 additions & 1 deletion libcontainer/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package libcontainer

import (
"os"
"time"

"github.com/opencontainers/runc/libcontainer/configs"
)
Expand Down Expand Up @@ -56,9 +57,12 @@ type BaseState struct {
// InitProcessPid is the init process id in the parent namespace.
InitProcessPid int `json:"init_process_pid"`

// InitProcessStartTime is the init process start time.
// InitProcessStartTime is the init process start time in clock cycles since boot time.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Can we instead say System Time?

Copy link
Member Author

Choose a reason for hiding this comment

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

changed the new one to creation timestamp per your suggestion..

This old one for init process start is a legacy naming issue.. in so far as it's been named and used this way for a while... I was just updating the comment to be more explanatory

InitProcessStartTime string `json:"init_process_start"`

// CreatedTime is the unix timestamp for the creation time of the container in UTC
CreatedTime time.Time `json:"created_time"`

// Config is the container's configuration.
Config configs.Config `json:"config"`
}
Expand Down
11 changes: 11 additions & 0 deletions libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/Sirupsen/logrus"
"github.com/golang/protobuf/proto"
Expand All @@ -38,6 +39,7 @@ type linuxContainer struct {
m sync.Mutex
criuVersion int
state containerState
createdTime time.Time
}

// State represents a running container's state
Expand Down Expand Up @@ -117,6 +119,11 @@ func (c *linuxContainer) ID() string {
return c.id
}

// CreatedTime returns the timestamp from when the container was CREATED
func (c *linuxContainer) CreatedTime() time.Time {
return c.createdTime
}

// Config returns the container's configuration
func (c *linuxContainer) Config() configs.Config {
return *c.config
Expand Down Expand Up @@ -189,6 +196,9 @@ func (c *linuxContainer) Start(process *Process) error {
}
return newSystemError(err)
}
// generate a timestamp indicating when the container was started
c.createdTime = time.Now().UTC()

c.state = &runningState{
c: c,
}
Expand Down Expand Up @@ -1042,6 +1052,7 @@ func (c *linuxContainer) currentState() (*State, error) {
Config: *c.config,
InitProcessPid: pid,
InitProcessStartTime: startTime,
CreatedTime: c.createdTime,
},
CgroupPaths: c.cgroupManager.GetPaths(),
NamespacePaths: make(map[configs.NamespaceType]string),
Expand Down
1 change: 1 addition & 0 deletions libcontainer/factory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
criuPath: l.CriuPath,
cgroupManager: l.NewCgroupsManager(state.Config.Cgroups, state.CgroupPaths),
root: containerRoot,
createdTime: state.CreatedTime,
}
c.state = &nullState{c: c}
if err := c.refreshState(); err != nil {
Expand Down
83 changes: 83 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// +build linux

package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/tabwriter"
"time"

"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)

var listCommand = cli.Command{
Name: "list",
Usage: "lists containers started by runc with the given root",
Action: func(context *cli.Context) {

// preload the container factory
if factory == nil {
err := factoryPreload(context)
if err != nil {
logrus.Fatal(err)
return
}
}

// get the list of containers
root := context.GlobalString("root")
absRoot, err := filepath.Abs(root)
if err != nil {
logrus.Fatal(err)
return
}
list, err := ioutil.ReadDir(absRoot)

Copy link
Member

Choose a reason for hiding this comment

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

You should use a tabwriter for this type of output. It is more robust way to handle it and easier for ppl to parse.

reference:

https://github.com/docker/containerd/blob/master/ctr/container.go#L64

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good will redo it using tabwritter in the morning.

w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0)
fmt.Fprint(w, "ID\tPID\tSTATUS\tCREATED\n")

// output containers
for _, item := range list {
switch {
case !item.IsDir():
// do nothing with misc files in the containers directory
case item.IsDir():
outputListInfo(item.Name(), w)
}
}

if err := w.Flush(); err != nil {
logrus.Fatal(err)
}
},
}

func outputListInfo(id string, w *tabwriter.Writer) {
container, err := factory.Load(id)
if err != nil {
logrus.Fatal(err)
return
}

containerStatus, err := container.Status()
if err != nil {
logrus.Fatal(err)
return
}

state, err := container.State()
if err != nil {
logrus.Fatal(err)
return
}

fmt.Fprintf(w, "%s\t%d\t%s\t%s\n", container.ID(),
state.BaseState.InitProcessPid,
containerStatus.String(),
state.BaseState.CreatedTime.Format(time.RFC3339Nano))

}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
pauseCommand,
resumeCommand,
execCommand,
listCommand,
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") {
Expand Down