Skip to content

Commit b93a33b

Browse files
authored
Merge pull request containerd#274 from crosbymichael/start-sync
Call start in containerd
2 parents 14e7949 + 654c537 commit b93a33b

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

containerd-shim/main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ func start(log *os.File) error {
136136
Height: uint16(msg.Height),
137137
}
138138
term.SetWinsize(p.console.Fd(), &ws)
139-
case 2:
140-
// tell runtime to execute the init process
141-
if err := p.start(); err != nil {
142-
p.delete()
143-
p.Wait()
144-
return err
145-
}
146139
}
147140
}
148141
}

containerd-shim/process.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,6 @@ func (p *process) create() error {
206206
return nil
207207
}
208208

209-
func (p *process) start() error {
210-
cwd, err := os.Getwd()
211-
if err != nil {
212-
return err
213-
}
214-
logPath := filepath.Join(cwd, "log.json")
215-
args := append([]string{
216-
"--log", logPath,
217-
"--log-format", "json",
218-
}, p.state.RuntimeArgs...)
219-
args = append(args, "start", p.id)
220-
cmd := exec.Command(p.runtime, args...)
221-
cmd.Dir = p.bundle
222-
cmd.Stdin = p.stdio.stdin
223-
cmd.Stdout = p.stdio.stdout
224-
cmd.Stderr = p.stdio.stderr
225-
cmd.SysProcAttr = setPDeathSig()
226-
return cmd.Run()
227-
}
228-
229209
func (p *process) pid() int {
230210
return p.containerPid
231211
}

integration-test/start_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"syscall"
78
"time"
@@ -326,6 +327,9 @@ func (cs *ContainerdSuite) TestOOM(t *check.C) {
326327
spec.Linux.Resources.Memory = &ocs.Memory{
327328
Limit: &limit,
328329
}
330+
if swapEnabled() {
331+
spec.Linux.Resources.Memory.Swap = &limit
332+
}
329333
}); err != nil {
330334
t.Fatal(err)
331335
}
@@ -362,7 +366,7 @@ func (cs *ContainerdSuite) TestOOM(t *check.C) {
362366
evt.Timestamp = e.Timestamp
363367
t.Assert(*e, checker.Equals, evt)
364368
case <-time.After(60 * time.Second):
365-
t.Fatal("Container took more than 10 seconds to terminate")
369+
t.Fatalf("Container took more than 60 seconds to %s", evt.Type)
366370
}
367371
}
368372
}
@@ -479,3 +483,8 @@ func (cs *ContainerdSuite) TestRestart(t *check.C) {
479483
t.Assert(containers[i].Status, checker.Equals, "running")
480484
}
481485
}
486+
487+
func swapEnabled() bool {
488+
_, err := os.Stat("/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes")
489+
return err == nil
490+
}

runtime/process.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,33 @@ func (p *process) Signal(s os.Signal) error {
260260
// This should only be called on the process with ID "init"
261261
func (p *process) Start() error {
262262
if p.ID() == InitProcessID {
263-
_, err := fmt.Fprintf(p.controlPipe, "%d %d %d\n", 2, 0, 0)
264-
return err
263+
var (
264+
errC = make(chan error, 1)
265+
shimExit = make(chan struct{}, 1)
266+
args = append(p.container.runtimeArgs, "start", p.container.id)
267+
cmd = exec.Command(p.container.runtime, args...)
268+
)
269+
go func() {
270+
out, err := cmd.CombinedOutput()
271+
if err != nil {
272+
errC <- fmt.Errorf("%s: %q", err.Error(), out)
273+
}
274+
errC <- nil
275+
}()
276+
go func() {
277+
p.Wait()
278+
close(shimExit)
279+
}()
280+
select {
281+
case err := <-errC:
282+
if err != nil {
283+
return err
284+
}
285+
case <-shimExit:
286+
cmd.Process.Kill()
287+
cmd.Wait()
288+
return ErrShimExited
289+
}
265290
}
266291
return nil
267292
}

runtime/runtime.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var (
2020
// ErrContainerStartTimeout is returned if a container takes too
2121
// long to start
2222
ErrContainerStartTimeout = errors.New("containerd: container did not start before the specified timeout")
23+
// ErrShimExited is returned if the shim or the contianer's init process
24+
// exits before completing
25+
ErrShimExited = errors.New("containerd: shim exited before container process was started")
2326

2427
errNoPidFile = errors.New("containerd: no process pid file found")
2528
errInvalidPidInt = errors.New("containerd: process pid is invalid")

supervisor/worker.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,23 @@ func (w *worker) Start() {
6262
}
6363
if err := w.s.monitorProcess(process); err != nil {
6464
logrus.WithField("error", err).Error("containerd: add process to monitor")
65+
t.Err <- err
66+
evt := &DeleteTask{
67+
ID: t.Container.ID(),
68+
NoEvent: true,
69+
}
70+
w.s.SendTask(evt)
71+
continue
6572
}
6673
if err := process.Start(); err != nil {
6774
logrus.WithField("error", err).Error("containerd: start init process")
75+
t.Err <- err
76+
evt := &DeleteTask{
77+
ID: t.Container.ID(),
78+
NoEvent: true,
79+
}
80+
w.s.SendTask(evt)
81+
continue
6882
}
6983
ContainerStartTimer.UpdateSince(started)
7084
t.Err <- nil

0 commit comments

Comments
 (0)