adapter.go 15 KB

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