client.go 25 KB

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