client.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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) CreateCheckpoint(ctx context.Context, containerID, checkpointDir string, exit bool) error {
  452. p, err := c.getProcess(ctx, containerID, libcontainerdtypes.InitProcessName)
  453. if err != nil {
  454. return err
  455. }
  456. opts := []containerd.CheckpointTaskOpts{}
  457. if exit {
  458. opts = append(opts, func(r *containerd.CheckpointTaskInfo) error {
  459. if r.Options == nil {
  460. r.Options = &runctypes.CheckpointOptions{
  461. Exit: true,
  462. }
  463. } else {
  464. opts, _ := r.Options.(*runctypes.CheckpointOptions)
  465. opts.Exit = true
  466. }
  467. return nil
  468. })
  469. }
  470. img, err := p.(containerd.Task).Checkpoint(ctx, opts...)
  471. if err != nil {
  472. return wrapError(err)
  473. }
  474. // Whatever happens, delete the checkpoint from containerd
  475. defer func() {
  476. err := c.client.ImageService().Delete(context.Background(), img.Name())
  477. if err != nil {
  478. c.logger.WithError(err).WithField("digest", img.Target().Digest).
  479. Warnf("failed to delete checkpoint image")
  480. }
  481. }()
  482. b, err := content.ReadBlob(ctx, c.client.ContentStore(), img.Target())
  483. if err != nil {
  484. return errdefs.System(errors.Wrapf(err, "failed to retrieve checkpoint data"))
  485. }
  486. var index v1.Index
  487. if err := json.Unmarshal(b, &index); err != nil {
  488. return errdefs.System(errors.Wrapf(err, "failed to decode checkpoint data"))
  489. }
  490. var cpDesc *v1.Descriptor
  491. for _, m := range index.Manifests {
  492. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  493. cpDesc = &m
  494. break
  495. }
  496. }
  497. if cpDesc == nil {
  498. return errdefs.System(errors.Wrapf(err, "invalid checkpoint"))
  499. }
  500. rat, err := c.client.ContentStore().ReaderAt(ctx, *cpDesc)
  501. if err != nil {
  502. return errdefs.System(errors.Wrapf(err, "failed to get checkpoint reader"))
  503. }
  504. defer rat.Close()
  505. _, err = archive.Apply(ctx, checkpointDir, content.NewReader(rat))
  506. if err != nil {
  507. return errdefs.System(errors.Wrapf(err, "failed to read checkpoint reader"))
  508. }
  509. return err
  510. }
  511. func (c *client) getContainer(ctx context.Context, id string) (containerd.Container, error) {
  512. ctr, err := c.client.LoadContainer(ctx, id)
  513. if err != nil {
  514. if containerderrors.IsNotFound(err) {
  515. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  516. }
  517. return nil, wrapError(err)
  518. }
  519. return ctr, nil
  520. }
  521. func (c *client) getProcess(ctx context.Context, containerID, processID string) (containerd.Process, error) {
  522. ctr, err := c.getContainer(ctx, containerID)
  523. if err != nil {
  524. return nil, err
  525. }
  526. t, err := ctr.Task(ctx, nil)
  527. if err != nil {
  528. if containerderrors.IsNotFound(err) {
  529. return nil, errors.WithStack(errdefs.NotFound(errors.New("container is not running")))
  530. }
  531. return nil, wrapError(err)
  532. }
  533. if processID == libcontainerdtypes.InitProcessName {
  534. return t, nil
  535. }
  536. p, err := t.LoadProcess(ctx, processID, nil)
  537. if err != nil {
  538. if containerderrors.IsNotFound(err) {
  539. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such exec")))
  540. }
  541. return nil, wrapError(err)
  542. }
  543. return p, nil
  544. }
  545. // createIO creates the io to be used by a process
  546. // This needs to get a pointer to interface as upon closure the process may not have yet been registered
  547. func (c *client) createIO(fifos *cio.FIFOSet, containerID, processID string, stdinCloseSync chan struct{}, attachStdio libcontainerdtypes.StdioCallback) (cio.IO, error) {
  548. var (
  549. io *cio.DirectIO
  550. err error
  551. )
  552. io, err = c.newDirectIO(context.Background(), fifos)
  553. if err != nil {
  554. return nil, err
  555. }
  556. if io.Stdin != nil {
  557. var (
  558. err error
  559. stdinOnce sync.Once
  560. )
  561. pipe := io.Stdin
  562. io.Stdin = ioutils.NewWriteCloserWrapper(pipe, func() error {
  563. stdinOnce.Do(func() {
  564. err = pipe.Close()
  565. // Do the rest in a new routine to avoid a deadlock if the
  566. // Exec/Start call failed.
  567. go func() {
  568. <-stdinCloseSync
  569. p, err := c.getProcess(context.Background(), containerID, processID)
  570. if err == nil {
  571. err = p.CloseIO(context.Background(), containerd.WithStdinCloser)
  572. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  573. err = nil
  574. }
  575. }
  576. }()
  577. })
  578. return err
  579. })
  580. }
  581. rio, err := attachStdio(io)
  582. if err != nil {
  583. io.Cancel()
  584. io.Close()
  585. }
  586. return rio, err
  587. }
  588. func (c *client) processEvent(ctx context.Context, et libcontainerdtypes.EventType, ei libcontainerdtypes.EventInfo) {
  589. c.eventQ.Append(ei.ContainerID, func() {
  590. err := c.backend.ProcessEvent(ei.ContainerID, et, ei)
  591. if err != nil {
  592. c.logger.WithError(err).WithFields(logrus.Fields{
  593. "container": ei.ContainerID,
  594. "event": et,
  595. "event-info": ei,
  596. }).Error("failed to process event")
  597. }
  598. if et == libcontainerdtypes.EventExit && ei.ProcessID != ei.ContainerID {
  599. p, err := c.getProcess(ctx, ei.ContainerID, ei.ProcessID)
  600. if err != nil {
  601. c.logger.WithError(errors.New("no such process")).
  602. WithFields(logrus.Fields{
  603. "error": err,
  604. "container": ei.ContainerID,
  605. "process": ei.ProcessID,
  606. }).Error("exit event")
  607. return
  608. }
  609. ctr, err := c.getContainer(ctx, ei.ContainerID)
  610. if err != nil {
  611. c.logger.WithFields(logrus.Fields{
  612. "container": ei.ContainerID,
  613. "error": err,
  614. }).Error("failed to find container")
  615. } else {
  616. labels, err := ctr.Labels(ctx)
  617. if err != nil {
  618. c.logger.WithFields(logrus.Fields{
  619. "container": ei.ContainerID,
  620. "error": err,
  621. }).Error("failed to get container labels")
  622. return
  623. }
  624. newFIFOSet(labels[DockerContainerBundlePath], ei.ProcessID, true, false).Close()
  625. }
  626. _, err = p.Delete(context.Background())
  627. if err != nil {
  628. c.logger.WithError(err).WithFields(logrus.Fields{
  629. "container": ei.ContainerID,
  630. "process": ei.ProcessID,
  631. }).Warn("failed to delete process")
  632. }
  633. }
  634. })
  635. }
  636. func (c *client) processEventStream(ctx context.Context, ns string) {
  637. var (
  638. err error
  639. ev *events.Envelope
  640. et libcontainerdtypes.EventType
  641. ei libcontainerdtypes.EventInfo
  642. )
  643. // Filter on both namespace *and* topic. To create an "and" filter,
  644. // this must be a single, comma-separated string
  645. eventStream, errC := c.client.EventService().Subscribe(ctx, "namespace=="+ns+",topic~=|^/tasks/|")
  646. c.logger.Debug("processing event stream")
  647. for {
  648. var oomKilled bool
  649. select {
  650. case err = <-errC:
  651. if err != nil {
  652. errStatus, ok := status.FromError(err)
  653. if !ok || errStatus.Code() != codes.Canceled {
  654. c.logger.WithError(err).Error("failed to get event")
  655. // rate limit
  656. select {
  657. case <-time.After(time.Second):
  658. go c.processEventStream(ctx, ns)
  659. return
  660. case <-ctx.Done():
  661. }
  662. }
  663. c.logger.WithError(ctx.Err()).Info("stopping event stream following graceful shutdown")
  664. }
  665. return
  666. case ev = <-eventStream:
  667. if ev.Event == nil {
  668. c.logger.WithField("event", ev).Warn("invalid event")
  669. continue
  670. }
  671. v, err := typeurl.UnmarshalAny(ev.Event)
  672. if err != nil {
  673. c.logger.WithError(err).WithField("event", ev).Warn("failed to unmarshal event")
  674. continue
  675. }
  676. c.logger.WithField("topic", ev.Topic).Debug("event")
  677. switch t := v.(type) {
  678. case *apievents.TaskCreate:
  679. et = libcontainerdtypes.EventCreate
  680. ei = libcontainerdtypes.EventInfo{
  681. ContainerID: t.ContainerID,
  682. ProcessID: t.ContainerID,
  683. Pid: t.Pid,
  684. }
  685. case *apievents.TaskStart:
  686. et = libcontainerdtypes.EventStart
  687. ei = libcontainerdtypes.EventInfo{
  688. ContainerID: t.ContainerID,
  689. ProcessID: t.ContainerID,
  690. Pid: t.Pid,
  691. }
  692. case *apievents.TaskExit:
  693. et = libcontainerdtypes.EventExit
  694. ei = libcontainerdtypes.EventInfo{
  695. ContainerID: t.ContainerID,
  696. ProcessID: t.ID,
  697. Pid: t.Pid,
  698. ExitCode: t.ExitStatus,
  699. ExitedAt: t.ExitedAt,
  700. }
  701. case *apievents.TaskOOM:
  702. et = libcontainerdtypes.EventOOM
  703. ei = libcontainerdtypes.EventInfo{
  704. ContainerID: t.ContainerID,
  705. OOMKilled: true,
  706. }
  707. oomKilled = true
  708. case *apievents.TaskExecAdded:
  709. et = libcontainerdtypes.EventExecAdded
  710. ei = libcontainerdtypes.EventInfo{
  711. ContainerID: t.ContainerID,
  712. ProcessID: t.ExecID,
  713. }
  714. case *apievents.TaskExecStarted:
  715. et = libcontainerdtypes.EventExecStarted
  716. ei = libcontainerdtypes.EventInfo{
  717. ContainerID: t.ContainerID,
  718. ProcessID: t.ExecID,
  719. Pid: t.Pid,
  720. }
  721. case *apievents.TaskPaused:
  722. et = libcontainerdtypes.EventPaused
  723. ei = libcontainerdtypes.EventInfo{
  724. ContainerID: t.ContainerID,
  725. }
  726. case *apievents.TaskResumed:
  727. et = libcontainerdtypes.EventResumed
  728. ei = libcontainerdtypes.EventInfo{
  729. ContainerID: t.ContainerID,
  730. }
  731. default:
  732. c.logger.WithFields(logrus.Fields{
  733. "topic": ev.Topic,
  734. "type": reflect.TypeOf(t)},
  735. ).Info("ignoring event")
  736. continue
  737. }
  738. c.oomMu.Lock()
  739. if oomKilled {
  740. c.oom[ei.ContainerID] = true
  741. }
  742. ei.OOMKilled = c.oom[ei.ContainerID]
  743. c.oomMu.Unlock()
  744. c.processEvent(ctx, et, ei)
  745. }
  746. }
  747. }
  748. func (c *client) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
  749. writer, err := c.client.ContentStore().Writer(ctx, content.WithRef(ref))
  750. if err != nil {
  751. return nil, err
  752. }
  753. defer writer.Close()
  754. size, err := io.Copy(writer, r)
  755. if err != nil {
  756. return nil, err
  757. }
  758. labels := map[string]string{
  759. "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339),
  760. }
  761. if err := writer.Commit(ctx, 0, "", content.WithLabels(labels)); err != nil {
  762. return nil, err
  763. }
  764. return &types.Descriptor{
  765. MediaType: mediaType,
  766. Digest: writer.Digest(),
  767. Size_: size,
  768. }, nil
  769. }
  770. func (c *client) bundleDir(id string) string {
  771. return filepath.Join(c.stateDir, id)
  772. }
  773. func wrapError(err error) error {
  774. switch {
  775. case err == nil:
  776. return nil
  777. case containerderrors.IsNotFound(err):
  778. return errdefs.NotFound(err)
  779. }
  780. msg := err.Error()
  781. for _, s := range []string{"container does not exist", "not found", "no such container"} {
  782. if strings.Contains(msg, s) {
  783. return errdefs.NotFound(err)
  784. }
  785. }
  786. return err
  787. }