adapter.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. package container // import "github.com/docker/docker/daemon/cluster/executor/container"
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "os"
  9. "strings"
  10. "syscall"
  11. "time"
  12. "github.com/containerd/log"
  13. "github.com/distribution/reference"
  14. "github.com/docker/docker/api/types"
  15. "github.com/docker/docker/api/types/backend"
  16. containertypes "github.com/docker/docker/api/types/container"
  17. "github.com/docker/docker/api/types/events"
  18. imagetypes "github.com/docker/docker/api/types/image"
  19. "github.com/docker/docker/api/types/registry"
  20. containerpkg "github.com/docker/docker/container"
  21. "github.com/docker/docker/daemon"
  22. "github.com/docker/docker/daemon/cluster/convert"
  23. executorpkg "github.com/docker/docker/daemon/cluster/executor"
  24. "github.com/docker/docker/libnetwork"
  25. volumeopts "github.com/docker/docker/volume/service/opts"
  26. gogotypes "github.com/gogo/protobuf/types"
  27. "github.com/moby/swarmkit/v2/agent/exec"
  28. "github.com/moby/swarmkit/v2/api"
  29. swarmlog "github.com/moby/swarmkit/v2/log"
  30. "github.com/opencontainers/go-digest"
  31. "github.com/pkg/errors"
  32. "golang.org/x/time/rate"
  33. )
  34. // nodeAttachmentReadyInterval is the interval to poll
  35. const nodeAttachmentReadyInterval = 100 * time.Millisecond
  36. // containerAdapter conducts remote operations for a container. All calls
  37. // are mostly naked calls to the client API, seeded with information from
  38. // containerConfig.
  39. type containerAdapter struct {
  40. backend executorpkg.Backend
  41. imageBackend executorpkg.ImageBackend
  42. volumeBackend executorpkg.VolumeBackend
  43. container *containerConfig
  44. dependencies exec.DependencyGetter
  45. }
  46. func newContainerAdapter(b executorpkg.Backend, i executorpkg.ImageBackend, v executorpkg.VolumeBackend, task *api.Task, node *api.NodeDescription, dependencies exec.DependencyGetter) (*containerAdapter, error) {
  47. ctnr, err := newContainerConfig(task, node)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return &containerAdapter{
  52. container: ctnr,
  53. backend: b,
  54. imageBackend: i,
  55. volumeBackend: v,
  56. dependencies: dependencies,
  57. }, nil
  58. }
  59. func (c *containerAdapter) pullImage(ctx context.Context) error {
  60. spec := c.container.spec()
  61. // Skip pulling if the image is referenced by image ID.
  62. if _, err := digest.Parse(spec.Image); err == nil {
  63. return nil
  64. }
  65. // Skip pulling if the image is referenced by digest and already
  66. // exists locally.
  67. named, err := reference.ParseNormalizedNamed(spec.Image)
  68. if err == nil {
  69. if _, ok := named.(reference.Canonical); ok {
  70. _, err := c.imageBackend.GetImage(ctx, spec.Image, imagetypes.GetImageOpts{})
  71. if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
  72. return err
  73. }
  74. if err == nil {
  75. return nil
  76. }
  77. }
  78. }
  79. // if the image needs to be pulled, the auth config will be retrieved and updated
  80. var encodedAuthConfig string
  81. if spec.PullOptions != nil {
  82. encodedAuthConfig = spec.PullOptions.RegistryAuth
  83. }
  84. authConfig := &registry.AuthConfig{}
  85. if encodedAuthConfig != "" {
  86. if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuthConfig))).Decode(authConfig); err != nil {
  87. swarmlog.G(ctx).Warnf("invalid authconfig: %v", err)
  88. }
  89. }
  90. pr, pw := io.Pipe()
  91. metaHeaders := map[string][]string{}
  92. go func() {
  93. // TODO LCOW Support: This will need revisiting as
  94. // the stack is built up to include LCOW support for swarm.
  95. // Make sure the image has a tag, otherwise it will pull all tags.
  96. ref := reference.TagNameOnly(named)
  97. err := c.imageBackend.PullImage(ctx, ref, nil, metaHeaders, authConfig, pw)
  98. pw.CloseWithError(err)
  99. }()
  100. dec := json.NewDecoder(pr)
  101. dec.UseNumber()
  102. m := map[string]interface{}{}
  103. spamLimiter := rate.NewLimiter(rate.Every(time.Second), 1)
  104. lastStatus := ""
  105. for {
  106. if err := dec.Decode(&m); err != nil {
  107. if err == io.EOF {
  108. break
  109. }
  110. return err
  111. }
  112. l := swarmlog.G(ctx)
  113. // limit pull progress logs unless the status changes
  114. if spamLimiter.Allow() || lastStatus != m["status"] {
  115. // if we have progress details, we have everything we need
  116. if progress, ok := m["progressDetail"].(map[string]interface{}); ok {
  117. // first, log the image and status
  118. l = l.WithFields(log.Fields{
  119. "image": c.container.image(),
  120. "status": m["status"],
  121. })
  122. // then, if we have progress, log the progress
  123. if progress["current"] != nil && progress["total"] != nil {
  124. l = l.WithFields(log.Fields{
  125. "current": progress["current"],
  126. "total": progress["total"],
  127. })
  128. }
  129. }
  130. l.Debug("pull in progress")
  131. }
  132. // sometimes, we get no useful information at all, and add no fields
  133. if status, ok := m["status"].(string); ok {
  134. lastStatus = status
  135. }
  136. }
  137. // if the final stream object contained an error, return it
  138. if errMsg, ok := m["error"]; ok {
  139. return fmt.Errorf("%v", errMsg)
  140. }
  141. return nil
  142. }
  143. // waitNodeAttachments validates that NetworkAttachments exist on this node
  144. // for every network in use by this task. It blocks until the network
  145. // attachments are ready, or the context times out. If it returns nil, then the
  146. // node's network attachments are all there.
  147. func (c *containerAdapter) waitNodeAttachments(ctx context.Context) error {
  148. // to do this, we're going to get the attachment store and try getting the
  149. // IP address for each network. if any network comes back not existing,
  150. // we'll wait and try again.
  151. attachmentStore := c.backend.GetAttachmentStore()
  152. if attachmentStore == nil {
  153. return fmt.Errorf("error getting attachment store")
  154. }
  155. // essentially, we're long-polling here. this is really sub-optimal, but a
  156. // better solution based off signaling channels would require a more
  157. // substantial rearchitecture and probably not be worth our time in terms
  158. // of performance gains.
  159. poll := time.NewTicker(nodeAttachmentReadyInterval)
  160. defer poll.Stop()
  161. for {
  162. // set a flag ready to true. if we try to get a network IP that doesn't
  163. // exist yet, we will set this flag to "false"
  164. ready := true
  165. for _, attachment := range c.container.networksAttachments {
  166. // we only need node attachments (IP address) for overlay networks
  167. // TODO(dperny): unsure if this will work with other network
  168. // drivers, but i also don't think other network drivers use the
  169. // node attachment IP address.
  170. if attachment.Network.DriverState.Name == "overlay" {
  171. if _, exists := attachmentStore.GetIPForNetwork(attachment.Network.ID); !exists {
  172. ready = false
  173. }
  174. }
  175. }
  176. // if everything is ready here, then we can just return no error
  177. if ready {
  178. return nil
  179. }
  180. // otherwise, try polling again, or wait for context canceled.
  181. select {
  182. case <-ctx.Done():
  183. return fmt.Errorf("node is missing network attachments, ip addresses may be exhausted")
  184. case <-poll.C:
  185. }
  186. }
  187. }
  188. func (c *containerAdapter) createNetworks(ctx context.Context) error {
  189. for name := range c.container.networksAttachments {
  190. ncr, err := c.container.networkCreateRequest(name)
  191. if err != nil {
  192. return err
  193. }
  194. if err := c.backend.CreateManagedNetwork(ncr); err != nil { // todo name missing
  195. if _, ok := err.(libnetwork.NetworkNameError); ok {
  196. continue
  197. }
  198. // We will continue if CreateManagedNetwork returns PredefinedNetworkError error.
  199. // Other callers still can treat it as Error.
  200. if _, ok := err.(daemon.PredefinedNetworkError); ok {
  201. continue
  202. }
  203. return err
  204. }
  205. }
  206. return nil
  207. }
  208. func (c *containerAdapter) removeNetworks(ctx context.Context) error {
  209. var (
  210. activeEndpointsError *libnetwork.ActiveEndpointsError
  211. errNoSuchNetwork libnetwork.ErrNoSuchNetwork
  212. )
  213. for name, v := range c.container.networksAttachments {
  214. if err := c.backend.DeleteManagedNetwork(v.Network.ID); err != nil {
  215. switch {
  216. case errors.As(err, &activeEndpointsError):
  217. continue
  218. case errors.As(err, &errNoSuchNetwork):
  219. continue
  220. default:
  221. swarmlog.G(ctx).Errorf("network %s remove failed: %v", name, err)
  222. return err
  223. }
  224. }
  225. }
  226. return nil
  227. }
  228. func (c *containerAdapter) networkAttach(ctx context.Context) error {
  229. config := c.container.createNetworkingConfig(c.backend)
  230. var (
  231. networkName string
  232. networkID string
  233. )
  234. if config != nil {
  235. for n, epConfig := range config.EndpointsConfig {
  236. networkName = n
  237. networkID = epConfig.NetworkID
  238. break
  239. }
  240. }
  241. return c.backend.UpdateAttachment(networkName, networkID, c.container.networkAttachmentContainerID(), config)
  242. }
  243. func (c *containerAdapter) waitForDetach(ctx context.Context) error {
  244. config := c.container.createNetworkingConfig(c.backend)
  245. var (
  246. networkName string
  247. networkID string
  248. )
  249. if config != nil {
  250. for n, epConfig := range config.EndpointsConfig {
  251. networkName = n
  252. networkID = epConfig.NetworkID
  253. break
  254. }
  255. }
  256. return c.backend.WaitForDetachment(ctx, networkName, networkID, c.container.taskID(), c.container.networkAttachmentContainerID())
  257. }
  258. func (c *containerAdapter) create(ctx context.Context) error {
  259. var cr containertypes.CreateResponse
  260. var err error
  261. if cr, err = c.backend.CreateManagedContainer(ctx, backend.ContainerCreateConfig{
  262. Name: c.container.name(),
  263. Config: c.container.config(),
  264. HostConfig: c.container.hostConfig(c.dependencies.Volumes()),
  265. // Use the first network in container create
  266. NetworkingConfig: c.container.createNetworkingConfig(c.backend),
  267. }); err != nil {
  268. return err
  269. }
  270. container := c.container.task.Spec.GetContainer()
  271. if container == nil {
  272. return errors.New("unable to get container from task spec")
  273. }
  274. if err := c.backend.SetContainerDependencyStore(cr.ID, c.dependencies); err != nil {
  275. return err
  276. }
  277. // configure secrets
  278. secretRefs := convert.SecretReferencesFromGRPC(container.Secrets)
  279. if err := c.backend.SetContainerSecretReferences(cr.ID, secretRefs); err != nil {
  280. return err
  281. }
  282. configRefs := convert.ConfigReferencesFromGRPC(container.Configs)
  283. if err := c.backend.SetContainerConfigReferences(cr.ID, configRefs); err != nil {
  284. return err
  285. }
  286. return c.backend.UpdateContainerServiceConfig(cr.ID, c.container.serviceConfig())
  287. }
  288. // checkMounts ensures that the provided mounts won't have any host-specific
  289. // problems at start up. For example, we disallow bind mounts without an
  290. // existing path, which slightly different from the container API.
  291. func (c *containerAdapter) checkMounts() error {
  292. spec := c.container.spec()
  293. for _, mount := range spec.Mounts {
  294. switch mount.Type {
  295. case api.MountTypeBind:
  296. if _, err := os.Stat(mount.Source); os.IsNotExist(err) {
  297. return fmt.Errorf("invalid bind mount source, source path not found: %s", mount.Source)
  298. }
  299. }
  300. }
  301. return nil
  302. }
  303. func (c *containerAdapter) start(ctx context.Context) error {
  304. if err := c.checkMounts(); err != nil {
  305. return err
  306. }
  307. return c.backend.ContainerStart(ctx, c.container.name(), nil, "", "")
  308. }
  309. func (c *containerAdapter) inspect(ctx context.Context) (types.ContainerJSON, error) {
  310. cs, err := c.backend.ContainerInspectCurrent(ctx, c.container.name(), false)
  311. if ctx.Err() != nil {
  312. return types.ContainerJSON{}, ctx.Err()
  313. }
  314. if err != nil {
  315. return types.ContainerJSON{}, err
  316. }
  317. return *cs, nil
  318. }
  319. // events issues a call to the events API and returns a channel with all
  320. // events. The stream of events can be shutdown by cancelling the context.
  321. func (c *containerAdapter) events(ctx context.Context) <-chan events.Message {
  322. swarmlog.G(ctx).Debugf("waiting on events")
  323. buffer, l := c.backend.SubscribeToEvents(time.Time{}, time.Time{}, c.container.eventFilter())
  324. eventsq := make(chan events.Message, len(buffer))
  325. for _, event := range buffer {
  326. eventsq <- event
  327. }
  328. go func() {
  329. defer c.backend.UnsubscribeFromEvents(l)
  330. for {
  331. select {
  332. case ev := <-l:
  333. jev, ok := ev.(events.Message)
  334. if !ok {
  335. swarmlog.G(ctx).Warnf("unexpected event message: %q", ev)
  336. continue
  337. }
  338. select {
  339. case eventsq <- jev:
  340. case <-ctx.Done():
  341. return
  342. }
  343. case <-ctx.Done():
  344. return
  345. }
  346. }
  347. }()
  348. return eventsq
  349. }
  350. func (c *containerAdapter) wait(ctx context.Context) (<-chan containerpkg.StateStatus, error) {
  351. return c.backend.ContainerWait(ctx, c.container.nameOrID(), containerpkg.WaitConditionNotRunning)
  352. }
  353. func (c *containerAdapter) shutdown(ctx context.Context) error {
  354. options := containertypes.StopOptions{}
  355. // Default stop grace period to nil (daemon will use the stopTimeout of the container)
  356. if spec := c.container.spec(); spec.StopGracePeriod != nil {
  357. timeout := int(spec.StopGracePeriod.Seconds)
  358. options.Timeout = &timeout
  359. }
  360. return c.backend.ContainerStop(ctx, c.container.name(), options)
  361. }
  362. func (c *containerAdapter) terminate(ctx context.Context) error {
  363. return c.backend.ContainerKill(c.container.name(), syscall.SIGKILL.String())
  364. }
  365. func (c *containerAdapter) remove(ctx context.Context) error {
  366. return c.backend.ContainerRm(c.container.name(), &backend.ContainerRmConfig{
  367. RemoveVolume: true,
  368. ForceRemove: true,
  369. })
  370. }
  371. func (c *containerAdapter) createVolumes(ctx context.Context) error {
  372. // Create plugin volumes that are embedded inside a Mount
  373. for _, mount := range c.container.task.Spec.GetContainer().Mounts {
  374. mount := mount
  375. if mount.Type != api.MountTypeVolume {
  376. continue
  377. }
  378. if mount.VolumeOptions == nil {
  379. continue
  380. }
  381. if mount.VolumeOptions.DriverConfig == nil {
  382. continue
  383. }
  384. req := c.container.volumeCreateRequest(&mount)
  385. // Check if this volume exists on the engine
  386. if _, err := c.volumeBackend.Create(ctx, req.Name, req.Driver,
  387. volumeopts.WithCreateOptions(req.DriverOpts),
  388. volumeopts.WithCreateLabels(req.Labels),
  389. ); err != nil {
  390. // TODO(amitshukla): Today, volume create through the engine api does not return an error
  391. // when the named volume with the same parameters already exists.
  392. // It returns an error if the driver name is different - that is a valid error
  393. return err
  394. }
  395. }
  396. return nil
  397. }
  398. // waitClusterVolumes blocks until the VolumeGetter returns a path for each
  399. // cluster volume in use by this task
  400. func (c *containerAdapter) waitClusterVolumes(ctx context.Context) error {
  401. for _, attached := range c.container.task.Volumes {
  402. // for every attachment, try until we succeed or until the context
  403. // is canceled.
  404. for {
  405. select {
  406. case <-ctx.Done():
  407. return ctx.Err()
  408. default:
  409. // continue through the code.
  410. }
  411. path, err := c.dependencies.Volumes().Get(attached.ID)
  412. if err == nil && path != "" {
  413. // break out of the inner-most loop
  414. break
  415. }
  416. }
  417. }
  418. swarmlog.G(ctx).Debug("volumes ready")
  419. return nil
  420. }
  421. func (c *containerAdapter) activateServiceBinding() error {
  422. return c.backend.ActivateContainerServiceBinding(c.container.name())
  423. }
  424. func (c *containerAdapter) deactivateServiceBinding() error {
  425. return c.backend.DeactivateContainerServiceBinding(c.container.name())
  426. }
  427. func (c *containerAdapter) logs(ctx context.Context, options api.LogSubscriptionOptions) (<-chan *backend.LogMessage, error) {
  428. apiOptions := &containertypes.LogsOptions{
  429. Follow: options.Follow,
  430. // Always say yes to Timestamps and Details. we make the decision
  431. // of whether to return these to the user or not way higher up the
  432. // stack.
  433. Timestamps: true,
  434. Details: true,
  435. }
  436. if options.Since != nil {
  437. since, err := gogotypes.TimestampFromProto(options.Since)
  438. if err != nil {
  439. return nil, err
  440. }
  441. // print since as this formatted string because the docker container
  442. // logs interface expects it like this.
  443. // see github.com/docker/docker/api/types/time.ParseTimestamps
  444. apiOptions.Since = fmt.Sprintf("%d.%09d", since.Unix(), int64(since.Nanosecond()))
  445. }
  446. if options.Tail < 0 {
  447. // See protobuf documentation for details of how this works.
  448. apiOptions.Tail = fmt.Sprint(-options.Tail - 1)
  449. } else if options.Tail > 0 {
  450. return nil, errors.New("tail relative to start of logs not supported via docker API")
  451. }
  452. if len(options.Streams) == 0 {
  453. // empty == all
  454. apiOptions.ShowStdout, apiOptions.ShowStderr = true, true
  455. } else {
  456. for _, stream := range options.Streams {
  457. switch stream {
  458. case api.LogStreamStdout:
  459. apiOptions.ShowStdout = true
  460. case api.LogStreamStderr:
  461. apiOptions.ShowStderr = true
  462. }
  463. }
  464. }
  465. msgs, _, err := c.backend.ContainerLogs(ctx, c.container.name(), apiOptions)
  466. if err != nil {
  467. return nil, err
  468. }
  469. return msgs, nil
  470. }
  471. // todo: typed/wrapped errors
  472. func isContainerCreateNameConflict(err error) bool {
  473. return strings.Contains(err.Error(), "Conflict. The name")
  474. }
  475. func isUnknownContainer(err error) bool {
  476. return strings.Contains(err.Error(), "No such container:")
  477. }
  478. func isStoppedContainer(err error) bool {
  479. return strings.Contains(err.Error(), "is already stopped")
  480. }