Skip to content

Commit 326bc9f

Browse files
authored
Update to containerd 1.2.2 (google#14)
* Update containerd to 1.2.2 Signed-off-by: Lantao Liu <[email protected]> * Port containerd/containerd#2803. Signed-off-by: Lantao Liu <[email protected]>
1 parent 35db607 commit 326bc9f

File tree

21 files changed

+480
-441
lines changed

21 files changed

+480
-441
lines changed

pkg/v1/proc/exec.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
type execProcess struct {
4343
wg sync.WaitGroup
4444

45-
proc.State
45+
execState execState
4646

4747
mu sync.Mutex
4848
id string
@@ -88,13 +88,27 @@ func (e *execProcess) ExitedAt() time.Time {
8888
return e.exited
8989
}
9090

91+
func (e *execProcess) SetExited(status int) {
92+
e.mu.Lock()
93+
defer e.mu.Unlock()
94+
95+
e.execState.SetExited(status)
96+
}
97+
9198
func (e *execProcess) setExited(status int) {
9299
e.status = status
93100
e.exited = time.Now()
94101
e.parent.Platform.ShutdownConsole(context.Background(), e.console)
95102
close(e.waitBlock)
96103
}
97104

105+
func (e *execProcess) Delete(ctx context.Context) error {
106+
e.mu.Lock()
107+
defer e.mu.Unlock()
108+
109+
return e.execState.Delete(ctx)
110+
}
111+
98112
func (e *execProcess) delete(ctx context.Context) error {
99113
e.wg.Wait()
100114
if e.io != nil {
@@ -112,13 +126,27 @@ func (e *execProcess) delete(ctx context.Context) error {
112126
return nil
113127
}
114128

129+
func (e *execProcess) Resize(ws console.WinSize) error {
130+
e.mu.Lock()
131+
defer e.mu.Unlock()
132+
133+
return e.execState.Resize(ws)
134+
}
135+
115136
func (e *execProcess) resize(ws console.WinSize) error {
116137
if e.console == nil {
117138
return nil
118139
}
119140
return e.console.Resize(ws)
120141
}
121142

143+
func (e *execProcess) Kill(ctx context.Context, sig uint32, _ bool) error {
144+
e.mu.Lock()
145+
defer e.mu.Unlock()
146+
147+
return e.execState.Kill(ctx, sig, false)
148+
}
149+
122150
func (e *execProcess) kill(ctx context.Context, sig uint32, _ bool) error {
123151
internalPid := e.internalPid
124152
if internalPid != 0 {
@@ -141,6 +169,13 @@ func (e *execProcess) Stdio() proc.Stdio {
141169
return e.stdio
142170
}
143171

172+
func (e *execProcess) Start(ctx context.Context) error {
173+
e.mu.Lock()
174+
defer e.mu.Unlock()
175+
176+
return e.execState.Start(ctx)
177+
}
178+
144179
func (e *execProcess) start(ctx context.Context) (err error) {
145180
var (
146181
socket *runc.Socket

pkg/v1/proc/exec_state.go

+13-44
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,37 @@ import (
2424
"github.com/pkg/errors"
2525
)
2626

27+
type execState interface {
28+
Resize(console.WinSize) error
29+
Start(context.Context) error
30+
Delete(context.Context) error
31+
Kill(context.Context, uint32, bool) error
32+
SetExited(int)
33+
}
34+
2735
type execCreatedState struct {
2836
p *execProcess
2937
}
3038

3139
func (s *execCreatedState) transition(name string) error {
3240
switch name {
3341
case "running":
34-
s.p.State = &execRunningState{p: s.p}
42+
s.p.execState = &execRunningState{p: s.p}
3543
case "stopped":
36-
s.p.State = &execStoppedState{p: s.p}
44+
s.p.execState = &execStoppedState{p: s.p}
3745
case "deleted":
38-
s.p.State = &deletedState{}
46+
s.p.execState = &deletedState{}
3947
default:
4048
return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
4149
}
4250
return nil
4351
}
4452

4553
func (s *execCreatedState) Resize(ws console.WinSize) error {
46-
s.p.mu.Lock()
47-
defer s.p.mu.Unlock()
48-
4954
return s.p.resize(ws)
5055
}
5156

5257
func (s *execCreatedState) Start(ctx context.Context) error {
53-
s.p.mu.Lock()
54-
defer s.p.mu.Unlock()
5558
if err := s.p.start(ctx); err != nil {
5659
return err
5760
}
@@ -62,22 +65,14 @@ func (s *execCreatedState) Delete(ctx context.Context) error {
6265
if err := s.p.delete(ctx); err != nil {
6366
return err
6467
}
65-
s.p.mu.Lock()
66-
defer s.p.mu.Unlock()
6768
return s.transition("deleted")
6869
}
6970

7071
func (s *execCreatedState) Kill(ctx context.Context, sig uint32, all bool) error {
71-
s.p.mu.Lock()
72-
defer s.p.mu.Unlock()
73-
7472
return s.p.kill(ctx, sig, all)
7573
}
7674

7775
func (s *execCreatedState) SetExited(status int) {
78-
s.p.mu.Lock()
79-
defer s.p.mu.Unlock()
80-
8176
s.p.setExited(status)
8277

8378
if err := s.transition("stopped"); err != nil {
@@ -92,45 +87,30 @@ type execRunningState struct {
9287
func (s *execRunningState) transition(name string) error {
9388
switch name {
9489
case "stopped":
95-
s.p.State = &execStoppedState{p: s.p}
90+
s.p.execState = &execStoppedState{p: s.p}
9691
default:
9792
return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
9893
}
9994
return nil
10095
}
10196

10297
func (s *execRunningState) Resize(ws console.WinSize) error {
103-
s.p.mu.Lock()
104-
defer s.p.mu.Unlock()
105-
10698
return s.p.resize(ws)
10799
}
108100

109101
func (s *execRunningState) Start(ctx context.Context) error {
110-
s.p.mu.Lock()
111-
defer s.p.mu.Unlock()
112-
113102
return errors.Errorf("cannot start a running process")
114103
}
115104

116105
func (s *execRunningState) Delete(ctx context.Context) error {
117-
s.p.mu.Lock()
118-
defer s.p.mu.Unlock()
119-
120106
return errors.Errorf("cannot delete a running process")
121107
}
122108

123109
func (s *execRunningState) Kill(ctx context.Context, sig uint32, all bool) error {
124-
s.p.mu.Lock()
125-
defer s.p.mu.Unlock()
126-
127110
return s.p.kill(ctx, sig, all)
128111
}
129112

130113
func (s *execRunningState) SetExited(status int) {
131-
s.p.mu.Lock()
132-
defer s.p.mu.Unlock()
133-
134114
s.p.setExited(status)
135115

136116
if err := s.transition("stopped"); err != nil {
@@ -145,40 +125,29 @@ type execStoppedState struct {
145125
func (s *execStoppedState) transition(name string) error {
146126
switch name {
147127
case "deleted":
148-
s.p.State = &deletedState{}
128+
s.p.execState = &deletedState{}
149129
default:
150130
return errors.Errorf("invalid state transition %q to %q", stateName(s), name)
151131
}
152132
return nil
153133
}
154134

155135
func (s *execStoppedState) Resize(ws console.WinSize) error {
156-
s.p.mu.Lock()
157-
defer s.p.mu.Unlock()
158-
159136
return errors.Errorf("cannot resize a stopped container")
160137
}
161138

162139
func (s *execStoppedState) Start(ctx context.Context) error {
163-
s.p.mu.Lock()
164-
defer s.p.mu.Unlock()
165-
166140
return errors.Errorf("cannot start a stopped process")
167141
}
168142

169143
func (s *execStoppedState) Delete(ctx context.Context) error {
170144
if err := s.p.delete(ctx); err != nil {
171145
return err
172146
}
173-
s.p.mu.Lock()
174-
defer s.p.mu.Unlock()
175147
return s.transition("deleted")
176148
}
177149

178150
func (s *execStoppedState) Kill(ctx context.Context, sig uint32, all bool) error {
179-
s.p.mu.Lock()
180-
defer s.p.mu.Unlock()
181-
182151
return s.p.kill(ctx, sig, all)
183152
}
184153

pkg/v1/proc/init.go

+59-8
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ const InitPidFile = "init.pid"
4545

4646
// Init represents an initial process for a container
4747
type Init struct {
48-
wg sync.WaitGroup
49-
initState
48+
wg sync.WaitGroup
49+
initState initState
5050

5151
// mu is used to ensure that `Start()` and `Exited()` calls return in
5252
// the right order when invoked in separate go routines.
@@ -217,6 +217,14 @@ func (p *Init) Status(ctx context.Context) (string, error) {
217217
return p.convertStatus(c.Status), nil
218218
}
219219

220+
// Start the init process
221+
func (p *Init) Start(ctx context.Context) error {
222+
p.mu.Lock()
223+
defer p.mu.Unlock()
224+
225+
return p.initState.Start(ctx)
226+
}
227+
220228
func (p *Init) start(context context.Context) error {
221229
var cio runc.IO
222230
if !p.Sandbox {
@@ -244,17 +252,33 @@ func (p *Init) start(context context.Context) error {
244252
return nil
245253
}
246254

255+
// SetExited of the init process with the next status
256+
func (p *Init) SetExited(status int) {
257+
p.mu.Lock()
258+
defer p.mu.Unlock()
259+
260+
p.initState.SetExited(status)
261+
}
262+
247263
func (p *Init) setExited(status int) {
248264
p.exited = time.Now()
249265
p.status = status
250266
p.Platform.ShutdownConsole(context.Background(), p.console)
251267
close(p.waitBlock)
252268
}
253269

254-
func (p *Init) delete(context context.Context) error {
255-
p.killAll(context)
270+
// Delete the init process
271+
func (p *Init) Delete(ctx context.Context) error {
272+
p.mu.Lock()
273+
defer p.mu.Unlock()
274+
275+
return p.initState.Delete(ctx)
276+
}
277+
278+
func (p *Init) delete(ctx context.Context) error {
279+
p.killAll(ctx)
256280
p.wg.Wait()
257-
err := p.runtime.Delete(context, p.id, nil)
281+
err := p.runtime.Delete(ctx, p.id, nil)
258282
// ignore errors if a runtime has already deleted the process
259283
// but we still hold metadata and pipes
260284
//
@@ -274,21 +298,40 @@ func (p *Init) delete(context context.Context) error {
274298
p.io.Close()
275299
}
276300
if err2 := mount.UnmountAll(p.Rootfs, 0); err2 != nil {
277-
log.G(context).WithError(err2).Warn("failed to cleanup rootfs mount")
301+
log.G(ctx).WithError(err2).Warn("failed to cleanup rootfs mount")
278302
if err == nil {
279303
err = errors.Wrap(err2, "failed rootfs umount")
280304
}
281305
}
282306
return err
283307
}
284308

309+
// Resize the init processes console
310+
func (p *Init) Resize(ws console.WinSize) error {
311+
p.mu.Lock()
312+
defer p.mu.Unlock()
313+
314+
if p.console == nil {
315+
return nil
316+
}
317+
return p.console.Resize(ws)
318+
}
319+
285320
func (p *Init) resize(ws console.WinSize) error {
286321
if p.console == nil {
287322
return nil
288323
}
289324
return p.console.Resize(ws)
290325
}
291326

327+
// Kill the init process
328+
func (p *Init) Kill(ctx context.Context, signal uint32, all bool) error {
329+
p.mu.Lock()
330+
defer p.mu.Unlock()
331+
332+
return p.initState.Kill(ctx, signal, all)
333+
}
334+
292335
func (p *Init) kill(context context.Context, signal uint32, all bool) error {
293336
var (
294337
killErr error
@@ -349,8 +392,16 @@ func (p *Init) Runtime() *runsc.Runsc {
349392
return p.runtime
350393
}
351394

395+
// Exec returns a new child process
396+
func (p *Init) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
397+
p.mu.Lock()
398+
defer p.mu.Unlock()
399+
400+
return p.initState.Exec(ctx, path, r)
401+
}
402+
352403
// exec returns a new exec'd process
353-
func (p *Init) exec(context context.Context, path string, r *ExecConfig) (proc.Process, error) {
404+
func (p *Init) exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
354405
// process exec request
355406
var spec specs.Process
356407
if err := json.Unmarshal(r.Spec.Value, &spec); err != nil {
@@ -371,7 +422,7 @@ func (p *Init) exec(context context.Context, path string, r *ExecConfig) (proc.P
371422
},
372423
waitBlock: make(chan struct{}),
373424
}
374-
e.State = &execCreatedState{p: e}
425+
e.execState = &execCreatedState{p: e}
375426
return e, nil
376427
}
377428

0 commit comments

Comments
 (0)