client_daemon.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // +build !windows
  2. package libcontainerd // import "github.com/docker/docker/libcontainerd"
  3. import (
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "reflect"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "syscall"
  15. "time"
  16. "github.com/containerd/containerd"
  17. apievents "github.com/containerd/containerd/api/events"
  18. "github.com/containerd/containerd/api/types"
  19. "github.com/containerd/containerd/archive"
  20. "github.com/containerd/containerd/cio"
  21. "github.com/containerd/containerd/content"
  22. containerderrors "github.com/containerd/containerd/errdefs"
  23. "github.com/containerd/containerd/events"
  24. "github.com/containerd/containerd/images"
  25. "github.com/containerd/containerd/runtime/linux/runctypes"
  26. "github.com/containerd/typeurl"
  27. "github.com/docker/docker/errdefs"
  28. "github.com/docker/docker/pkg/ioutils"
  29. "github.com/opencontainers/image-spec/specs-go/v1"
  30. specs "github.com/opencontainers/runtime-spec/specs-go"
  31. "github.com/pkg/errors"
  32. "github.com/sirupsen/logrus"
  33. "google.golang.org/grpc/codes"
  34. "google.golang.org/grpc/status"
  35. )
  36. // InitProcessName is the name given to the first process of a
  37. // container
  38. const InitProcessName = "init"
  39. type container struct {
  40. mu sync.Mutex
  41. bundleDir string
  42. ctr containerd.Container
  43. task containerd.Task
  44. execs map[string]containerd.Process
  45. oomKilled bool
  46. }
  47. func (c *container) setTask(t containerd.Task) {
  48. c.mu.Lock()
  49. c.task = t
  50. c.mu.Unlock()
  51. }
  52. func (c *container) getTask() containerd.Task {
  53. c.mu.Lock()
  54. t := c.task
  55. c.mu.Unlock()
  56. return t
  57. }
  58. func (c *container) addProcess(id string, p containerd.Process) {
  59. c.mu.Lock()
  60. if c.execs == nil {
  61. c.execs = make(map[string]containerd.Process)
  62. }
  63. c.execs[id] = p
  64. c.mu.Unlock()
  65. }
  66. func (c *container) deleteProcess(id string) {
  67. c.mu.Lock()
  68. delete(c.execs, id)
  69. c.mu.Unlock()
  70. }
  71. func (c *container) getProcess(id string) containerd.Process {
  72. c.mu.Lock()
  73. p := c.execs[id]
  74. c.mu.Unlock()
  75. return p
  76. }
  77. func (c *container) setOOMKilled(killed bool) {
  78. c.mu.Lock()
  79. c.oomKilled = killed
  80. c.mu.Unlock()
  81. }
  82. func (c *container) getOOMKilled() bool {
  83. c.mu.Lock()
  84. killed := c.oomKilled
  85. c.mu.Unlock()
  86. return killed
  87. }
  88. type client struct {
  89. sync.RWMutex // protects containers map
  90. client *containerd.Client
  91. stateDir string
  92. logger *logrus.Entry
  93. ns string
  94. backend Backend
  95. eventQ queue
  96. containers map[string]*container
  97. }
  98. // NewClient creates a new libcontainerd client from a containerd client
  99. func NewClient(ctx context.Context, cli *containerd.Client, stateDir, ns string, b Backend) (Client, error) {
  100. c := &client{
  101. client: cli,
  102. stateDir: stateDir,
  103. logger: logrus.WithField("module", "libcontainerd").WithField("namespace", ns),
  104. ns: ns,
  105. backend: b,
  106. containers: make(map[string]*container),
  107. }
  108. go c.processEventStream(ctx, ns)
  109. return c, nil
  110. }
  111. func (c *client) Version(ctx context.Context) (containerd.Version, error) {
  112. return c.client.Version(ctx)
  113. }
  114. // Restore loads the containerd container.
  115. // It should not be called concurrently with any other operation for the given ID.
  116. func (c *client) Restore(ctx context.Context, id string, attachStdio StdioCallback) (alive bool, pid int, err error) {
  117. c.Lock()
  118. _, ok := c.containers[id]
  119. if ok {
  120. c.Unlock()
  121. return false, 0, errors.WithStack(newConflictError("id already in use"))
  122. }
  123. cntr := &container{}
  124. c.containers[id] = cntr
  125. cntr.mu.Lock()
  126. defer cntr.mu.Unlock()
  127. c.Unlock()
  128. defer func() {
  129. if err != nil {
  130. c.Lock()
  131. delete(c.containers, id)
  132. c.Unlock()
  133. }
  134. }()
  135. var dio *cio.DirectIO
  136. defer func() {
  137. if err != nil && dio != nil {
  138. dio.Cancel()
  139. dio.Close()
  140. }
  141. err = wrapError(err)
  142. }()
  143. ctr, err := c.client.LoadContainer(ctx, id)
  144. if err != nil {
  145. return false, -1, errors.WithStack(wrapError(err))
  146. }
  147. attachIO := func(fifos *cio.FIFOSet) (cio.IO, error) {
  148. // dio must be assigned to the previously defined dio for the defer above
  149. // to handle cleanup
  150. dio, err = cio.NewDirectIO(ctx, fifos)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return attachStdio(dio)
  155. }
  156. t, err := ctr.Task(ctx, attachIO)
  157. if err != nil && !containerderrors.IsNotFound(err) {
  158. return false, -1, errors.Wrap(wrapError(err), "error getting containerd task for container")
  159. }
  160. if t != nil {
  161. s, err := t.Status(ctx)
  162. if err != nil {
  163. return false, -1, errors.Wrap(wrapError(err), "error getting task status")
  164. }
  165. alive = s.Status != containerd.Stopped
  166. pid = int(t.Pid())
  167. }
  168. cntr.bundleDir = filepath.Join(c.stateDir, id)
  169. cntr.ctr = ctr
  170. cntr.task = t
  171. // TODO(mlaventure): load execs
  172. c.logger.WithFields(logrus.Fields{
  173. "container": id,
  174. "alive": alive,
  175. "pid": pid,
  176. }).Debug("restored container")
  177. return alive, pid, nil
  178. }
  179. func (c *client) Create(ctx context.Context, id string, ociSpec *specs.Spec, runtimeOptions interface{}) error {
  180. if ctr := c.getContainer(id); ctr != nil {
  181. return errors.WithStack(newConflictError("id already in use"))
  182. }
  183. bdir, err := prepareBundleDir(filepath.Join(c.stateDir, id), ociSpec)
  184. if err != nil {
  185. return errdefs.System(errors.Wrap(err, "prepare bundle dir failed"))
  186. }
  187. c.logger.WithField("bundle", bdir).WithField("root", ociSpec.Root.Path).Debug("bundle dir created")
  188. cdCtr, err := c.client.NewContainer(ctx, id,
  189. containerd.WithSpec(ociSpec),
  190. // TODO(mlaventure): when containerd support lcow, revisit runtime value
  191. containerd.WithRuntime(fmt.Sprintf("io.containerd.runtime.v1.%s", runtime.GOOS), runtimeOptions))
  192. if err != nil {
  193. return wrapError(err)
  194. }
  195. c.Lock()
  196. c.containers[id] = &container{
  197. bundleDir: bdir,
  198. ctr: cdCtr,
  199. }
  200. c.Unlock()
  201. return nil
  202. }
  203. // Start create and start a task for the specified containerd id
  204. func (c *client) Start(ctx context.Context, id, checkpointDir string, withStdin bool, attachStdio StdioCallback) (int, error) {
  205. ctr := c.getContainer(id)
  206. if ctr == nil {
  207. return -1, errors.WithStack(newNotFoundError("no such container"))
  208. }
  209. if t := ctr.getTask(); t != nil {
  210. return -1, errors.WithStack(newConflictError("container already started"))
  211. }
  212. var (
  213. cp *types.Descriptor
  214. t containerd.Task
  215. rio cio.IO
  216. err error
  217. stdinCloseSync = make(chan struct{})
  218. )
  219. if checkpointDir != "" {
  220. // write checkpoint to the content store
  221. tar := archive.Diff(ctx, "", checkpointDir)
  222. cp, err = c.writeContent(ctx, images.MediaTypeContainerd1Checkpoint, checkpointDir, tar)
  223. // remove the checkpoint when we're done
  224. defer func() {
  225. if cp != nil {
  226. err := c.client.ContentStore().Delete(context.Background(), cp.Digest)
  227. if err != nil {
  228. c.logger.WithError(err).WithFields(logrus.Fields{
  229. "ref": checkpointDir,
  230. "digest": cp.Digest,
  231. }).Warnf("failed to delete temporary checkpoint entry")
  232. }
  233. }
  234. }()
  235. if err := tar.Close(); err != nil {
  236. return -1, errors.Wrap(err, "failed to close checkpoint tar stream")
  237. }
  238. if err != nil {
  239. return -1, errors.Wrapf(err, "failed to upload checkpoint to containerd")
  240. }
  241. }
  242. spec, err := ctr.ctr.Spec(ctx)
  243. if err != nil {
  244. return -1, errors.Wrap(err, "failed to retrieve spec")
  245. }
  246. uid, gid := getSpecUser(spec)
  247. t, err = ctr.ctr.NewTask(ctx,
  248. func(id string) (cio.IO, error) {
  249. fifos := newFIFOSet(ctr.bundleDir, InitProcessName, withStdin, spec.Process.Terminal)
  250. rio, err = c.createIO(fifos, id, InitProcessName, stdinCloseSync, attachStdio)
  251. return rio, err
  252. },
  253. func(_ context.Context, _ *containerd.Client, info *containerd.TaskInfo) error {
  254. info.Checkpoint = cp
  255. info.Options = &runctypes.CreateOptions{
  256. IoUid: uint32(uid),
  257. IoGid: uint32(gid),
  258. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  259. }
  260. return nil
  261. })
  262. if err != nil {
  263. close(stdinCloseSync)
  264. if rio != nil {
  265. rio.Cancel()
  266. rio.Close()
  267. }
  268. return -1, wrapError(err)
  269. }
  270. ctr.setTask(t)
  271. // Signal c.createIO that it can call CloseIO
  272. close(stdinCloseSync)
  273. if err := t.Start(ctx); err != nil {
  274. if _, err := t.Delete(ctx); err != nil {
  275. c.logger.WithError(err).WithField("container", id).
  276. Error("failed to delete task after fail start")
  277. }
  278. ctr.setTask(nil)
  279. return -1, wrapError(err)
  280. }
  281. return int(t.Pid()), nil
  282. }
  283. // Exec creates exec process.
  284. //
  285. // The containerd client calls Exec to register the exec config in the shim side.
  286. // When the client calls Start, the shim will create stdin fifo if needs. But
  287. // for the container main process, the stdin fifo will be created in Create not
  288. // the Start call. stdinCloseSync channel should be closed after Start exec
  289. // process.
  290. func (c *client) Exec(ctx context.Context, containerID, processID string, spec *specs.Process, withStdin bool, attachStdio StdioCallback) (int, error) {
  291. ctr := c.getContainer(containerID)
  292. if ctr == nil {
  293. return -1, errors.WithStack(newNotFoundError("no such container"))
  294. }
  295. t := ctr.getTask()
  296. if t == nil {
  297. return -1, errors.WithStack(newInvalidParameterError("container is not running"))
  298. }
  299. if p := ctr.getProcess(processID); p != nil {
  300. return -1, errors.WithStack(newConflictError("id already in use"))
  301. }
  302. var (
  303. p containerd.Process
  304. rio cio.IO
  305. err error
  306. stdinCloseSync = make(chan struct{})
  307. )
  308. fifos := newFIFOSet(ctr.bundleDir, processID, withStdin, spec.Terminal)
  309. defer func() {
  310. if err != nil {
  311. if rio != nil {
  312. rio.Cancel()
  313. rio.Close()
  314. }
  315. }
  316. }()
  317. p, err = t.Exec(ctx, processID, spec, func(id string) (cio.IO, error) {
  318. rio, err = c.createIO(fifos, containerID, processID, stdinCloseSync, attachStdio)
  319. return rio, err
  320. })
  321. if err != nil {
  322. close(stdinCloseSync)
  323. return -1, wrapError(err)
  324. }
  325. ctr.addProcess(processID, p)
  326. // Signal c.createIO that it can call CloseIO
  327. //
  328. // the stdin of exec process will be created after p.Start in containerd
  329. defer close(stdinCloseSync)
  330. if err = p.Start(ctx); err != nil {
  331. p.Delete(ctx)
  332. ctr.deleteProcess(processID)
  333. return -1, wrapError(err)
  334. }
  335. return int(p.Pid()), nil
  336. }
  337. func (c *client) SignalProcess(ctx context.Context, containerID, processID string, signal int) error {
  338. p, err := c.getProcess(containerID, processID)
  339. if err != nil {
  340. return err
  341. }
  342. return wrapError(p.Kill(ctx, syscall.Signal(signal)))
  343. }
  344. func (c *client) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
  345. p, err := c.getProcess(containerID, processID)
  346. if err != nil {
  347. return err
  348. }
  349. return p.Resize(ctx, uint32(width), uint32(height))
  350. }
  351. func (c *client) CloseStdin(ctx context.Context, containerID, processID string) error {
  352. p, err := c.getProcess(containerID, processID)
  353. if err != nil {
  354. return err
  355. }
  356. return p.CloseIO(ctx, containerd.WithStdinCloser)
  357. }
  358. func (c *client) Pause(ctx context.Context, containerID string) error {
  359. p, err := c.getProcess(containerID, InitProcessName)
  360. if err != nil {
  361. return err
  362. }
  363. return wrapError(p.(containerd.Task).Pause(ctx))
  364. }
  365. func (c *client) Resume(ctx context.Context, containerID string) error {
  366. p, err := c.getProcess(containerID, InitProcessName)
  367. if err != nil {
  368. return err
  369. }
  370. return p.(containerd.Task).Resume(ctx)
  371. }
  372. func (c *client) Stats(ctx context.Context, containerID string) (*Stats, error) {
  373. p, err := c.getProcess(containerID, InitProcessName)
  374. if err != nil {
  375. return nil, err
  376. }
  377. m, err := p.(containerd.Task).Metrics(ctx)
  378. if err != nil {
  379. return nil, err
  380. }
  381. v, err := typeurl.UnmarshalAny(m.Data)
  382. if err != nil {
  383. return nil, err
  384. }
  385. return interfaceToStats(m.Timestamp, v), nil
  386. }
  387. func (c *client) ListPids(ctx context.Context, containerID string) ([]uint32, error) {
  388. p, err := c.getProcess(containerID, InitProcessName)
  389. if err != nil {
  390. return nil, err
  391. }
  392. pis, err := p.(containerd.Task).Pids(ctx)
  393. if err != nil {
  394. return nil, err
  395. }
  396. var pids []uint32
  397. for _, i := range pis {
  398. pids = append(pids, i.Pid)
  399. }
  400. return pids, nil
  401. }
  402. func (c *client) Summary(ctx context.Context, containerID string) ([]Summary, error) {
  403. p, err := c.getProcess(containerID, InitProcessName)
  404. if err != nil {
  405. return nil, err
  406. }
  407. pis, err := p.(containerd.Task).Pids(ctx)
  408. if err != nil {
  409. return nil, err
  410. }
  411. var infos []Summary
  412. for _, pi := range pis {
  413. i, err := typeurl.UnmarshalAny(pi.Info)
  414. if err != nil {
  415. return nil, errors.Wrap(err, "unable to decode process details")
  416. }
  417. s, err := summaryFromInterface(i)
  418. if err != nil {
  419. return nil, err
  420. }
  421. infos = append(infos, *s)
  422. }
  423. return infos, nil
  424. }
  425. func (c *client) DeleteTask(ctx context.Context, containerID string) (uint32, time.Time, error) {
  426. p, err := c.getProcess(containerID, InitProcessName)
  427. if err != nil {
  428. return 255, time.Now(), nil
  429. }
  430. status, err := p.(containerd.Task).Delete(ctx)
  431. if err != nil {
  432. return 255, time.Now(), nil
  433. }
  434. if ctr := c.getContainer(containerID); ctr != nil {
  435. ctr.setTask(nil)
  436. }
  437. return status.ExitCode(), status.ExitTime(), nil
  438. }
  439. func (c *client) Delete(ctx context.Context, containerID string) error {
  440. ctr := c.getContainer(containerID)
  441. if ctr == nil {
  442. return errors.WithStack(newNotFoundError("no such container"))
  443. }
  444. if err := ctr.ctr.Delete(ctx); err != nil {
  445. return wrapError(err)
  446. }
  447. if os.Getenv("LIBCONTAINERD_NOCLEAN") != "1" {
  448. if err := os.RemoveAll(ctr.bundleDir); err != nil {
  449. c.logger.WithError(err).WithFields(logrus.Fields{
  450. "container": containerID,
  451. "bundle": ctr.bundleDir,
  452. }).Error("failed to remove state dir")
  453. }
  454. }
  455. c.removeContainer(containerID)
  456. return nil
  457. }
  458. func (c *client) Status(ctx context.Context, containerID string) (Status, error) {
  459. ctr := c.getContainer(containerID)
  460. if ctr == nil {
  461. return StatusUnknown, errors.WithStack(newNotFoundError("no such container"))
  462. }
  463. t := ctr.getTask()
  464. if t == nil {
  465. return StatusUnknown, errors.WithStack(newNotFoundError("no such task"))
  466. }
  467. s, err := t.Status(ctx)
  468. if err != nil {
  469. return StatusUnknown, wrapError(err)
  470. }
  471. return Status(s.Status), nil
  472. }
  473. func (c *client) CreateCheckpoint(ctx context.Context, containerID, checkpointDir string, exit bool) error {
  474. p, err := c.getProcess(containerID, InitProcessName)
  475. if err != nil {
  476. return err
  477. }
  478. opts := []containerd.CheckpointTaskOpts{}
  479. if exit {
  480. opts = append(opts, func(r *containerd.CheckpointTaskInfo) error {
  481. if r.Options == nil {
  482. r.Options = &runctypes.CheckpointOptions{
  483. Exit: true,
  484. }
  485. } else {
  486. opts, _ := r.Options.(*runctypes.CheckpointOptions)
  487. opts.Exit = true
  488. }
  489. return nil
  490. })
  491. }
  492. img, err := p.(containerd.Task).Checkpoint(ctx, opts...)
  493. if err != nil {
  494. return wrapError(err)
  495. }
  496. // Whatever happens, delete the checkpoint from containerd
  497. defer func() {
  498. err := c.client.ImageService().Delete(context.Background(), img.Name())
  499. if err != nil {
  500. c.logger.WithError(err).WithField("digest", img.Target().Digest).
  501. Warnf("failed to delete checkpoint image")
  502. }
  503. }()
  504. b, err := content.ReadBlob(ctx, c.client.ContentStore(), img.Target())
  505. if err != nil {
  506. return errdefs.System(errors.Wrapf(err, "failed to retrieve checkpoint data"))
  507. }
  508. var index v1.Index
  509. if err := json.Unmarshal(b, &index); err != nil {
  510. return errdefs.System(errors.Wrapf(err, "failed to decode checkpoint data"))
  511. }
  512. var cpDesc *v1.Descriptor
  513. for _, m := range index.Manifests {
  514. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  515. cpDesc = &m
  516. break
  517. }
  518. }
  519. if cpDesc == nil {
  520. return errdefs.System(errors.Wrapf(err, "invalid checkpoint"))
  521. }
  522. rat, err := c.client.ContentStore().ReaderAt(ctx, *cpDesc)
  523. if err != nil {
  524. return errdefs.System(errors.Wrapf(err, "failed to get checkpoint reader"))
  525. }
  526. defer rat.Close()
  527. _, err = archive.Apply(ctx, checkpointDir, content.NewReader(rat))
  528. if err != nil {
  529. return errdefs.System(errors.Wrapf(err, "failed to read checkpoint reader"))
  530. }
  531. return err
  532. }
  533. func (c *client) getContainer(id string) *container {
  534. c.RLock()
  535. ctr := c.containers[id]
  536. c.RUnlock()
  537. return ctr
  538. }
  539. func (c *client) removeContainer(id string) {
  540. c.Lock()
  541. delete(c.containers, id)
  542. c.Unlock()
  543. }
  544. func (c *client) getProcess(containerID, processID string) (containerd.Process, error) {
  545. ctr := c.getContainer(containerID)
  546. if ctr == nil {
  547. return nil, errors.WithStack(newNotFoundError("no such container"))
  548. }
  549. t := ctr.getTask()
  550. if t == nil {
  551. return nil, errors.WithStack(newNotFoundError("container is not running"))
  552. }
  553. if processID == InitProcessName {
  554. return t, nil
  555. }
  556. p := ctr.getProcess(processID)
  557. if p == nil {
  558. return nil, errors.WithStack(newNotFoundError("no such exec"))
  559. }
  560. return p, nil
  561. }
  562. // createIO creates the io to be used by a process
  563. // This needs to get a pointer to interface as upon closure the process may not have yet been registered
  564. func (c *client) createIO(fifos *cio.FIFOSet, containerID, processID string, stdinCloseSync chan struct{}, attachStdio StdioCallback) (cio.IO, error) {
  565. var (
  566. io *cio.DirectIO
  567. err error
  568. )
  569. io, err = cio.NewDirectIO(context.Background(), fifos)
  570. if err != nil {
  571. return nil, err
  572. }
  573. if io.Stdin != nil {
  574. var (
  575. err error
  576. stdinOnce sync.Once
  577. )
  578. pipe := io.Stdin
  579. io.Stdin = ioutils.NewWriteCloserWrapper(pipe, func() error {
  580. stdinOnce.Do(func() {
  581. err = pipe.Close()
  582. // Do the rest in a new routine to avoid a deadlock if the
  583. // Exec/Start call failed.
  584. go func() {
  585. <-stdinCloseSync
  586. p, err := c.getProcess(containerID, processID)
  587. if err == nil {
  588. err = p.CloseIO(context.Background(), containerd.WithStdinCloser)
  589. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  590. err = nil
  591. }
  592. }
  593. }()
  594. })
  595. return err
  596. })
  597. }
  598. rio, err := attachStdio(io)
  599. if err != nil {
  600. io.Cancel()
  601. io.Close()
  602. }
  603. return rio, err
  604. }
  605. func (c *client) processEvent(ctr *container, et EventType, ei EventInfo) {
  606. c.eventQ.append(ei.ContainerID, func() {
  607. err := c.backend.ProcessEvent(ei.ContainerID, et, ei)
  608. if err != nil {
  609. c.logger.WithError(err).WithFields(logrus.Fields{
  610. "container": ei.ContainerID,
  611. "event": et,
  612. "event-info": ei,
  613. }).Error("failed to process event")
  614. }
  615. if et == EventExit && ei.ProcessID != ei.ContainerID {
  616. p := ctr.getProcess(ei.ProcessID)
  617. if p == nil {
  618. c.logger.WithError(errors.New("no such process")).
  619. WithFields(logrus.Fields{
  620. "container": ei.ContainerID,
  621. "process": ei.ProcessID,
  622. }).Error("exit event")
  623. return
  624. }
  625. _, err = p.Delete(context.Background())
  626. if err != nil {
  627. c.logger.WithError(err).WithFields(logrus.Fields{
  628. "container": ei.ContainerID,
  629. "process": ei.ProcessID,
  630. }).Warn("failed to delete process")
  631. }
  632. ctr.deleteProcess(ei.ProcessID)
  633. ctr := c.getContainer(ei.ContainerID)
  634. if ctr == nil {
  635. c.logger.WithFields(logrus.Fields{
  636. "container": ei.ContainerID,
  637. }).Error("failed to find container")
  638. } else {
  639. newFIFOSet(ctr.bundleDir, ei.ProcessID, true, false).Close()
  640. }
  641. }
  642. })
  643. }
  644. func (c *client) processEventStream(ctx context.Context, ns string) {
  645. var (
  646. err error
  647. ev *events.Envelope
  648. et EventType
  649. ei EventInfo
  650. ctr *container
  651. )
  652. // Filter on both namespace *and* topic. To create an "and" filter,
  653. // this must be a single, comma-separated string
  654. eventStream, errC := c.client.EventService().Subscribe(ctx, "namespace=="+ns+",topic~=|^/tasks/|")
  655. c.logger.Debug("processing event stream")
  656. var oomKilled bool
  657. for {
  658. select {
  659. case err = <-errC:
  660. if err != nil {
  661. errStatus, ok := status.FromError(err)
  662. if !ok || errStatus.Code() != codes.Canceled {
  663. c.logger.WithError(err).Error("failed to get event")
  664. go c.processEventStream(ctx, ns)
  665. } else {
  666. c.logger.WithError(ctx.Err()).Info("stopping event stream following graceful shutdown")
  667. }
  668. }
  669. return
  670. case ev = <-eventStream:
  671. if ev.Event == nil {
  672. c.logger.WithField("event", ev).Warn("invalid event")
  673. continue
  674. }
  675. v, err := typeurl.UnmarshalAny(ev.Event)
  676. if err != nil {
  677. c.logger.WithError(err).WithField("event", ev).Warn("failed to unmarshal event")
  678. continue
  679. }
  680. c.logger.WithField("topic", ev.Topic).Debug("event")
  681. switch t := v.(type) {
  682. case *apievents.TaskCreate:
  683. et = EventCreate
  684. ei = EventInfo{
  685. ContainerID: t.ContainerID,
  686. ProcessID: t.ContainerID,
  687. Pid: t.Pid,
  688. }
  689. case *apievents.TaskStart:
  690. et = EventStart
  691. ei = EventInfo{
  692. ContainerID: t.ContainerID,
  693. ProcessID: t.ContainerID,
  694. Pid: t.Pid,
  695. }
  696. case *apievents.TaskExit:
  697. et = EventExit
  698. ei = EventInfo{
  699. ContainerID: t.ContainerID,
  700. ProcessID: t.ID,
  701. Pid: t.Pid,
  702. ExitCode: t.ExitStatus,
  703. ExitedAt: t.ExitedAt,
  704. }
  705. case *apievents.TaskOOM:
  706. et = EventOOM
  707. ei = EventInfo{
  708. ContainerID: t.ContainerID,
  709. OOMKilled: true,
  710. }
  711. oomKilled = true
  712. case *apievents.TaskExecAdded:
  713. et = EventExecAdded
  714. ei = EventInfo{
  715. ContainerID: t.ContainerID,
  716. ProcessID: t.ExecID,
  717. }
  718. case *apievents.TaskExecStarted:
  719. et = EventExecStarted
  720. ei = EventInfo{
  721. ContainerID: t.ContainerID,
  722. ProcessID: t.ExecID,
  723. Pid: t.Pid,
  724. }
  725. case *apievents.TaskPaused:
  726. et = EventPaused
  727. ei = EventInfo{
  728. ContainerID: t.ContainerID,
  729. }
  730. case *apievents.TaskResumed:
  731. et = EventResumed
  732. ei = EventInfo{
  733. ContainerID: t.ContainerID,
  734. }
  735. default:
  736. c.logger.WithFields(logrus.Fields{
  737. "topic": ev.Topic,
  738. "type": reflect.TypeOf(t)},
  739. ).Info("ignoring event")
  740. continue
  741. }
  742. ctr = c.getContainer(ei.ContainerID)
  743. if ctr == nil {
  744. c.logger.WithField("container", ei.ContainerID).Warn("unknown container")
  745. continue
  746. }
  747. if oomKilled {
  748. ctr.setOOMKilled(true)
  749. oomKilled = false
  750. }
  751. ei.OOMKilled = ctr.getOOMKilled()
  752. c.processEvent(ctr, et, ei)
  753. }
  754. }
  755. }
  756. func (c *client) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
  757. writer, err := c.client.ContentStore().Writer(ctx, content.WithRef(ref))
  758. if err != nil {
  759. return nil, err
  760. }
  761. defer writer.Close()
  762. size, err := io.Copy(writer, r)
  763. if err != nil {
  764. return nil, err
  765. }
  766. labels := map[string]string{
  767. "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339),
  768. }
  769. if err := writer.Commit(ctx, 0, "", content.WithLabels(labels)); err != nil {
  770. return nil, err
  771. }
  772. return &types.Descriptor{
  773. MediaType: mediaType,
  774. Digest: writer.Digest(),
  775. Size_: size,
  776. }, nil
  777. }
  778. func wrapError(err error) error {
  779. switch {
  780. case err == nil:
  781. return nil
  782. case containerderrors.IsNotFound(err):
  783. return errdefs.NotFound(err)
  784. }
  785. msg := err.Error()
  786. for _, s := range []string{"container does not exist", "not found", "no such container"} {
  787. if strings.Contains(msg, s) {
  788. return errdefs.NotFound(err)
  789. }
  790. }
  791. return err
  792. }