convergence.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "time"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/containerd/containerd/platforms"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/api/types/filters"
  25. "github.com/docker/docker/api/types/network"
  26. specs "github.com/opencontainers/image-spec/specs-go/v1"
  27. "github.com/sirupsen/logrus"
  28. "golang.org/x/sync/errgroup"
  29. "github.com/docker/compose/v2/pkg/api"
  30. "github.com/docker/compose/v2/pkg/progress"
  31. "github.com/docker/compose/v2/pkg/utils"
  32. )
  33. const (
  34. extLifecycle = "x-lifecycle"
  35. forceRecreate = "force_recreate"
  36. doubledContainerNameWarning = "WARNING: The %q service is using the custom container name %q. " +
  37. "Docker requires each container to have a unique name. " +
  38. "Remove the custom name to scale the service.\n"
  39. )
  40. // convergence manages service's container lifecycle.
  41. // Based on initially observed state, it reconciles the existing container with desired state, which might include
  42. // re-creating container, adding or removing replicas, or starting stopped containers.
  43. // Cross services dependencies are managed by creating services in expected order and updating `service:xx` reference
  44. // when a service has converged, so dependent ones can be managed with resolved containers references.
  45. type convergence struct {
  46. service *composeService
  47. observedState map[string]Containers
  48. stateMutex sync.Mutex
  49. }
  50. func (c *convergence) getObservedState(serviceName string) Containers {
  51. c.stateMutex.Lock()
  52. defer c.stateMutex.Unlock()
  53. return c.observedState[serviceName]
  54. }
  55. func (c *convergence) setObservedState(serviceName string, containers Containers) {
  56. c.stateMutex.Lock()
  57. defer c.stateMutex.Unlock()
  58. c.observedState[serviceName] = containers
  59. }
  60. func newConvergence(services []string, state Containers, s *composeService) *convergence {
  61. observedState := map[string]Containers{}
  62. for _, s := range services {
  63. observedState[s] = Containers{}
  64. }
  65. for _, c := range state.filter(isNotOneOff) {
  66. service := c.Labels[api.ServiceLabel]
  67. observedState[service] = append(observedState[service], c)
  68. }
  69. return &convergence{
  70. service: s,
  71. observedState: observedState,
  72. }
  73. }
  74. func (c *convergence) apply(ctx context.Context, project *types.Project, options api.CreateOptions) error {
  75. return InDependencyOrder(ctx, project, func(ctx context.Context, name string) error {
  76. service, err := project.GetService(name)
  77. if err != nil {
  78. return err
  79. }
  80. strategy := options.RecreateDependencies
  81. if utils.StringContains(options.Services, name) {
  82. strategy = options.Recreate
  83. }
  84. err = c.ensureService(ctx, project, service, strategy, options.Inherit, options.Timeout)
  85. if err != nil {
  86. return err
  87. }
  88. c.updateProject(project, name)
  89. return nil
  90. })
  91. }
  92. var mu sync.Mutex
  93. // updateProject updates project after service converged, so dependent services relying on `service:xx` can refer to actual containers.
  94. func (c *convergence) updateProject(project *types.Project, service string) {
  95. containers := c.getObservedState(service)
  96. if len(containers) == 0 {
  97. return
  98. }
  99. container := containers[0]
  100. // operation is protected by a Mutex so that we can safely update project.Services while running concurrent convergence on services
  101. mu.Lock()
  102. defer mu.Unlock()
  103. for i, s := range project.Services {
  104. if d := getDependentServiceFromMode(s.NetworkMode); d == service {
  105. s.NetworkMode = types.NetworkModeContainerPrefix + container.ID
  106. }
  107. if d := getDependentServiceFromMode(s.Ipc); d == service {
  108. s.Ipc = types.NetworkModeContainerPrefix + container.ID
  109. }
  110. if d := getDependentServiceFromMode(s.Pid); d == service {
  111. s.Pid = types.NetworkModeContainerPrefix + container.ID
  112. }
  113. var links []string
  114. for _, serviceLink := range s.Links {
  115. parts := strings.Split(serviceLink, ":")
  116. serviceName := serviceLink
  117. serviceAlias := ""
  118. if len(parts) == 2 {
  119. serviceName = parts[0]
  120. serviceAlias = parts[1]
  121. }
  122. if serviceName != service {
  123. links = append(links, serviceLink)
  124. continue
  125. }
  126. for _, container := range containers {
  127. name := getCanonicalContainerName(container)
  128. if serviceAlias != "" {
  129. links = append(links,
  130. fmt.Sprintf("%s:%s", name, serviceAlias))
  131. }
  132. links = append(links,
  133. fmt.Sprintf("%s:%s", name, name),
  134. fmt.Sprintf("%s:%s", name, getContainerNameWithoutProject(container)))
  135. }
  136. s.Links = links
  137. }
  138. project.Services[i] = s
  139. }
  140. }
  141. func (c *convergence) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string, inherit bool, timeout *time.Duration) error {
  142. expected, err := getScale(service)
  143. if err != nil {
  144. return err
  145. }
  146. containers := c.getObservedState(service.Name)
  147. actual := len(containers)
  148. updated := make(Containers, expected)
  149. eg, _ := errgroup.WithContext(ctx)
  150. for i, container := range containers {
  151. if i >= expected {
  152. // Scale Down
  153. container := container
  154. eg.Go(func() error {
  155. err := c.service.apiClient.ContainerStop(ctx, container.ID, timeout)
  156. if err != nil {
  157. return err
  158. }
  159. return c.service.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
  160. })
  161. continue
  162. }
  163. if recreate == api.RecreateNever {
  164. continue
  165. }
  166. // Re-create diverged containers
  167. configHash, err := ServiceHash(service)
  168. if err != nil {
  169. return err
  170. }
  171. name := getContainerProgressName(container)
  172. diverged := container.Labels[api.ConfigHashLabel] != configHash
  173. if diverged || recreate == api.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
  174. i, container := i, container
  175. eg.Go(func() error {
  176. recreated, err := c.service.recreateContainer(ctx, project, service, container, inherit, timeout)
  177. updated[i] = recreated
  178. return err
  179. })
  180. continue
  181. }
  182. // Enforce non-diverged containers are running
  183. w := progress.ContextWriter(ctx)
  184. switch container.State {
  185. case ContainerRunning:
  186. w.Event(progress.RunningEvent(name))
  187. case ContainerCreated:
  188. case ContainerRestarting:
  189. case ContainerExited:
  190. w.Event(progress.CreatedEvent(name))
  191. default:
  192. container := container
  193. eg.Go(func() error {
  194. return c.service.startContainer(ctx, container)
  195. })
  196. }
  197. updated[i] = container
  198. }
  199. next, err := nextContainerNumber(containers)
  200. if err != nil {
  201. return err
  202. }
  203. for i := 0; i < expected-actual; i++ {
  204. // Scale UP
  205. number := next + i
  206. name := getContainerName(project.Name, service, number)
  207. i := i
  208. eg.Go(func() error {
  209. container, err := c.service.createContainer(ctx, project, service, name, number, false, true)
  210. updated[actual+i] = container
  211. return err
  212. })
  213. continue
  214. }
  215. err = eg.Wait()
  216. c.setObservedState(service.Name, updated)
  217. return err
  218. }
  219. func getContainerName(projectName string, service types.ServiceConfig, number int) string {
  220. name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator)
  221. if service.ContainerName != "" {
  222. name = service.ContainerName
  223. }
  224. return name
  225. }
  226. func getContainerProgressName(container moby.Container) string {
  227. return "Container " + getCanonicalContainerName(container)
  228. }
  229. func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
  230. eg, _ := errgroup.WithContext(ctx)
  231. for dep, config := range service.DependsOn {
  232. dep, config := dep, config
  233. eg.Go(func() error {
  234. ticker := time.NewTicker(500 * time.Millisecond)
  235. defer ticker.Stop()
  236. for {
  237. <-ticker.C
  238. switch config.Condition {
  239. case types.ServiceConditionHealthy:
  240. healthy, err := s.isServiceHealthy(ctx, project, dep)
  241. if err != nil {
  242. return err
  243. }
  244. if healthy {
  245. return nil
  246. }
  247. case types.ServiceConditionCompletedSuccessfully:
  248. exited, code, err := s.isServiceCompleted(ctx, project, dep)
  249. if err != nil {
  250. return err
  251. }
  252. if exited {
  253. if code != 0 {
  254. return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
  255. }
  256. return nil
  257. }
  258. case types.ServiceConditionStarted:
  259. // already managed by InDependencyOrder
  260. return nil
  261. default:
  262. logrus.Warnf("unsupported depends_on condition: %s", config.Condition)
  263. return nil
  264. }
  265. }
  266. })
  267. }
  268. return eg.Wait()
  269. }
  270. func nextContainerNumber(containers []moby.Container) (int, error) {
  271. max := 0
  272. for _, c := range containers {
  273. n, err := strconv.Atoi(c.Labels[api.ContainerNumberLabel])
  274. if err != nil {
  275. return 0, err
  276. }
  277. if n > max {
  278. max = n
  279. }
  280. }
  281. return max + 1, nil
  282. }
  283. func getScale(config types.ServiceConfig) (int, error) {
  284. scale := 1
  285. if config.Deploy != nil && config.Deploy.Replicas != nil {
  286. scale = int(*config.Deploy.Replicas)
  287. }
  288. if scale > 1 && config.ContainerName != "" {
  289. return 0, fmt.Errorf(doubledContainerNameWarning,
  290. config.Name,
  291. config.ContainerName)
  292. }
  293. return scale, nil
  294. }
  295. func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  296. name string, number int, autoRemove bool, useNetworkAliases bool) (container moby.Container, err error) {
  297. w := progress.ContextWriter(ctx)
  298. eventName := "Container " + name
  299. w.Event(progress.CreatingEvent(eventName))
  300. container, err = s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove, useNetworkAliases)
  301. if err != nil {
  302. return
  303. }
  304. w.Event(progress.CreatedEvent(eventName))
  305. return
  306. }
  307. func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  308. replaced moby.Container, inherit bool, timeout *time.Duration) (moby.Container, error) {
  309. var created moby.Container
  310. w := progress.ContextWriter(ctx)
  311. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Working, "Recreate"))
  312. err := s.apiClient.ContainerStop(ctx, replaced.ID, timeout)
  313. if err != nil {
  314. return created, err
  315. }
  316. name := getCanonicalContainerName(replaced)
  317. tmpName := fmt.Sprintf("%s_%s", replaced.ID[:12], name)
  318. err = s.apiClient.ContainerRename(ctx, replaced.ID, tmpName)
  319. if err != nil {
  320. return created, err
  321. }
  322. number, err := strconv.Atoi(replaced.Labels[api.ContainerNumberLabel])
  323. if err != nil {
  324. return created, err
  325. }
  326. var inherited *moby.Container
  327. if inherit {
  328. inherited = &replaced
  329. }
  330. name = getContainerName(project.Name, service, number)
  331. created, err = s.createMobyContainer(ctx, project, service, name, number, inherited, false, true)
  332. if err != nil {
  333. return created, err
  334. }
  335. err = s.apiClient.ContainerRemove(ctx, replaced.ID, moby.ContainerRemoveOptions{})
  336. if err != nil {
  337. return created, err
  338. }
  339. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Done, "Recreated"))
  340. setDependentLifecycle(project, service.Name, forceRecreate)
  341. return created, err
  342. }
  343. // setDependentLifecycle define the Lifecycle strategy for all services to depend on specified service
  344. func setDependentLifecycle(project *types.Project, service string, strategy string) {
  345. for i, s := range project.Services {
  346. if utils.StringContains(s.GetDependencies(), service) {
  347. if s.Extensions == nil {
  348. s.Extensions = map[string]interface{}{}
  349. }
  350. s.Extensions[extLifecycle] = strategy
  351. project.Services[i] = s
  352. }
  353. }
  354. }
  355. func (s *composeService) startContainer(ctx context.Context, container moby.Container) error {
  356. w := progress.ContextWriter(ctx)
  357. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Restart"))
  358. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  359. if err != nil {
  360. return err
  361. }
  362. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Restarted"))
  363. return nil
  364. }
  365. func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  366. name string, number int, inherit *moby.Container, autoRemove bool, useNetworkAliases bool) (moby.Container, error) {
  367. var created moby.Container
  368. containerConfig, hostConfig, networkingConfig, err := s.getCreateOptions(ctx, project, service, number, inherit, autoRemove)
  369. if err != nil {
  370. return created, err
  371. }
  372. var plat *specs.Platform
  373. if service.Platform != "" {
  374. var p specs.Platform
  375. p, err = platforms.Parse(service.Platform)
  376. if err != nil {
  377. return created, err
  378. }
  379. plat = &p
  380. }
  381. response, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, plat, name)
  382. if err != nil {
  383. return created, err
  384. }
  385. inspectedContainer, err := s.apiClient.ContainerInspect(ctx, response.ID)
  386. if err != nil {
  387. return created, err
  388. }
  389. created = moby.Container{
  390. ID: inspectedContainer.ID,
  391. Labels: inspectedContainer.Config.Labels,
  392. Names: []string{inspectedContainer.Name},
  393. NetworkSettings: &moby.SummaryNetworkSettings{
  394. Networks: inspectedContainer.NetworkSettings.Networks,
  395. },
  396. }
  397. links := append(service.Links, service.ExternalLinks...)
  398. for _, netName := range service.NetworksByPriority() {
  399. netwrk := project.Networks[netName]
  400. cfg := service.Networks[netName]
  401. aliases := []string{getContainerName(project.Name, service, number)}
  402. if useNetworkAliases {
  403. aliases = append(aliases, service.Name)
  404. if cfg != nil {
  405. aliases = append(aliases, cfg.Aliases...)
  406. }
  407. }
  408. if val, ok := created.NetworkSettings.Networks[netwrk.Name]; ok {
  409. if shortIDAliasExists(created.ID, val.Aliases...) {
  410. continue
  411. }
  412. err = s.apiClient.NetworkDisconnect(ctx, netwrk.Name, created.ID, false)
  413. if err != nil {
  414. return created, err
  415. }
  416. }
  417. err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
  418. if err != nil {
  419. return created, err
  420. }
  421. }
  422. return created, err
  423. }
  424. func shortIDAliasExists(containerID string, aliases ...string) bool {
  425. for _, alias := range aliases {
  426. if alias == containerID[:12] {
  427. return true
  428. }
  429. }
  430. return false
  431. }
  432. func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
  433. var (
  434. ipv4Address string
  435. ipv6Address string
  436. ipam *network.EndpointIPAMConfig
  437. )
  438. if cfg != nil {
  439. ipv4Address = cfg.Ipv4Address
  440. ipv6Address = cfg.Ipv6Address
  441. ipam = &network.EndpointIPAMConfig{
  442. IPv4Address: ipv4Address,
  443. IPv6Address: ipv6Address,
  444. }
  445. }
  446. err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
  447. Aliases: aliases,
  448. IPAddress: ipv4Address,
  449. GlobalIPv6Address: ipv6Address,
  450. Links: links,
  451. IPAMConfig: ipam,
  452. })
  453. if err != nil {
  454. return err
  455. }
  456. return nil
  457. }
  458. func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
  459. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
  460. if err != nil {
  461. return false, err
  462. }
  463. if len(containers) == 0 {
  464. return false, nil
  465. }
  466. for _, c := range containers {
  467. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  468. if err != nil {
  469. return false, err
  470. }
  471. if container.State == nil || container.State.Health == nil {
  472. return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
  473. }
  474. if container.State.Health.Status != moby.Healthy {
  475. return false, nil
  476. }
  477. }
  478. return true, nil
  479. }
  480. func (s *composeService) isServiceCompleted(ctx context.Context, project *types.Project, dep string) (bool, int, error) {
  481. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, dep)
  482. if err != nil {
  483. return false, 0, err
  484. }
  485. for _, c := range containers {
  486. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  487. if err != nil {
  488. return false, 0, err
  489. }
  490. if container.State != nil && container.State.Status == "exited" {
  491. return true, container.State.ExitCode, nil
  492. }
  493. }
  494. return false, 0, nil
  495. }
  496. func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
  497. err := s.waitDependencies(ctx, project, service)
  498. if err != nil {
  499. return err
  500. }
  501. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  502. Filters: filters.NewArgs(
  503. projectFilter(project.Name),
  504. serviceFilter(service.Name),
  505. oneOffFilter(false),
  506. ),
  507. All: true,
  508. })
  509. if err != nil {
  510. return err
  511. }
  512. if len(containers) == 0 {
  513. if scale, err := getScale(service); err != nil && scale == 0 {
  514. return nil
  515. }
  516. return fmt.Errorf("no containers to start")
  517. }
  518. w := progress.ContextWriter(ctx)
  519. eg, ctx := errgroup.WithContext(ctx)
  520. for _, container := range containers {
  521. if container.State == ContainerRunning {
  522. continue
  523. }
  524. container := container
  525. eg.Go(func() error {
  526. eventName := getContainerProgressName(container)
  527. w.Event(progress.StartingEvent(eventName))
  528. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  529. if err == nil {
  530. w.Event(progress.StartedEvent(eventName))
  531. }
  532. return err
  533. })
  534. }
  535. return eg.Wait()
  536. }