Skip to content

Commit 4171ca0

Browse files
Merge pull request containerd#391 from mlaventure/restore-and-cleanup
Add restore and small cleanup
2 parents 58640c9 + 0fdd246 commit 4171ca0

File tree

15 files changed

+248
-83
lines changed

15 files changed

+248
-83
lines changed

cmd/containerd/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ high performance container runtime
119119
}
120120
defer nec.Close()
121121

122-
execService, err := execution.New(executor)
122+
ctx := log.WithModule(gocontext.Background(), "containerd")
123+
ctx = log.WithModule(ctx, "execution")
124+
ctx = events.WithPoster(ctx, events.GetNATSPoster(nec))
125+
execService, err := execution.New(ctx, executor)
123126
if err != nil {
124127
return err
125128
}

cmd/ctr/events.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/nats-io/go-nats"
9+
"github.com/urfave/cli"
10+
)
11+
12+
var eventsCommand = cli.Command{
13+
Name: "events",
14+
Usage: "display containerd events",
15+
Flags: []cli.Flag{
16+
cli.StringFlag{
17+
Name: "subject, s",
18+
Usage: "subjects filter",
19+
Value: "containerd.>",
20+
},
21+
},
22+
Action: func(context *cli.Context) error {
23+
nc, err := nats.Connect(nats.DefaultURL)
24+
if err != nil {
25+
return err
26+
}
27+
nec, err := nats.NewEncodedConn(nc, nats.JSON_ENCODER)
28+
if err != nil {
29+
nc.Close()
30+
return err
31+
}
32+
defer nec.Close()
33+
34+
evCh := make(chan *nats.Msg, 64)
35+
sub, err := nec.Subscribe(context.String("subject"), func(e *nats.Msg) {
36+
evCh <- e
37+
})
38+
if err != nil {
39+
return err
40+
}
41+
defer sub.Unsubscribe()
42+
43+
for {
44+
e, more := <-evCh
45+
if !more {
46+
break
47+
}
48+
49+
var prettyJSON bytes.Buffer
50+
51+
err := json.Indent(&prettyJSON, e.Data, "", "\t")
52+
if err != nil {
53+
fmt.Println(string(e.Data))
54+
} else {
55+
fmt.Println(prettyJSON.String())
56+
}
57+
}
58+
59+
return nil
60+
},
61+
}

