client_daemon.go 20 KB

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