/
oci.go
425 lines (358 loc) · 13.5 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
package oci
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/cri-o/cri-o/pkg/config"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"golang.org/x/net/context"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
"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
// killContainerTimeout is the timeout that we wait for the container to
// be SIGKILLed.
killContainerTimeout = 2 * time.Minute
// maxExecSyncSize is the maximum size of exec sync output CRI-O will process.
// It is set to the amount of logs allowed in the dockershim implementation:
// https://github.com/kubernetes/kubernetes/pull/82514
maxExecSyncSize = 16 * 1024 * 1024
)
// Runtime is the generic structure holding both global and specific
// information about the runtime.
type Runtime struct {
config *config.Config
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(context.Context, *Container, string) error
StartContainer(context.Context, *Container) error
ExecContainer(context.Context, *Container, []string, io.Reader, io.WriteCloser, io.WriteCloser,
bool, <-chan remotecommand.TerminalSize) error
ExecSyncContainer(context.Context, *Container, []string, int64) (*types.ExecSyncResponse, error)
UpdateContainer(context.Context, *Container, *rspec.LinuxResources) error
StopContainer(context.Context, *Container, int64) error
DeleteContainer(context.Context, *Container) error
UpdateContainerStatus(context.Context, *Container) error
PauseContainer(context.Context, *Container) error
UnpauseContainer(context.Context, *Container) error
ContainerStats(context.Context, *Container, string) (*types.ContainerStats, error)
SignalContainer(context.Context, *Container, syscall.Signal) error
AttachContainer(context.Context, *Container, io.Reader, io.WriteCloser, io.WriteCloser,
bool, <-chan remotecommand.TerminalSize) error
PortForwardContainer(context.Context, *Container, string,
int32, io.ReadWriteCloser) error
ReopenContainerLog(context.Context, *Container) error
}
// New creates a new Runtime with options provided
func New(c *config.Config) (*Runtime, error) {
execNotifyDir := filepath.Join(c.ContainerAttachSocketDir, "exec-pid-dir")
if err := os.MkdirAll(execNotifyDir, 0o750); err != nil {
return nil, errors.Wrapf(err, "create oci runtime pid dir")
}
return &Runtime{
config: c,
runtimeImplMap: make(map[string]RuntimeImpl),
}, nil
}
// Runtimes returns the map of OCI runtimes.
func (r *Runtime) Runtimes() config.Runtimes {
return r.config.Runtimes
}
// ValidateRuntimeHandler returns an error if the runtime handler string
// provided does not match any valid use case.
func (r *Runtime) ValidateRuntimeHandler(handler string) (*config.RuntimeHandler, error) {
if handler == "" {
return nil, fmt.Errorf("empty runtime handler")
}
runtimeHandler, ok := r.config.Runtimes[handler]
if !ok {
return nil, fmt.Errorf("failed to find runtime handler %s from runtime list %v",
handler, r.config.Runtimes)
}
if runtimeHandler.RuntimePath == "" {
return nil, fmt.Errorf("empty runtime path for runtime handler %s", handler)
}
return runtimeHandler, nil
}
func (r *Runtime) getRuntimeHandler(handler string) (*config.RuntimeHandler, error) {
// Define the current runtime handler as the default runtime handler.
rh := r.config.Runtimes[r.config.DefaultRuntime]
// Override the current runtime handler with the runtime handler
// corresponding to the runtime handler key provided with this
// specific container.
if handler != "" {
runtimeHandler, err := r.ValidateRuntimeHandler(handler)
if err != nil {
return nil, err
}
rh = runtimeHandler
}
return rh, nil
}
// PrivelegedRuntimeHandler returns a boolean value configured for the
// runtimeHandler indicating if devices on the host are passed/not passed
// to a container running as privileged.
func (r *Runtime) PrivilegedWithoutHostDevices(handler string) (bool, error) {
rh, err := r.getRuntimeHandler(handler)
if err != nil {
return false, err
}
return rh.PrivilegedWithoutHostDevices, nil
}
// AllowedAnnotations returns the allowed annotations for this runtime.
func (r *Runtime) AllowedAnnotations(handler string) ([]string, error) {
rh, err := r.getRuntimeHandler(handler)
if err != nil {
return []string{}, err
}
return rh.AllowedAnnotations, nil
}
// RuntimeType returns the type of runtimeHandler
// This is needed when callers need to do specific work for oci vs vm
// containers, like monitor an oci container's conmon.
func (r *Runtime) RuntimeType(runtimeHandler string) (string, error) {
rh, err := r.getRuntimeHandler(runtimeHandler)
if err != nil {
return "", err
}
return rh.RuntimeType, nil
}
func (r *Runtime) newRuntimeImpl(c *Container) (RuntimeImpl, error) {
rh, err := r.getRuntimeHandler(c.runtimeHandler)
if err != nil {
return nil, err
}
if rh.RuntimeType == config.RuntimeTypeVM {
return newRuntimeVM(rh.RuntimePath, rh.RuntimeRoot, rh.RuntimeConfigPath), nil
}
if rh.RuntimeType == config.RuntimeTypePod {
return newRuntimePod(r, rh, c)
}
// If the runtime type is different from "vm", then let's fallback
// onto the OCI implementation by default.
return newRuntimeOCI(r, rh), 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 impl, nil
}
r.runtimeImplMapMutex.Lock()
defer r.runtimeImplMapMutex.Unlock()
impl, err := r.newRuntimeImpl(c)
if err != nil {
return nil, err
}
r.runtimeImplMap[c.ID()] = impl
return impl, nil
}
// CreateContainer creates a container.
func (r *Runtime) CreateContainer(ctx context.Context, 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(ctx, c, cgroupParent)
}
// StartContainer starts a container.
func (r *Runtime) StartContainer(ctx context.Context, c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.StartContainer(ctx, c)
}
// ExecContainer prepares a streaming endpoint to execute a command in the container.
func (r *Runtime) ExecContainer(ctx context.Context, 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(ctx, 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(ctx context.Context, c *Container, command []string, timeout int64) (*types.ExecSyncResponse, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return nil, err
}
return impl.ExecSyncContainer(ctx, c, command, timeout)
}
// UpdateContainer updates container resources
func (r *Runtime) UpdateContainer(ctx context.Context, c *Container, res *rspec.LinuxResources) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UpdateContainer(ctx, 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(ctx context.Context, c *Container) (err error) {
r.runtimeImplMapMutex.RLock()
impl, ok := r.runtimeImplMap[c.ID()]
r.runtimeImplMapMutex.RUnlock()
if !ok {
if impl, err = r.newRuntimeImpl(c); err != nil {
return err
}
} else {
defer func() {
if err == nil {
r.runtimeImplMapMutex.Lock()
delete(r.runtimeImplMap, c.ID())
r.runtimeImplMapMutex.Unlock()
}
}()
}
return impl.DeleteContainer(ctx, c)
}
// UpdateContainerStatus refreshes the status of the container.
func (r *Runtime) UpdateContainerStatus(ctx context.Context, c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UpdateContainerStatus(ctx, c)
}
// PauseContainer pauses a container.
func (r *Runtime) PauseContainer(ctx context.Context, c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.PauseContainer(ctx, c)
}
// UnpauseContainer unpauses a container.
func (r *Runtime) UnpauseContainer(ctx context.Context, c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.UnpauseContainer(ctx, c)
}
// ContainerStats provides statistics of a container.
func (r *Runtime) ContainerStats(ctx context.Context, c *Container, cgroup string) (*types.ContainerStats, error) {
impl, err := r.RuntimeImpl(c)
if err != nil {
return nil, err
}
return impl.ContainerStats(ctx, c, cgroup)
}
// SignalContainer sends a signal to a container process.
func (r *Runtime) SignalContainer(ctx context.Context, c *Container, sig syscall.Signal) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.SignalContainer(ctx, c, sig)
}
// AttachContainer attaches IO to a running container.
func (r *Runtime) AttachContainer(ctx context.Context, 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(ctx, c, inputStream, outputStream, errorStream, tty, resize)
}
// PortForwardContainer forwards the specified port provides statistics of a container.
func (r *Runtime) PortForwardContainer(ctx context.Context, c *Container, netNsPath string, port int32, stream io.ReadWriteCloser) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.PortForwardContainer(ctx, c, netNsPath, port, stream)
}
// ReopenContainerLog reopens the log file of a container.
func (r *Runtime) ReopenContainerLog(ctx context.Context, c *Container) error {
impl, err := r.RuntimeImpl(c)
if err != nil {
return err
}
return impl.ReopenContainerLog(ctx, c)
}
// 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)
}
// BuildContainerdBinaryName() is responsible for ensuring the binary passed will
// be properly converted to the containerd binary naming pattern.
//
// This method should never ever be called from anywhere else but the runtimeVM,
// and the only reason its exported here is in order to get some test coverage.
func BuildContainerdBinaryName(path string) string {
// containerd expects the runtime name to be in the following pattern:
// ($dir.)?$prefix.$name.$version
// -------- ------ ----- ---------
// | | | |
// v | | |
// "/usr/local/bin" | |
// (optional) | | |
// v | |
// "containerd.shim." |
// | |
// v |
// "kata-qemu" |
// v
// "v2"
const expectedPrefix = "containerd-shim-"
const expectedVersion = "-v2"
const binaryPrefix = "containerd.shim"
const binaryVersion = "v2"
runtimeDir := filepath.Dir(path)
// This is only safe to do because the runtime_path, for the VM runtime_type, is validated in the config,
// allowing us to take the liberty to simply go ahead and check, without having to ensure we're receiving
// the binary in the expected form.
//
// For clarity, it could be ensured twice, but we count on the developer to never ever call this function
// in a different context from the one used in the runtime_vm.go file.
runtimeName := strings.SplitAfter(strings.Split(filepath.Base(path), expectedVersion)[0], expectedPrefix)[1]
return filepath.Join(runtimeDir, fmt.Sprintf("%s.%s.%s", binaryPrefix, runtimeName, binaryVersion))
}