cmd/ctr/exec.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var execCommand = cli.Command{
1818
Name: "id, i",
1919
Usage: "target container id",
2020
},
21+
cli.StringFlag{
22+
Name: "pid, p",
23+
Usage: "new process id",
24+
},
2125
cli.StringFlag{
2226
Name: "cwd, c",
2327
Usage: "current working directory for the process",
@@ -48,6 +52,7 @@ var execCommand = cli.Command{
4852
sOpts := &execution.StartProcessRequest{
4953
ContainerID: id,
5054
Process: &execution.Process{
55+
ID: context.String("pid"),
5156
Cwd: context.String("cwd"),
5257
Terminal: context.Bool("tty"),
5358
Args: context.Args(),

cmd/ctr/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ containerd client
3636
app.Commands = []cli.Command{
3737
runCommand,
3838
execCommand,
39+
eventsCommand,
3940
}
4041
app.Before = func(context *cli.Context) error {
4142
if context.GlobalBool("debug") {

cmd/ctr/run.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"time"
78

89
gocontext "context"
910

@@ -110,15 +111,23 @@ var runCommand = cli.Command{
110111
}
111112

112113
var ec uint32
114+
eventLoop:
113115
for {
114-
e, more := <-evCh
115-
if !more {
116-
break
117-
}
118-
119-
if e.ID == cr.Container.ID && e.PID == cr.InitProcess.ID {
120-
ec = e.StatusCode
121-
break
116+
select {
117+
case e, more := <-evCh:
118+
if !more {
119+
fmt.Println("No More!")
120+
break eventLoop
121+
}
122+
123+
if e.ID == cr.Container.ID && e.PID == cr.InitProcess.ID {
124+
ec = e.StatusCode
125+
break eventLoop
126+
}
127+
case <-time.After(1 * time.Second):
128+
if nec.Conn.Status() != nats.CONNECTED {
129+
break eventLoop
130+
}
122131
}
123132
}
124133

execution/container.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ func NewContainer(stateRoot, id, bundle string) (*Container, error) {
1111
id: id,
1212
bundle: bundle,
1313
stateDir: stateDir,
14-
status: "created",
14+
status: Created,
1515
processes: make(map[string]Process),
1616
}, nil
1717
}
1818

19-
func LoadContainer(dir StateDir, id, bundle, status string, initPid int64) *Container {
19+
func LoadContainer(dir StateDir, id, bundle string, status Status, initPid int64) *Container {
2020
return &Container{
2121
id: id,
2222
stateDir: dir,
@@ -32,7 +32,7 @@ type Container struct {
3232
bundle string
3333
stateDir StateDir
3434
initPid int64
35-
status string
35+
status Status
3636

3737
processes map[string]Process
3838
}
@@ -41,7 +41,13 @@ func (c *Container) ID() string {
4141
return c.id
4242
}
4343

44-
func (c *Container) Status() string {
44+
func (c *Container) Status() Status {
45+
for _, p := range c.processes {
46+
if p.Pid() == c.initPid {
47+
c.status = p.Status()
48+
break
49+
}
50+
}
4551
return c.status
4652
}
4753

execution/error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package execution
2+
3+
import "fmt"
4+
5+
var (
6+
ErrProcessNotFound = fmt.Errorf("process not found")
7+
)

execution/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type CreateOpts struct {
1616
}
1717

1818
type StartProcessOpts struct {
19+
ID string
1920
Spec specs.Process
2021
Console bool
2122
Stdin string

execution/executors/oci/oci.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ import (
1212
"github.com/docker/containerd/execution"
1313
)
1414

15+
const (
16+
initProcessID = "init"
17+
)
18+
19+
const (
20+
PidFilename = "pid"
21+
StartTimeFilename = "starttime"
22+
)
23+
1524
var (
1625
ErrRootEmpty = errors.New("oci: runtime root cannot be an empty string")
1726
)
@@ -59,11 +68,11 @@ func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOp
5968
}
6069
}(container)
6170

62-
initProcID, initStateDir, err := container.StateDir().NewProcess()
71+
initStateDir, err := container.StateDir().NewProcess(initProcessID)
6372
if err != nil {
6473
return nil, err
6574
}
66-
pidFile := filepath.Join(initStateDir, "pid")
75+
pidFile := filepath.Join(initStateDir, PidFilename)
6776
err = r.runc.Create(ctx, id, o.Bundle, &runc.CreateOpts{
6877
PidFile: pidFile,
6978
Console: oio.console,
@@ -79,11 +88,7 @@ func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOp
7988
}
8089
}()
8190

82-
pid, err := runc.ReadPidFile(pidFile)
83-
if err != nil {
84-
return nil, err
85-
}
86-
process, err := newProcess(initProcID, pid)
91+
process, err := newProcess(initProcessID, initStateDir, execution.Created)
8792
if err != nil {
8893
return nil, err
8994
}
@@ -112,7 +117,7 @@ func (r *OCIRuntime) load(runcC *runc.Container) (*execution.Container, error) {
112117
execution.StateDir(filepath.Join(r.root, runcC.ID)),
113118
runcC.ID,
114119
runcC.Bundle,
115-
runcC.Status,
120+
execution.Status(runcC.Status),
116121
int64(runcC.Pid),
117122
)
118123

@@ -121,19 +126,11 @@ func (r *OCIRuntime) load(runcC *runc.Container) (*execution.Container, error) {
121126
return nil, err
122127
}
123128
for _, d := range dirs {
124-
pid, err := runc.ReadPidFile(filepath.Join(d, "pid"))
125-
if err != nil {
126-
if os.IsNotExist(err) {
127-
// Process died in between
128-
continue
129-
}
130-
return nil, err
131-
}
132-
process, err := newProcess(filepath.Base(d), pid)
129+
process, err := newProcess(filepath.Base(d), d, execution.Running)
133130
if err != nil {
134131
return nil, err
135132
}
136-
container.AddProcess(process, pid == runcC.Pid)
133+
container.AddProcess(process, process.Pid() == int64(runcC.Pid))
137134
}
138135

139136
return container, nil
@@ -201,17 +198,17 @@ func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o
201198
}
202199
}()
203200

204-
procID, procStateDir, err := c.StateDir().NewProcess()
201+
procStateDir, err := c.StateDir().NewProcess(o.ID)
205202
if err != nil {
206203
return nil, err
207204
}
208205
defer func() {
209206
if err != nil {
210-
c.StateDir().DeleteProcess(procID)
207+
c.StateDir().DeleteProcess(o.ID)
211208
}
212209
}()
213210

214-
pidFile := filepath.Join(procStateDir, "pid")
211+
pidFile := filepath.Join(procStateDir, PidFilename)
215212
if err := r.runc.Exec(ctx, c.ID(), o.Spec, &runc.ExecOpts{
216213
PidFile: pidFile,
217214
Detach: false,
@@ -221,12 +218,8 @@ func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o
221218
}); err != nil {
222219
return nil, err
223220
}
224-
pid, err := runc.ReadPidFile(pidFile)
225-
if err != nil {
226-
return nil, err
227-
}
228221

229-
process, err := newProcess(procID, pid)
222+
process, err := newProcess(o.ID, procStateDir, execution.Running)
230223
if err != nil {
231224
return nil, err
232225
}

0 commit comments

Comments
 (0)