client.go 25 KB

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