client.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  502. cpDesc = &m
  503. break
  504. }
  505. }
  506. if cpDesc == nil {
  507. return errdefs.System(errors.Wrapf(err, "invalid checkpoint"))
  508. }
  509. rat, err := c.client.ContentStore().ReaderAt(ctx, *cpDesc)
  510. if err != nil {
  511. return errdefs.System(errors.Wrapf(err, "failed to get checkpoint reader"))
  512. }
  513. defer rat.Close()
  514. _, err = archive.Apply(ctx, checkpointDir, content.NewReader(rat))
  515. if err != nil {
  516. return errdefs.System(errors.Wrapf(err, "failed to read checkpoint reader"))
  517. }
  518. return err
  519. }
  520. func (c *client) getContainer(ctx context.Context, id string) (containerd.Container, error) {
  521. ctr, err := c.client.LoadContainer(ctx, id)
  522. if err != nil {
  523. if containerderrors.IsNotFound(err) {
  524. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  525. }
  526. return nil, wrapError(err)
  527. }
  528. return ctr, nil
  529. }
  530. func (c *client) getProcess(ctx context.Context, containerID, processID string) (containerd.Process, error) {
  531. ctr, err := c.getContainer(ctx, containerID)
  532. if err != nil {
  533. return nil, err
  534. }
  535. t, err := ctr.Task(ctx, nil)
  536. if err != nil {
  537. if containerderrors.IsNotFound(err) {
  538. return nil, errors.WithStack(errdefs.NotFound(errors.New("container is not running")))
  539. }
  540. return nil, wrapError(err)
  541. }
  542. if processID == libcontainerdtypes.InitProcessName {
  543. return t, nil
  544. }
  545. p, err := t.LoadProcess(ctx, processID, nil)
  546. if err != nil {
  547. if containerderrors.IsNotFound(err) {
  548. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such exec")))
  549. }
  550. return nil, wrapError(err)
  551. }
  552. return p, nil
  553. }
  554. // createIO creates the io to be used by a process
  555. // This needs to get a pointer to interface as upon closure the process may not have yet been registered
  556. func (c *client) createIO(fifos *cio.FIFOSet, containerID, processID string, stdinCloseSync chan struct{}, attachStdio libcontainerdtypes.StdioCallback) (cio.IO, error) {
  557. var (
  558. io *cio.DirectIO
  559. err error
  560. )
  561. io, err = c.newDirectIO(context.Background(), fifos)
  562. if err != nil {
  563. return nil, err
  564. }
  565. if io.Stdin != nil {
  566. var (
  567. err error
  568. stdinOnce sync.Once
  569. )
  570. pipe := io.Stdin
  571. io.Stdin = ioutils.NewWriteCloserWrapper(pipe, func() error {
  572. stdinOnce.Do(func() {
  573. err = pipe.Close()
  574. // Do the rest in a new routine to avoid a deadlock if the
  575. // Exec/Start call failed.
  576. go func() {
  577. <-stdinCloseSync
  578. p, err := c.getProcess(context.Background(), containerID, processID)
  579. if err == nil {
  580. err = p.CloseIO(context.Background(), containerd.WithStdinCloser)
  581. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  582. err = nil
  583. }
  584. }
  585. }()
  586. })
  587. return err
  588. })
  589. }
  590. rio, err := attachStdio(io)
  591. if err != nil {
  592. io.Cancel()
  593. io.Close()
  594. }
  595. return rio, err
  596. }
  597. func (c *client) processEvent(ctx context.Context, et libcontainerdtypes.EventType, ei libcontainerdtypes.EventInfo) {
  598. c.eventQ.Append(ei.ContainerID, func() {
  599. err := c.backend.ProcessEvent(ei.ContainerID, et, ei)
  600. if err != nil {
  601. c.logger.WithError(err).WithFields(logrus.Fields{
  602. "container": ei.ContainerID,
  603. "event": et,
  604. "event-info": ei,
  605. }).Error("failed to process event")
  606. }
  607. if et == libcontainerdtypes.EventExit && ei.ProcessID != ei.ContainerID {
  608. p, err := c.getProcess(ctx, ei.ContainerID, ei.ProcessID)
  609. if err != nil {
  610. c.logger.WithError(errors.New("no such process")).
  611. WithFields(logrus.Fields{
  612. "error": err,
  613. "container": ei.ContainerID,
  614. "process": ei.ProcessID,
  615. }).Error("exit event")
  616. return
  617. }
  618. ctr, err := c.getContainer(ctx, ei.ContainerID)
  619. if err != nil {
  620. c.logger.WithFields(logrus.Fields{
  621. "container": ei.ContainerID,
  622. "error": err,
  623. }).Error("failed to find container")
  624. } else {
  625. labels, err := ctr.Labels(ctx)
  626. if err != nil {
  627. c.logger.WithFields(logrus.Fields{
  628. "container": ei.ContainerID,
  629. "error": err,
  630. }).Error("failed to get container labels")
  631. return
  632. }
  633. newFIFOSet(labels[DockerContainerBundlePath], ei.ProcessID, true, false).Close()
  634. }
  635. _, err = p.Delete(context.Background())
  636. if err != nil {
  637. c.logger.WithError(err).WithFields(logrus.Fields{
  638. "container": ei.ContainerID,
  639. "process": ei.ProcessID,
  640. }).Warn("failed to delete process")
  641. }
  642. }
  643. })
  644. }
  645. func (c *client) waitServe(ctx context.Context) bool {
  646. t := 100 * time.Millisecond
  647. delay := time.NewTimer(t)
  648. if !delay.Stop() {
  649. <-delay.C
  650. }
  651. defer delay.Stop()
  652. // `IsServing` will actually block until the service is ready.
  653. // However it can return early, so we'll loop with a delay to handle it.
  654. for {
  655. serving, err := c.client.IsServing(ctx)
  656. if err != nil {
  657. if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
  658. return false
  659. }
  660. logrus.WithError(err).Warn("Error while testing if containerd API is ready")
  661. }
  662. if serving {
  663. return true
  664. }
  665. delay.Reset(t)
  666. select {
  667. case <-ctx.Done():
  668. return false
  669. case <-delay.C:
  670. }
  671. }
  672. }
  673. func (c *client) processEventStream(ctx context.Context, ns string) {
  674. var (
  675. err error
  676. ev *events.Envelope
  677. et libcontainerdtypes.EventType
  678. ei libcontainerdtypes.EventInfo
  679. )
  680. // Create a new context specifically for this subscription.
  681. // The context must be cancelled to cancel the subscription.
  682. // In cases where we have to restart event stream processing,
  683. // we'll need the original context b/c this one will be cancelled
  684. subCtx, cancel := context.WithCancel(ctx)
  685. defer cancel()
  686. // Filter on both namespace *and* topic. To create an "and" filter,
  687. // this must be a single, comma-separated string
  688. eventStream, errC := c.client.EventService().Subscribe(subCtx, "namespace=="+ns+",topic~=|^/tasks/|")
  689. c.logger.Debug("processing event stream")
  690. for {
  691. var oomKilled bool
  692. select {
  693. case err = <-errC:
  694. if err != nil {
  695. errStatus, ok := status.FromError(err)
  696. if !ok || errStatus.Code() != codes.Canceled {
  697. c.logger.WithError(err).Error("Failed to get event")
  698. c.logger.Info("Waiting for containerd to be ready to restart event processing")
  699. if c.waitServe(ctx) {
  700. go c.processEventStream(ctx, ns)
  701. return
  702. }
  703. }
  704. c.logger.WithError(ctx.Err()).Info("stopping event stream following graceful shutdown")
  705. }
  706. return
  707. case ev = <-eventStream:
  708. if ev.Event == nil {
  709. c.logger.WithField("event", ev).Warn("invalid event")
  710. continue
  711. }
  712. v, err := typeurl.UnmarshalAny(ev.Event)
  713. if err != nil {
  714. c.logger.WithError(err).WithField("event", ev).Warn("failed to unmarshal event")
  715. continue
  716. }
  717. c.logger.WithField("topic", ev.Topic).Debug("event")
  718. switch t := v.(type) {
  719. case *apievents.TaskCreate:
  720. et = libcontainerdtypes.EventCreate
  721. ei = libcontainerdtypes.EventInfo{
  722. ContainerID: t.ContainerID,
  723. ProcessID: t.ContainerID,
  724. Pid: t.Pid,
  725. }
  726. case *apievents.TaskStart:
  727. et = libcontainerdtypes.EventStart
  728. ei = libcontainerdtypes.EventInfo{
  729. ContainerID: t.ContainerID,
  730. ProcessID: t.ContainerID,
  731. Pid: t.Pid,
  732. }
  733. case *apievents.TaskExit:
  734. et = libcontainerdtypes.EventExit
  735. ei = libcontainerdtypes.EventInfo{
  736. ContainerID: t.ContainerID,
  737. ProcessID: t.ID,
  738. Pid: t.Pid,
  739. ExitCode: t.ExitStatus,
  740. ExitedAt: t.ExitedAt,
  741. }
  742. case *apievents.TaskOOM:
  743. et = libcontainerdtypes.EventOOM
  744. ei = libcontainerdtypes.EventInfo{
  745. ContainerID: t.ContainerID,
  746. OOMKilled: true,
  747. }
  748. oomKilled = true
  749. case *apievents.TaskExecAdded:
  750. et = libcontainerdtypes.EventExecAdded
  751. ei = libcontainerdtypes.EventInfo{
  752. ContainerID: t.ContainerID,
  753. ProcessID: t.ExecID,
  754. }
  755. case *apievents.TaskExecStarted:
  756. et = libcontainerdtypes.EventExecStarted
  757. ei = libcontainerdtypes.EventInfo{
  758. ContainerID: t.ContainerID,
  759. ProcessID: t.ExecID,
  760. Pid: t.Pid,
  761. }
  762. case *apievents.TaskPaused:
  763. et = libcontainerdtypes.EventPaused
  764. ei = libcontainerdtypes.EventInfo{
  765. ContainerID: t.ContainerID,
  766. }
  767. case *apievents.TaskResumed:
  768. et = libcontainerdtypes.EventResumed
  769. ei = libcontainerdtypes.EventInfo{
  770. ContainerID: t.ContainerID,
  771. }
  772. case *apievents.TaskDelete:
  773. c.logger.WithFields(logrus.Fields{
  774. "topic": ev.Topic,
  775. "type": reflect.TypeOf(t),
  776. "container": t.ContainerID},
  777. ).Info("ignoring event")
  778. continue
  779. default:
  780. c.logger.WithFields(logrus.Fields{
  781. "topic": ev.Topic,
  782. "type": reflect.TypeOf(t)},
  783. ).Info("ignoring event")
  784. continue
  785. }
  786. c.oomMu.Lock()
  787. if oomKilled {
  788. c.oom[ei.ContainerID] = true
  789. }
  790. ei.OOMKilled = c.oom[ei.ContainerID]
  791. c.oomMu.Unlock()
  792. c.processEvent(ctx, et, ei)
  793. }
  794. }
  795. }
  796. func (c *client) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
  797. writer, err := c.client.ContentStore().Writer(ctx, content.WithRef(ref))
  798. if err != nil {
  799. return nil, err
  800. }
  801. defer writer.Close()
  802. size, err := io.Copy(writer, r)
  803. if err != nil {
  804. return nil, err
  805. }
  806. labels := map[string]string{
  807. "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339),
  808. }
  809. if err := writer.Commit(ctx, 0, "", content.WithLabels(labels)); err != nil {
  810. return nil, err
  811. }
  812. return &types.Descriptor{
  813. MediaType: mediaType,
  814. Digest: writer.Digest(),
  815. Size_: size,
  816. }, nil
  817. }
  818. func (c *client) bundleDir(id string) string {
  819. return filepath.Join(c.stateDir, id)
  820. }
  821. func wrapError(err error) error {
  822. switch {
  823. case err == nil:
  824. return nil
  825. case containerderrors.IsNotFound(err):
  826. return errdefs.NotFound(err)
  827. }
  828. msg := err.Error()
  829. for _, s := range []string{"container does not exist", "not found", "no such container"} {
  830. if strings.Contains(msg, s) {
  831. return errdefs.NotFound(err)
  832. }
  833. }
  834. return err
  835. }