/
oci.go
440 lines (374 loc) · 12.6 KB
/
oci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package oci
import (
"bytes"
"fmt"
"io"
"sync"
"syscall"
"time"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/net/context"
"k8s.io/client-go/tools/remotecommand"
)
const (
// ContainerStateCreated represents the created state of a container
ContainerStateCreated = "created"
// ContainerStatePaused represents the paused state of a container
ContainerStatePaused = "paused"
// ContainerStateRunning represents the running state of a container
ContainerStateRunning = "running"
// ContainerStateStopped represents the stopped state of a container
ContainerStateStopped = "stopped"
// ContainerCreateTimeout represents the value of container creating timeout
ContainerCreateTimeout = 240 * time.Second
// CgroupfsCgroupsManager represents cgroupfs native cgroup manager
CgroupfsCgroupsManager = "cgroupfs"
// SystemdCgroupsManager represents systemd native cgroup manager
SystemdCgroupsManager = "systemd"
// BufSize is the size of buffers passed in to sockets
BufSize = 8192
// killContainerTimeout is the timeout that we wait for the container to
// be SIGKILLed.
killContainerTimeout = 2 * time.Minute
// minCtrStopTimeout is the minimal amount of time in seconds to wait
// before issuing a timeout regarding the proper termination of the
// container.
minCtrStopTimeout = 30
)
// Runtime is the generic structure holding both global and specific
// information about the runtime.
type Runtime struct {
defaultRuntime RuntimeHandler
runtimes map[string]RuntimeHandler
conmonPath string
conmonEnv []string
cgroupManager string
containerExitsDir string
containerAttachSocketDir string
logSizeMax int64
logToJournald bool
noPivot bool
ctrStopTimeout int64
runtimeImplMap map[string]RuntimeImpl
runtimeImplMapMutex sync.RWMutex
}
// RuntimeImpl is an interface used by the caller to interact with the
// container runtime. The purpose of this interface being to abstract
// implementations and their associated assumptions regarding the way to
// interact with containers. This will allow for new implementations of
// this interface, especially useful for the case of VM based container
// runtimes. Assumptions based on the fact that a container process runs
// on the host will be limited to the RuntimeOCI implementation.
type RuntimeImpl interface {
CreateContainer(*Container, string) error
StartContainer(*Container) error
ExecContainer(*Container, []string, io.Reader, io.WriteCloser, io.WriteCloser,
bool, <-chan remotecommand.TerminalSize) error
ExecSyncContainer(*Container, []string, int64) (*ExecSyncResponse, error)
UpdateContainer(*Container, *rspec.LinuxResources) error
StopContainer(context.Context, *Container, int64) error
DeleteContainer(*Container) error
UpdateContainerStatus(*Container) error
PauseContainer(*Container) error
UnpauseContainer(*Container) error
ContainerStats(*Container) (*ContainerStats, error)
SignalContainer(*Container, syscall.Signal) error
AttachContainer(*Container, io.Reader, io.WriteCloser, io.WriteCloser,
bool, <-chan remotecommand.TerminalSize) error
PortForwardContainer(*Container, int32, io.ReadWriter) error
ReopenContainerLog(*Container) error
WaitContainerStateStopped(context.Context, *Container) error
}
// RuntimeHandler represents each item of the "crio.runtime.runtimes" TOML
// config table.
type RuntimeHandler struct {
RuntimePath string `toml:"runtime_path"`
RuntimeType string `toml:"runtime_type"`
}
// New creates a new Runtime with options provided
func New(defaultRuntime string,
runtimes map[string]RuntimeHandler,
conmonPath string,
conmonEnv []string,
cgroupManager string,
containerExitsDir string,
containerAttachSocketDir string,
logSizeMax int64,
logToJournald bool,
noPivot bool,
ctrStopTimeout int64) (*Runtime, error) {
defRuntime, ok := runtimes[defaultRuntime]
if !ok {
return nil, fmt.Errorf("no runtime configured for default_runtime=%q", defaultRuntime)
}
if defRuntime.RuntimePath == "" {
return nil, fmt.Errorf("empty runtime path for default_runtime=%q", defaultRuntime)
}
return &Runtime{
defaultRuntime: defRuntime,
runtimes: runtimes,
conmonPath: conmonPath,
conmonEnv: conmonEnv,
cgroupManager: cgroupManager,
containerExitsDir: containerExitsDir,
containerAttachSocketDir: containerAttachSocketDir,
logSizeMax: logSizeMax,
logToJournald: logToJournald,
noPivot: noPivot,
ctrStopTimeout: ctrStopTimeout,
runtimeImplMap: make(map[string]RuntimeImpl),
}, nil
}
// Runtimes returns the map of OCI runtimes.
func (r *Runtime) Runtimes() map[string]RuntimeHandler {
return r.runtimes
}
// ValidateRuntimeHandler returns an error if the runtime handler string
// provided does not match any valid use case.
func (r *Runtime) ValidateRuntimeHandler(handler string) (RuntimeHandler, error) {
if handler == "" {
return RuntimeHandler{}, fmt.Errorf("empty runtime handler")
}
runtimeHandler, ok := r.runtimes[handler]
if !ok {
return RuntimeHandler{}, fmt.Errorf("failed to find runtime handler %s from runtime list %v",
handler, r.runtimes)
}
if runtimeHandler.RuntimePath == "" {
return RuntimeHandler{}, fmt.Errorf("empty runtime path for runtime handler %s", handler)
}
return runtimeHandler, nil
}
// WaitContainerStateStopped runs a loop polling UpdateStatus(), seeking for
// the container status to be updated to 'stopped'. Either it gets the expected
// status and returns nil, or it reaches the timeout and returns an error.
func (r *Runtime) WaitContainerStateStopped(ctx context.Context, c *Container) (err error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
// No need to go further and spawn the go routine if the container
// is already in the expected status.
if c.State().Status == ContainerStateStopped {
return nil
}
// We need to ensure the container termination will be properly waited
// for by defining a minimal timeout value. This will prevent timeout
// value defined in the configuration file to be too low.
timeout := r.ctrStopTimeout
if timeout < minCtrStopTimeout {
timeout = minCtrStopTimeout
}
done := make(chan error)
chControl := make(chan struct{})
go func() {
for {
select {
case <-chControl:
return
default:
// Check if the container is stopped
if err := impl.UpdateContainerStatus(c); err != nil {
done <- err
close(done)
return
}
if c.State().Status == ContainerStateStopped {
close(done)
return
}
time.Sleep(100 * time.Millisecond)
}
}
}()
select {
case err = <-done:
break
case <-ctx.Done():
close(chControl)
return ctx.Err()
case <-time.After(time.Duration(timeout) * time.Second):
close(chControl)
return fmt.Errorf("failed to get container stopped status: %ds timeout reached", timeout)
}
if err != nil {
return fmt.Errorf("failed to get container stopped status: %v", err)
}
return nil
}
func (r *Runtime) newRuntimeImpl(c *Container) (RuntimeImpl, error) {
// Define the current runtime handler as the default runtime handler.
rh := r.defaultRuntime
// Override the current runtime handler with the runtime handler
// corresponding to the runtime handler key provided with this
// specific container.
if c.runtimeHandler != "" {
runtimeHandler, err := r.ValidateRuntimeHandler(c.runtimeHandler)
if err != nil {
return nil, err
}
rh = runtimeHandler
}
if rh.RuntimeType == RuntimeTypeVM {
return newRuntimeVM(rh.RuntimePath), nil
}
// If the runtime type is different from "vm", then let's fallback
// onto the OCI implementation by default.
return newRuntimeOCI(r, rh.RuntimePath), nil
}
// RuntimeImpl returns the runtime implementation for a given container
func (r *Runtime) RuntimeImpl(c *Container) (RuntimeImpl, error) {
r.runtimeImplMapMutex.RLock()
impl, ok := r.runtimeImplMap[c.ID()]
r.runtimeImplMapMutex.RUnlock()
if !ok {
return r.newRuntimeImpl(c)
}
return impl, nil
}
// CreateContainer creates a container.
func (r *Runtime) CreateContainer(c *Container, cgroupParent string) error {
// Instantiate a new runtime implementation for this new container
impl, err := r.newRuntimeImpl(c)
if err != nil {
return err
}
// Assign this runtime implementation to the current container
r.runtimeImplMapMutex.Lock()
r.runtimeImplMap[c.ID()] = impl
r.runtimeImplMapMutex.Unlock()
return impl.CreateContainer(c, cgroupParent)
}
// StartContainer starts a container.
func (r *Runtime) StartContainer(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.StartContainer(c)
}
// ExecContainer prepares a streaming endpoint to execute a command in the container.
func (r *Runtime) ExecContainer(c *Container, cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.ExecContainer(c, cmd, stdin, stdout, stderr, tty, resize)
}
// ExecSyncContainer execs a command in a container and returns it's stdout, stderr and return code.
func (r *Runtime) ExecSyncContainer(c *Container, command []string, timeout int64) (*ExecSyncResponse, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return nil, err
}
return impl.ExecSyncContainer(c, command, timeout)
}
// UpdateContainer updates container resources
func (r *Runtime) UpdateContainer(c *Container, res *rspec.LinuxResources) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UpdateContainer(c, res)
}
// StopContainer stops a container. Timeout is given in seconds.
func (r *Runtime) StopContainer(ctx context.Context, c *Container, timeout int64) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.StopContainer(ctx, c, timeout)
}
// DeleteContainer deletes a container.
func (r *Runtime) DeleteContainer(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
defer func() {
r.runtimeImplMapMutex.Lock()
delete(r.runtimeImplMap, c.ID())
r.runtimeImplMapMutex.Unlock()
}()
return impl.DeleteContainer(c)
}
// UpdateContainerStatus refreshes the status of the container.
func (r *Runtime) UpdateContainerStatus(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UpdateContainerStatus(c)
}
// PauseContainer pauses a container.
func (r *Runtime) PauseContainer(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.PauseContainer(c)
}
// UnpauseContainer unpauses a container.
func (r *Runtime) UnpauseContainer(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UnpauseContainer(c)
}
// ContainerStats provides statistics of a container.
func (r *Runtime) ContainerStats(c *Container) (*ContainerStats, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return nil, err
}
return impl.ContainerStats(c)
}
// SignalContainer sends a signal to a container process.
func (r *Runtime) SignalContainer(c *Container, sig syscall.Signal) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.SignalContainer(c, sig)
}
// AttachContainer attaches IO to a running container.
func (r *Runtime) AttachContainer(c *Container, inputStream io.Reader, outputStream, errorStream io.WriteCloser, tty bool, resize <-chan remotecommand.TerminalSize) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.AttachContainer(c, inputStream, outputStream, errorStream, tty, resize)
}
// PortForwardContainer forwards the specified port provides statistics of a container.
func (r *Runtime) PortForwardContainer(c *Container, port int32, stream io.ReadWriter) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.PortForwardContainer(c, port, stream)
}
// ReopenContainerLog reopens the log file of a container.
func (r *Runtime) ReopenContainerLog(c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.ReopenContainerLog(c)
}
// ExecSyncResponse is returned from ExecSync.
type ExecSyncResponse struct {
Stdout []byte
Stderr []byte
ExitCode int32
}
// ExecSyncError wraps command's streams, exit code and error on ExecSync error.
type ExecSyncError struct {
Stdout bytes.Buffer
Stderr bytes.Buffer
ExitCode int32
Err error
}
func (e ExecSyncError) Error() string {
return fmt.Sprintf("command error: %+v, stdout: %s, stderr: %s, exit code %d", e.Err, e.Stdout.Bytes(), e.Stderr.Bytes(), e.ExitCode)
}