client.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. if _, err := t.Delete(ctx); err != nil {
  222. c.logger.WithError(err).WithField("container", id).
  223. Error("failed to delete task after fail start")
  224. }
  225. return -1, wrapError(err)
  226. }
  227. return int(t.Pid()), nil
  228. }
  229. // Exec creates exec process.
  230. //
  231. // The containerd client calls Exec to register the exec config in the shim side.
  232. // When the client calls Start, the shim will create stdin fifo if needs. But
  233. // for the container main process, the stdin fifo will be created in Create not
  234. // the Start call. stdinCloseSync channel should be closed after Start exec
  235. // process.
  236. func (c *client) Exec(ctx context.Context, containerID, processID string, spec *specs.Process, withStdin bool, attachStdio libcontainerdtypes.StdioCallback) (int, error) {
  237. ctr, err := c.getContainer(ctx, containerID)
  238. if err != nil {
  239. return -1, err
  240. }
  241. t, err := ctr.Task(ctx, nil)
  242. if err != nil {
  243. if containerderrors.IsNotFound(err) {
  244. return -1, errors.WithStack(errdefs.InvalidParameter(errors.New("container is not running")))
  245. }
  246. return -1, wrapError(err)
  247. }
  248. var (
  249. p containerd.Process
  250. rio cio.IO
  251. stdinCloseSync = make(chan struct{})
  252. )
  253. labels, err := ctr.Labels(ctx)
  254. if err != nil {
  255. return -1, wrapError(err)
  256. }
  257. fifos := newFIFOSet(labels[DockerContainerBundlePath], processID, withStdin, spec.Terminal)
  258. defer func() {
  259. if err != nil {
  260. if rio != nil {
  261. rio.Cancel()
  262. rio.Close()
  263. }
  264. }
  265. }()
  266. p, err = t.Exec(ctx, processID, spec, func(id string) (cio.IO, error) {
  267. rio, err = c.createIO(fifos, containerID, processID, stdinCloseSync, attachStdio)
  268. return rio, err
  269. })
  270. if err != nil {
  271. close(stdinCloseSync)
  272. if containerderrors.IsAlreadyExists(err) {
  273. return -1, errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
  274. }
  275. return -1, wrapError(err)
  276. }
  277. // Signal c.createIO that it can call CloseIO
  278. //
  279. // the stdin of exec process will be created after p.Start in containerd
  280. defer close(stdinCloseSync)
  281. if err = p.Start(ctx); err != nil {
  282. // use new context for cleanup because old one may be cancelled by user, but leave a timeout to make sure
  283. // we are not waiting forever if containerd is unresponsive or to work around fifo cancelling issues in
  284. // older containerd-shim
  285. ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
  286. defer cancel()
  287. p.Delete(ctx)
  288. return -1, wrapError(err)
  289. }
  290. return int(p.Pid()), nil
  291. }
  292. func (c *client) SignalProcess(ctx context.Context, containerID, processID string, signal syscall.Signal) error {
  293. p, err := c.getProcess(ctx, containerID, processID)
  294. if err != nil {
  295. return err
  296. }
  297. return wrapError(p.Kill(ctx, signal))
  298. }
  299. func (c *client) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
  300. p, err := c.getProcess(ctx, containerID, processID)
  301. if err != nil {
  302. return err
  303. }
  304. return p.Resize(ctx, uint32(width), uint32(height))
  305. }
  306. func (c *client) CloseStdin(ctx context.Context, containerID, processID string) error {
  307. p, err := c.getProcess(ctx, containerID, processID)
  308. if err != nil {
  309. return err
  310. }
  311. return p.CloseIO(ctx, containerd.WithStdinCloser)
  312. }
  313. func (c *client) Pause(ctx context.Context, containerID string) error {
  314. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  315. if err != nil {
  316. return err
  317. }
  318. return wrapError(p.(containerd.Task).Pause(ctx))
  319. }
  320. func (c *client) Resume(ctx context.Context, containerID string) error {
  321. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  322. if err != nil {
  323. return err
  324. }
  325. return p.(containerd.Task).Resume(ctx)
  326. }
  327. func (c *client) Stats(ctx context.Context, containerID string) (*libcontainerdtypes.Stats, error) {
  328. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  329. if err != nil {
  330. return nil, err
  331. }
  332. m, err := p.(containerd.Task).Metrics(ctx)
  333. if err != nil {
  334. return nil, err
  335. }
  336. v, err := typeurl.UnmarshalAny(m.Data)
  337. if err != nil {
  338. return nil, err
  339. }
  340. return libcontainerdtypes.InterfaceToStats(m.Timestamp, v), nil
  341. }
  342. func (c *client) ListPids(ctx context.Context, containerID string) ([]uint32, error) {
  343. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  344. if err != nil {
  345. return nil, err
  346. }
  347. pis, err := p.(containerd.Task).Pids(ctx)
  348. if err != nil {
  349. return nil, err
  350. }
  351. var pids []uint32
  352. for _, i := range pis {
  353. pids = append(pids, i.Pid)
  354. }
  355. return pids, nil
  356. }
  357. func (c *client) Summary(ctx context.Context, containerID string) ([]libcontainerdtypes.Summary, error) {
  358. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  359. if err != nil {
  360. return nil, err
  361. }
  362. pis, err := p.(containerd.Task).Pids(ctx)
  363. if err != nil {
  364. return nil, err
  365. }
  366. var infos []libcontainerdtypes.Summary
  367. for _, pi := range pis {
  368. i, err := typeurl.UnmarshalAny(pi.Info)
  369. if err != nil {
  370. return nil, errors.Wrap(err, "unable to decode process details")
  371. }
  372. s, err := summaryFromInterface(i)
  373. if err != nil {
  374. return nil, err
  375. }
  376. infos = append(infos, *s)
  377. }
  378. return infos, nil
  379. }
  380. type restoredProcess struct {
  381. p containerd.Process
  382. }
  383. func (p *restoredProcess) Delete(ctx context.Context) (uint32, time.Time, error) {
  384. if p.p == nil {
  385. return 255, time.Now(), nil
  386. }
  387. status, err := p.p.Delete(ctx)
  388. if err != nil {
  389. return 255, time.Now(), nil
  390. }
  391. return status.ExitCode(), status.ExitTime(), nil
  392. }
  393. func (c *client) DeleteTask(ctx context.Context, containerID string) (uint32, time.Time, error) {
  394. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  395. if err != nil {
  396. return 255, time.Now(), nil
  397. }
  398. status, err := p.Delete(ctx)
  399. if err != nil {
  400. return 255, time.Now(), nil
  401. }
  402. return status.ExitCode(), status.ExitTime(), nil
  403. }
  404. func (c *client) Delete(ctx context.Context, containerID string) error {
  405. ctr, err := c.getContainer(ctx, containerID)
  406. if err != nil {
  407. return err
  408. }
  409. labels, err := ctr.Labels(ctx)
  410. if err != nil {
  411. return err
  412. }
  413. bundle := labels[DockerContainerBundlePath]
  414. if err := ctr.Delete(ctx); err != nil {
  415. return wrapError(err)
  416. }
  417. c.oomMu.Lock()
  418. delete(c.oom, containerID)
  419. c.oomMu.Unlock()
  420. c.v2runcoptionsMu.Lock()
  421. delete(c.v2runcoptions, containerID)
  422. c.v2runcoptionsMu.Unlock()
  423. if os.Getenv("LIBCONTAINERD_NOCLEAN") != "1" {
  424. if err := os.RemoveAll(bundle); err != nil {
  425. c.logger.WithError(err).WithFields(logrus.Fields{
  426. "container": containerID,
  427. "bundle": bundle,
  428. }).Error("failed to remove state dir")
  429. }
  430. }
  431. return nil
  432. }
  433. func (c *client) Status(ctx context.Context, containerID string) (containerd.ProcessStatus, error) {
  434. t, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  435. if err != nil {
  436. return containerd.Unknown, err
  437. }
  438. s, err := t.Status(ctx)
  439. if err != nil {
  440. return containerd.Unknown, wrapError(err)
  441. }
  442. return s.Status, nil
  443. }
  444. func (c *client) getCheckpointOptions(id string, exit bool) containerd.CheckpointTaskOpts {
  445. return func(r *containerd.CheckpointTaskInfo) error {
  446. if r.Options == nil {
  447. c.v2runcoptionsMu.Lock()
  448. _, ok := c.v2runcoptions[id]
  449. c.v2runcoptionsMu.Unlock()
  450. if ok {
  451. r.Options = &v2runcoptions.CheckpointOptions{Exit: exit}
  452. }
  453. return nil
  454. }
  455. switch opts := r.Options.(type) {
  456. case *v2runcoptions.CheckpointOptions:
  457. opts.Exit = exit
  458. }
  459. return nil
  460. }
  461. }
  462. func (c *client) CreateCheckpoint(ctx context.Context, containerID, checkpointDir string, exit bool) error {
  463. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  464. if err != nil {
  465. return err
  466. }
  467. opts := []containerd.CheckpointTaskOpts{c.getCheckpointOptions(containerID, exit)}
  468. img, err := p.(containerd.Task).Checkpoint(ctx, opts...)
  469. if err != nil {
  470. return wrapError(err)
  471. }
  472. // Whatever happens, delete the checkpoint from containerd
  473. defer func() {
  474. err := c.client.ImageService().Delete(context.Background(), img.Name())
  475. if err != nil {
  476. c.logger.WithError(err).WithField("digest", img.Target().Digest).
  477. Warnf("failed to delete checkpoint image")
  478. }
  479. }()
  480. b, err := content.ReadBlob(ctx, c.client.ContentStore(), img.Target())
  481. if err != nil {
  482. return errdefs.System(errors.Wrapf(err, "failed to retrieve checkpoint data"))
  483. }
  484. var index v1.Index
  485. if err := json.Unmarshal(b, &index); err != nil {
  486. return errdefs.System(errors.Wrapf(err, "failed to decode checkpoint data"))
  487. }
  488. var cpDesc *v1.Descriptor
  489. for _, m := range index.Manifests {
  490. m := m
  491. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  492. cpDesc = &m //nolint:gosec
  493. break
  494. }
  495. }
  496. if cpDesc == nil {
  497. return errdefs.System(errors.Wrapf(err, "invalid checkpoint"))
  498. }
  499. rat, err := c.client.ContentStore().ReaderAt(ctx, *cpDesc)
  500. if err != nil {
  501. return errdefs.System(errors.Wrapf(err, "failed to get checkpoint reader"))
  502. }
  503. defer rat.Close()
  504. _, err = archive.Apply(ctx, checkpointDir, content.NewReader(rat))
  505. if err != nil {
  506. return errdefs.System(errors.Wrapf(err, "failed to read checkpoint reader"))
  507. }
  508. return err
  509. }
  510. func (c *client) getContainer(ctx context.Context, id string) (containerd.Container, error) {
  511. ctr, err := c.client.LoadContainer(ctx, id)
  512. if err != nil {
  513. if containerderrors.IsNotFound(err) {
  514. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  515. }
  516. return nil, wrapError(err)
  517. }
  518. return ctr, nil
  519. }
  520. func (c *client) getProcess(ctx context.Context, containerID, processID string) (containerd.Process, error) {
  521. ctr, err := c.getContainer(ctx, containerID)
  522. if err != nil {
  523. return nil, err
  524. }
  525. t, err := ctr.Task(ctx, nil)
  526. if err != nil {
  527. if containerderrors.IsNotFound(err) {
  528. return nil, errors.WithStack(errdefs.NotFound(errors.New("container is not running")))
  529. }
  530. return nil, wrapError(err)
  531. }
  532. if processID == libcontainerdtypes.InitProcessName {
  533. return t, nil
  534. }
  535. p, err := t.LoadProcess(ctx, processID, nil)
  536. if err != nil {
  537. if containerderrors.IsNotFound(err) {
  538. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such exec")))
  539. }
  540. return nil, wrapError(err)
  541. }
  542. return p, nil
  543. }
  544. // createIO creates the io to be used by a process
  545. // This needs to get a pointer to interface as upon closure the process may not have yet been registered
  546. func (c *client) createIO(fifos *cio.FIFOSet, containerID, processID string, stdinCloseSync chan struct{}, attachStdio libcontainerdtypes.StdioCallback) (cio.IO, error) {
  547. var (
  548. io *cio.DirectIO
  549. err error
  550. )
  551. io, err = c.newDirectIO(context.Background(), fifos)
  552. if err != nil {
  553. return nil, err
  554. }
  555. if io.Stdin != nil {
  556. var (
  557. err error
  558. stdinOnce sync.Once
  559. )
  560. pipe := io.Stdin
  561. io.Stdin = ioutils.NewWriteCloserWrapper(pipe, func() error {
  562. stdinOnce.Do(func() {
  563. err = pipe.Close()
  564. // Do the rest in a new routine to avoid a deadlock if the
  565. // Exec/Start call failed.
  566. go func() {
  567. <-stdinCloseSync
  568. p, err := c.getProcess(context.Background(), containerID, processID)
  569. if err == nil {
  570. err = p.CloseIO(context.Background(), containerd.WithStdinCloser)
  571. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  572. err = nil
  573. }
  574. }
  575. }()
  576. })
  577. return err
  578. })
  579. }
  580. rio, err := attachStdio(io)
  581. if err != nil {
  582. io.Cancel()
  583. io.Close()
  584. }
  585. return rio, err
  586. }
  587. func (c *client) processEvent(ctx context.Context, et libcontainerdtypes.EventType, ei libcontainerdtypes.EventInfo) {
  588. c.eventQ.Append(ei.ContainerID, func() {
  589. err := c.backend.ProcessEvent(ei.ContainerID, et, ei)
  590. if err != nil {
  591. c.logger.WithError(err).WithFields(logrus.Fields{
  592. "container": ei.ContainerID,
  593. "event": et,
  594. "event-info": ei,
  595. }).Error("failed to process event")
  596. }
  597. if et == libcontainerdtypes.EventExit && ei.ProcessID != ei.ContainerID {
  598. p, err := c.getProcess(ctx, ei.ContainerID, ei.ProcessID)
  599. if err != nil {
  600. c.logger.WithError(errors.New("no such process")).
  601. WithFields(logrus.Fields{
  602. "error": err,
  603. "container": ei.ContainerID,
  604. "process": ei.ProcessID,
  605. }).Error("exit event")
  606. return
  607. }
  608. ctr, err := c.getContainer(ctx, ei.ContainerID)
  609. if err != nil {
  610. c.logger.WithFields(logrus.Fields{
  611. "container": ei.ContainerID,
  612. "error": err,
  613. }).Error("failed to find container")
  614. } else {
  615. labels, err := ctr.Labels(ctx)
  616. if err != nil {
  617. c.logger.WithFields(logrus.Fields{
  618. "container": ei.ContainerID,
  619. "error": err,
  620. }).Error("failed to get container labels")
  621. return
  622. }
  623. newFIFOSet(labels[DockerContainerBundlePath], ei.ProcessID, true, false).Close()
  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. }
  633. })
  634. }
  635. func (c *client) waitServe(ctx context.Context) bool {
  636. t := 100 * time.Millisecond
  637. delay := time.NewTimer(t)
  638. if !delay.Stop() {
  639. <-delay.C
  640. }
  641. defer delay.Stop()
  642. // `IsServing` will actually block until the service is ready.
  643. // However it can return early, so we'll loop with a delay to handle it.
  644. for {
  645. serving, err := c.client.IsServing(ctx)
  646. if err != nil {
  647. if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
  648. return false
  649. }
  650. logrus.WithError(err).Warn("Error while testing if containerd API is ready")
  651. }
  652. if serving {
  653. return true
  654. }
  655. delay.Reset(t)
  656. select {
  657. case <-ctx.Done():
  658. return false
  659. case <-delay.C:
  660. }
  661. }
  662. }
  663. func (c *client) processEventStream(ctx context.Context, ns string) {
  664. var (
  665. err error
  666. ev *events.Envelope
  667. et libcontainerdtypes.EventType
  668. ei libcontainerdtypes.EventInfo
  669. )
  670. // Create a new context specifically for this subscription.
  671. // The context must be cancelled to cancel the subscription.
  672. // In cases where we have to restart event stream processing,
  673. // we'll need the original context b/c this one will be cancelled
  674. subCtx, cancel := context.WithCancel(ctx)
  675. defer cancel()
  676. // Filter on both namespace *and* topic. To create an "and" filter,
  677. // this must be a single, comma-separated string
  678. eventStream, errC := c.client.EventService().Subscribe(subCtx, "namespace=="+ns+",topic~=|^/tasks/|")
  679. c.logger.Debug("processing event stream")
  680. for {
  681. var oomKilled bool
  682. select {
  683. case err = <-errC:
  684. if err != nil {
  685. errStatus, ok := status.FromError(err)
  686. if !ok || errStatus.Code() != codes.Canceled {
  687. c.logger.WithError(err).Error("Failed to get event")
  688. c.logger.Info("Waiting for containerd to be ready to restart event processing")
  689. if c.waitServe(ctx) {
  690. go c.processEventStream(ctx, ns)
  691. return
  692. }
  693. }
  694. c.logger.WithError(ctx.Err()).Info("stopping event stream following graceful shutdown")
  695. }
  696. return
  697. case ev = <-eventStream:
  698. if ev.Event == nil {
  699. c.logger.WithField("event", ev).Warn("invalid event")
  700. continue
  701. }
  702. v, err := typeurl.UnmarshalAny(ev.Event)
  703. if err != nil {
  704. c.logger.WithError(err).WithField("event", ev).Warn("failed to unmarshal event")
  705. continue
  706. }
  707. c.logger.WithField("topic", ev.Topic).Debug("event")
  708. switch t := v.(type) {
  709. case *apievents.TaskCreate:
  710. et = libcontainerdtypes.EventCreate
  711. ei = libcontainerdtypes.EventInfo{
  712. ContainerID: t.ContainerID,
  713. ProcessID: t.ContainerID,
  714. Pid: t.Pid,
  715. }
  716. case *apievents.TaskStart:
  717. et = libcontainerdtypes.EventStart
  718. ei = libcontainerdtypes.EventInfo{
  719. ContainerID: t.ContainerID,
  720. ProcessID: t.ContainerID,
  721. Pid: t.Pid,
  722. }
  723. case *apievents.TaskExit:
  724. et = libcontainerdtypes.EventExit
  725. ei = libcontainerdtypes.EventInfo{
  726. ContainerID: t.ContainerID,
  727. ProcessID: t.ID,
  728. Pid: t.Pid,
  729. ExitCode: t.ExitStatus,
  730. ExitedAt: t.ExitedAt,
  731. }
  732. case *apievents.TaskOOM:
  733. et = libcontainerdtypes.EventOOM
  734. ei = libcontainerdtypes.EventInfo{
  735. ContainerID: t.ContainerID,
  736. OOMKilled: true,
  737. }
  738. oomKilled = true
  739. case *apievents.TaskExecAdded:
  740. et = libcontainerdtypes.EventExecAdded
  741. ei = libcontainerdtypes.EventInfo{
  742. ContainerID: t.ContainerID,
  743. ProcessID: t.ExecID,
  744. }
  745. case *apievents.TaskExecStarted:
  746. et = libcontainerdtypes.EventExecStarted
  747. ei = libcontainerdtypes.EventInfo{
  748. ContainerID: t.ContainerID,
  749. ProcessID: t.ExecID,
  750. Pid: t.Pid,
  751. }
  752. case *apievents.TaskPaused:
  753. et = libcontainerdtypes.EventPaused
  754. ei = libcontainerdtypes.EventInfo{
  755. ContainerID: t.ContainerID,
  756. }
  757. case *apievents.TaskResumed:
  758. et = libcontainerdtypes.EventResumed
  759. ei = libcontainerdtypes.EventInfo{
  760. ContainerID: t.ContainerID,
  761. }
  762. case *apievents.TaskDelete:
  763. c.logger.WithFields(logrus.Fields{
  764. "topic": ev.Topic,
  765. "type": reflect.TypeOf(t),
  766. "container": t.ContainerID},
  767. ).Info("ignoring event")
  768. continue
  769. default:
  770. c.logger.WithFields(logrus.Fields{
  771. "topic": ev.Topic,
  772. "type": reflect.TypeOf(t)},
  773. ).Info("ignoring event")
  774. continue
  775. }
  776. c.oomMu.Lock()
  777. if oomKilled {
  778. c.oom[ei.ContainerID] = true
  779. }
  780. ei.OOMKilled = c.oom[ei.ContainerID]
  781. c.oomMu.Unlock()
  782. c.processEvent(ctx, et, ei)
  783. }
  784. }
  785. }
  786. func (c *client) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
  787. writer, err := c.client.ContentStore().Writer(ctx, content.WithRef(ref))
  788. if err != nil {
  789. return nil, err
  790. }
  791. defer writer.Close()
  792. size, err := io.Copy(writer, r)
  793. if err != nil {
  794. return nil, err
  795. }
  796. labels := map[string]string{
  797. "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339),
  798. }
  799. if err := writer.Commit(ctx, 0, "", content.WithLabels(labels)); err != nil {
  800. return nil, err
  801. }
  802. return &types.Descriptor{
  803. MediaType: mediaType,
  804. Digest: writer.Digest(),
  805. Size_: size,
  806. }, nil
  807. }
  808. func (c *client) bundleDir(id string) string {
  809. return filepath.Join(c.stateDir, id)
  810. }
  811. func wrapError(err error) error {
  812. switch {
  813. case err == nil:
  814. return nil
  815. case containerderrors.IsNotFound(err):
  816. return errdefs.NotFound(err)
  817. }
  818. msg := err.Error()
  819. for _, s := range []string{"container does not exist", "not found", "no such container"} {
  820. if strings.Contains(msg, s) {
  821. return errdefs.NotFound(err)
  822. }
  823. }
  824. return err
  825. }