-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcontainerizer.go
454 lines (373 loc) · 12.7 KB
/
containerizer.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package rundmc
import (
"fmt"
"io"
"os"
"strings"
"time"
specs "github.com/opencontainers/runtime-spec/specs-go"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/guardian/gardener"
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
"code.cloudfoundry.org/guardian/rundmc/event"
"code.cloudfoundry.org/guardian/rundmc/goci"
"code.cloudfoundry.org/lager"
"github.com/cloudfoundry/dropsonde/metrics"
)
//go:generate counterfeiter . Depot
//go:generate counterfeiter . OCIRuntime
//go:generate counterfeiter . NstarRunner
//go:generate counterfeiter . EventStore
//go:generate counterfeiter . ProcessesStopper
//go:generate counterfeiter . StateStore
//go:generate counterfeiter . PeaCreator
//go:generate counterfeiter . PeaUsernameResolver
//go:generate counterfeiter . RuntimeStopper
//go:generate counterfeiter . CPUCgrouper
type Depot interface {
Destroy(log lager.Logger, handle string) error
}
//go:generate counterfeiter . BundleGenerator
type BundleGenerator interface {
Generate(desiredContainerSpec spec.DesiredContainerSpec) (goci.Bndl, error)
}
type Status string
const RunningStatus Status = "running"
const CreatedStatus Status = "created"
const StoppedStatus Status = "stopped"
type State struct {
Pid int
Status Status
}
type OCIRuntime interface {
Create(log lager.Logger, id string, bundle goci.Bndl, io garden.ProcessIO) error
Exec(log lager.Logger, id string, spec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error)
Attach(log lager.Logger, id, processId string, io garden.ProcessIO) (garden.Process, error)
Delete(log lager.Logger, id string) error
State(log lager.Logger, id string) (State, error)
Stats(log lager.Logger, id string) (gardener.StatsContainerMetrics, error)
Events(log lager.Logger) (<-chan event.Event, error)
ContainerHandles() ([]string, error)
ContainerPeaHandles(log lager.Logger, id string) ([]string, error)
BundleInfo(log lager.Logger, id string) (string, goci.Bndl, error)
RemoveBundle(log lager.Logger, id string) error
}
type PeaCreator interface {
CreatePea(log lager.Logger, processSpec garden.ProcessSpec, pio garden.ProcessIO, sandboxHandle string) (garden.Process, error)
}
type NstarRunner interface {
StreamIn(log lager.Logger, pid int, path string, user string, tarStream io.Reader) error
StreamOut(log lager.Logger, pid int, path string, user string) (io.ReadCloser, error)
}
type ProcessesStopper interface {
StopAll(log lager.Logger, cgroupName string, save []int, kill bool) error
}
type RuntimeStopper interface {
Stop() error
}
type EventStore interface {
OnEvent(id string, event string) error
Events(id string) []string
}
type StateStore interface {
StoreStopped(handle string)
IsStopped(handle string) bool
}
type PeaUsernameResolver interface {
ResolveUser(log lager.Logger, handle string, image garden.ImageRef, username string) (int, int, error)
}
type CPUCgrouper interface {
CreateBadCgroup(handle string) error
DestroyBadCgroup(handle string) error
ReadBadCgroupUsage(handle string) (garden.ContainerCPUStat, error)
}
// Containerizer knows how to manage a depot of container bundles
type Containerizer struct {
depot Depot
bundler BundleGenerator
runtime OCIRuntime
processesStopper ProcessesStopper
nstar NstarRunner
events EventStore
states StateStore
peaCreator PeaCreator
peaUsernameResolver PeaUsernameResolver
cpuEntitlementPerShare float64
runtimeStopper RuntimeStopper
cpuCgrouper CPUCgrouper
}
func New(
depot Depot,
bundler BundleGenerator,
runtime OCIRuntime,
nstarRunner NstarRunner,
processesStopper ProcessesStopper,
events EventStore,
states StateStore,
peaCreator PeaCreator,
peaUsernameResolver PeaUsernameResolver,
cpuEntitlementPerShare float64,
runtimeStopper RuntimeStopper,
cpuCgrouper CPUCgrouper,
) *Containerizer {
containerizer := &Containerizer{
depot: depot,
bundler: bundler,
runtime: runtime,
nstar: nstarRunner,
processesStopper: processesStopper,
events: events,
states: states,
peaCreator: peaCreator,
peaUsernameResolver: peaUsernameResolver,
cpuEntitlementPerShare: cpuEntitlementPerShare,
runtimeStopper: runtimeStopper,
cpuCgrouper: cpuCgrouper,
}
return containerizer
}
func (c *Containerizer) WatchRuntimeEvents(log lager.Logger) error {
events, err := c.runtime.Events(log)
if err != nil {
return err
}
go func() {
for event := range events {
if err := c.events.OnEvent(event.ContainerID, event.Message); err != nil {
log.Error("failed to store event", err, lager.Data{"event": event})
}
}
}()
return nil
}
// Create creates a bundle in the depot and starts its init process
func (c *Containerizer) Create(log lager.Logger, spec spec.DesiredContainerSpec) error {
log = log.Session("containerizer-create", lager.Data{"handle": spec.Handle})
log.Info("start")
defer log.Info("finished")
bundle, err := c.bundler.Generate(spec)
if err != nil {
log.Error("bundle-generate-failed", err)
return err
}
if err := c.cpuCgrouper.CreateBadCgroup(spec.Handle); err != nil {
log.Error("create-bad-cgroup-failed", err)
return err
}
if err := c.runtime.Create(log, spec.Handle, bundle, garden.ProcessIO{}); err != nil {
log.Error("runtime-create-failed", err)
return err
}
return nil
}
// Run runs a process inside a running container
func (c *Containerizer) Run(log lager.Logger, handle string, spec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error) {
log = log.Session("run", lager.Data{"handle": handle, "path": spec.Path})
log.Info("started")
defer log.Info("finished")
if isPea(spec) {
if shouldResolveUsername(spec.User) {
resolvedUID, resolvedGID, err := c.peaUsernameResolver.ResolveUser(log, handle, spec.Image, spec.User)
if err != nil {
return nil, err
}
spec.User = fmt.Sprintf("%d:%d", resolvedUID, resolvedGID)
}
return c.peaCreator.CreatePea(log, spec, io, handle)
}
if spec.BindMounts != nil {
err := fmt.Errorf("Running a process with bind mounts and no image provided is not allowed")
log.Error("invalid-spec", err)
return nil, err
}
return c.runtime.Exec(log, handle, spec, io)
}
func isPea(spec garden.ProcessSpec) bool {
return spec.Image != (garden.ImageRef{})
}
func shouldResolveUsername(username string) bool {
return username != "" && !strings.Contains(username, ":")
}
func (c *Containerizer) Attach(log lager.Logger, handle string, processID string, io garden.ProcessIO) (garden.Process, error) {
log = log.Session("attach", lager.Data{"handle": handle, "process-id": processID})
log.Info("started")
defer log.Info("finished")
return c.runtime.Attach(log, handle, processID, io)
}
// StreamIn streams files in to the container
func (c *Containerizer) StreamIn(log lager.Logger, handle string, spec garden.StreamInSpec) error {
log = log.Session("stream-in", lager.Data{"handle": handle})
log.Info("started")
defer log.Info("finished")
defer func(startedAt time.Time) {
_ = metrics.SendValue("StreamInDuration", float64(time.Since(startedAt).Nanoseconds()), "nanos")
}(time.Now())
state, err := c.runtime.State(log, handle)
if err != nil {
log.Error("check-pid-failed", err)
return fmt.Errorf("stream-in: pid not found for container")
}
if err := c.nstar.StreamIn(log, state.Pid, spec.Path, spec.User, spec.TarStream); err != nil {
log.Error("nstar-failed", err)
return fmt.Errorf("stream-in: nstar: %s", err)
}
return nil
}
// StreamOut stream files from the container
func (c *Containerizer) StreamOut(log lager.Logger, handle string, spec garden.StreamOutSpec) (io.ReadCloser, error) {
log = log.Session("stream-out", lager.Data{"handle": handle})
log.Info("started")
defer log.Info("finished")
state, err := c.runtime.State(log, handle)
if err != nil {
log.Error("check-pid-failed", err)
return nil, fmt.Errorf("stream-out: pid not found for container")
}
stream, err := c.nstar.StreamOut(log, state.Pid, spec.Path, spec.User)
if err != nil {
log.Error("nstar-failed", err)
return nil, fmt.Errorf("stream-out: nstar: %s", err)
}
return stream, nil
}
// Stop stops all the processes other than the init process in the container
func (c *Containerizer) Stop(log lager.Logger, handle string, kill bool) error {
log = log.Session("stop", lager.Data{"handle": handle, "kill": kill})
log.Info("started")
defer log.Info("finished")
state, err := c.runtime.State(log, handle)
if err != nil {
log.Error("check-pid-failed", err)
return fmt.Errorf("stop: pid not found for container: %s", err)
}
if err = c.processesStopper.StopAll(log, handle, []int{state.Pid}, kill); err != nil {
log.Error("stop-all-processes-failed", err, lager.Data{"pid": state.Pid})
return fmt.Errorf("stop: %s", err)
}
c.states.StoreStopped(handle)
return nil
}
// Destroy deletes the container and the bundle directory
func (c *Containerizer) Destroy(log lager.Logger, handle string) error {
log = log.Session("destroy", lager.Data{"handle": handle})
log.Info("started")
defer log.Info("finished")
if err := c.runtime.Delete(log, handle); err != nil {
return err
}
return c.cpuCgrouper.DestroyBadCgroup(handle)
}
func (c *Containerizer) RemoveBundle(log lager.Logger, handle string) error {
log = log.Session("remove-bundle", lager.Data{"handle": handle})
// TODO: this should be removed once containerd processes are the only code path
// and this should be managed by the network depot
if err := c.depot.Destroy(log, handle); err != nil {
log.Debug("failed-to-remove-bundle-dir")
}
return c.runtime.RemoveBundle(log, handle)
}
func (c *Containerizer) Info(log lager.Logger, handle string) (spec.ActualContainerSpec, error) {
bundlePath, bundle, err := c.runtime.BundleInfo(log, handle)
if err != nil {
return spec.ActualContainerSpec{}, err
}
state, err := c.runtime.State(log, handle)
if err != nil {
return spec.ActualContainerSpec{}, err
}
privileged := true
for _, ns := range bundle.Namespaces() {
if ns.Type == specs.UserNamespace {
privileged = false
break
}
}
var cpuShares, limitInBytes uint64
if bundle.Resources() != nil {
cpuShares = *bundle.Resources().CPU.Shares
limitInBytes = uint64(*bundle.Resources().Memory.Limit)
} else {
log.Debug("bundle-resources-is-nil", lager.Data{"bundle": bundle})
}
return spec.ActualContainerSpec{
Pid: state.Pid,
BundlePath: bundlePath,
RootFSPath: bundle.RootFS(),
Events: c.events.Events(handle),
Stopped: c.states.IsStopped(handle),
Limits: garden.Limits{
CPU: garden.CPULimits{
LimitInShares: cpuShares,
},
Memory: garden.MemoryLimits{
LimitInBytes: limitInBytes,
},
},
Privileged: privileged,
}, nil
}
func (c *Containerizer) Metrics(log lager.Logger, handle string) (gardener.ActualContainerMetrics, error) {
containerMetrics, err := c.runtime.Stats(log, handle)
if err != nil {
return gardener.ActualContainerMetrics{}, err
}
badCpuStats, err := c.readBadCgroupUsage(handle)
if err != nil {
return gardener.ActualContainerMetrics{}, err
}
containerMetrics.CPU = garden.ContainerCPUStat{
Usage: containerMetrics.CPU.Usage + badCpuStats.Usage,
User: containerMetrics.CPU.User + badCpuStats.User,
System: containerMetrics.CPU.System + badCpuStats.System,
}
actualContainerMetrics := gardener.ActualContainerMetrics{
StatsContainerMetrics: containerMetrics,
}
_, bundle, err := c.runtime.BundleInfo(log, handle)
if isNotFound(err) { // pea
return actualContainerMetrics, nil
}
if err != nil {
return gardener.ActualContainerMetrics{}, err
}
actualContainerMetrics.CPUEntitlement = calculateCPUEntitlement(getShares(bundle), c.cpuEntitlementPerShare, containerMetrics.Age)
return actualContainerMetrics, nil
}
func (c *Containerizer) readBadCgroupUsage(handle string) (garden.ContainerCPUStat, error) {
badCpuStats, err := c.cpuCgrouper.ReadBadCgroupUsage(handle)
if err == nil {
return badCpuStats, nil
}
if os.IsNotExist(err) {
return garden.ContainerCPUStat{}, nil
}
return garden.ContainerCPUStat{}, err
}
func (c *Containerizer) Shutdown() error {
return c.runtimeStopper.Stop()
}
func isNotFound(err error) bool {
_, ok := err.(garden.ContainerNotFoundError)
return ok
}
func (c *Containerizer) Handles() ([]string, error) {
return c.runtime.ContainerHandles()
}
func calculateCPUEntitlement(shares uint64, entitlementPerShare float64, containerAge time.Duration) uint64 {
return uint64(float64(shares) * (entitlementPerShare / 100) * float64(containerAge.Nanoseconds()))
}
func getShares(bundle goci.Bndl) uint64 {
resources := bundle.Resources()
if resources == nil {
return 0
}
cpu := resources.CPU
if cpu == nil {
return 0
}
shares := cpu.Shares
if shares == nil {
return 0
}
return *shares
}