convergence.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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, serviceName string) {
  95. // operation is protected by a Mutex so that we can safely update project.Services while running concurrent convergence on services
  96. mu.Lock()
  97. defer mu.Unlock()
  98. cnts := c.getObservedState(serviceName)
  99. for i, s := range project.Services {
  100. updateServices(&s, cnts)
  101. project.Services[i] = s
  102. }
  103. }
  104. func updateServices(service *types.ServiceConfig, cnts Containers) {
  105. if len(cnts) == 0 {
  106. return
  107. }
  108. cnt := cnts[0]
  109. serviceName := cnt.Labels[api.ServiceLabel]
  110. if d := getDependentServiceFromMode(service.NetworkMode); d == serviceName {
  111. service.NetworkMode = types.NetworkModeContainerPrefix + cnt.ID
  112. }
  113. if d := getDependentServiceFromMode(service.Ipc); d == serviceName {
  114. service.Ipc = types.NetworkModeContainerPrefix + cnt.ID
  115. }
  116. if d := getDependentServiceFromMode(service.Pid); d == serviceName {
  117. service.Pid = types.NetworkModeContainerPrefix + cnt.ID
  118. }
  119. var links []string
  120. for _, serviceLink := range service.Links {
  121. parts := strings.Split(serviceLink, ":")
  122. serviceName := serviceLink
  123. serviceAlias := ""
  124. if len(parts) == 2 {
  125. serviceName = parts[0]
  126. serviceAlias = parts[1]
  127. }
  128. if serviceName != service.Name {
  129. links = append(links, serviceLink)
  130. continue
  131. }
  132. for _, container := range cnts {
  133. name := getCanonicalContainerName(container)
  134. if serviceAlias != "" {
  135. links = append(links,
  136. fmt.Sprintf("%s:%s", name, serviceAlias))
  137. }
  138. links = append(links,
  139. fmt.Sprintf("%s:%s", name, name),
  140. fmt.Sprintf("%s:%s", name, getContainerNameWithoutProject(container)))
  141. }
  142. service.Links = links
  143. }
  144. }
  145. func (c *convergence) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string, inherit bool, timeout *time.Duration) error {
  146. expected, err := getScale(service)
  147. if err != nil {
  148. return err
  149. }
  150. containers := c.getObservedState(service.Name)
  151. actual := len(containers)
  152. updated := make(Containers, expected)
  153. eg, _ := errgroup.WithContext(ctx)
  154. for i, container := range containers {
  155. if i >= expected {
  156. // Scale Down
  157. container := container
  158. eg.Go(func() error {
  159. err := c.service.apiClient.ContainerStop(ctx, container.ID, timeout)
  160. if err != nil {
  161. return err
  162. }
  163. return c.service.apiClient.ContainerRemove(ctx, container.ID, moby.ContainerRemoveOptions{})
  164. })
  165. continue
  166. }
  167. if recreate == api.RecreateNever {
  168. continue
  169. }
  170. // Re-create diverged containers
  171. configHash, err := ServiceHash(service)
  172. if err != nil {
  173. return err
  174. }
  175. name := getContainerProgressName(container)
  176. diverged := container.Labels[api.ConfigHashLabel] != configHash
  177. if diverged || recreate == api.RecreateForce || service.Extensions[extLifecycle] == forceRecreate {
  178. i, container := i, container
  179. eg.Go(func() error {
  180. recreated, err := c.service.recreateContainer(ctx, project, service, container, inherit, timeout)
  181. updated[i] = recreated
  182. return err
  183. })
  184. continue
  185. }
  186. // Enforce non-diverged containers are running
  187. w := progress.ContextWriter(ctx)
  188. switch container.State {
  189. case ContainerRunning:
  190. w.Event(progress.RunningEvent(name))
  191. case ContainerCreated:
  192. case ContainerRestarting:
  193. case ContainerExited:
  194. w.Event(progress.CreatedEvent(name))
  195. default:
  196. container := container
  197. eg.Go(func() error {
  198. return c.service.startContainer(ctx, container)
  199. })
  200. }
  201. updated[i] = container
  202. }
  203. next, err := nextContainerNumber(containers)
  204. if err != nil {
  205. return err
  206. }
  207. for i := 0; i < expected-actual; i++ {
  208. // Scale UP
  209. number := next + i
  210. name := getContainerName(project.Name, service, number)
  211. i := i
  212. eg.Go(func() error {
  213. container, err := c.service.createContainer(ctx, project, service, name, number, false, true, false)
  214. updated[actual+i] = container
  215. return err
  216. })
  217. continue
  218. }
  219. err = eg.Wait()
  220. c.setObservedState(service.Name, updated)
  221. return err
  222. }
  223. func getContainerName(projectName string, service types.ServiceConfig, number int) string {
  224. name := strings.Join([]string{projectName, service.Name, strconv.Itoa(number)}, Separator)
  225. if service.ContainerName != "" {
  226. name = service.ContainerName
  227. }
  228. return name
  229. }
  230. func getContainerProgressName(container moby.Container) string {
  231. return "Container " + getCanonicalContainerName(container)
  232. }
  233. const ServiceConditionRunningOrHealthy = "running_or_healthy"
  234. func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig) error {
  235. eg, _ := errgroup.WithContext(ctx)
  236. for dep, config := range dependencies {
  237. dep, config := dep, config
  238. eg.Go(func() error {
  239. ticker := time.NewTicker(500 * time.Millisecond)
  240. defer ticker.Stop()
  241. for {
  242. <-ticker.C
  243. switch config.Condition {
  244. case ServiceConditionRunningOrHealthy:
  245. healthy, err := s.isServiceHealthy(ctx, project, dep, true)
  246. if err != nil {
  247. return err
  248. }
  249. if healthy {
  250. return nil
  251. }
  252. case types.ServiceConditionHealthy:
  253. healthy, err := s.isServiceHealthy(ctx, project, dep, false)
  254. if err != nil {
  255. return err
  256. }
  257. if healthy {
  258. return nil
  259. }
  260. case types.ServiceConditionCompletedSuccessfully:
  261. exited, code, err := s.isServiceCompleted(ctx, project, dep)
  262. if err != nil {
  263. return err
  264. }
  265. if exited {
  266. if code != 0 {
  267. return fmt.Errorf("service %q didn't completed successfully: exit %d", dep, code)
  268. }
  269. return nil
  270. }
  271. case types.ServiceConditionStarted:
  272. // already managed by InDependencyOrder
  273. return nil
  274. default:
  275. logrus.Warnf("unsupported depends_on condition: %s", config.Condition)
  276. return nil
  277. }
  278. }
  279. })
  280. }
  281. return eg.Wait()
  282. }
  283. func nextContainerNumber(containers []moby.Container) (int, error) {
  284. max := 0
  285. for _, c := range containers {
  286. n, err := strconv.Atoi(c.Labels[api.ContainerNumberLabel])
  287. if err != nil {
  288. return 0, err
  289. }
  290. if n > max {
  291. max = n
  292. }
  293. }
  294. return max + 1, nil
  295. }
  296. func getScale(config types.ServiceConfig) (int, error) {
  297. scale := 1
  298. if config.Deploy != nil && config.Deploy.Replicas != nil {
  299. scale = int(*config.Deploy.Replicas)
  300. }
  301. if scale > 1 && config.ContainerName != "" {
  302. return 0, fmt.Errorf(doubledContainerNameWarning,
  303. config.Name,
  304. config.ContainerName)
  305. }
  306. return scale, nil
  307. }
  308. func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  309. name string, number int, autoRemove bool, useNetworkAliases bool, attachStdin bool) (container moby.Container, err error) {
  310. w := progress.ContextWriter(ctx)
  311. eventName := "Container " + name
  312. w.Event(progress.CreatingEvent(eventName))
  313. container, err = s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove, useNetworkAliases, attachStdin)
  314. if err != nil {
  315. return
  316. }
  317. w.Event(progress.CreatedEvent(eventName))
  318. return
  319. }
  320. func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  321. replaced moby.Container, inherit bool, timeout *time.Duration) (moby.Container, error) {
  322. var created moby.Container
  323. w := progress.ContextWriter(ctx)
  324. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Working, "Recreate"))
  325. err := s.apiClient.ContainerStop(ctx, replaced.ID, timeout)
  326. if err != nil {
  327. return created, err
  328. }
  329. name := getCanonicalContainerName(replaced)
  330. tmpName := fmt.Sprintf("%s_%s", replaced.ID[:12], name)
  331. err = s.apiClient.ContainerRename(ctx, replaced.ID, tmpName)
  332. if err != nil {
  333. return created, err
  334. }
  335. number, err := strconv.Atoi(replaced.Labels[api.ContainerNumberLabel])
  336. if err != nil {
  337. return created, err
  338. }
  339. var inherited *moby.Container
  340. if inherit {
  341. inherited = &replaced
  342. }
  343. name = getContainerName(project.Name, service, number)
  344. created, err = s.createMobyContainer(ctx, project, service, name, number, inherited, false, true, false)
  345. if err != nil {
  346. return created, err
  347. }
  348. err = s.apiClient.ContainerRemove(ctx, replaced.ID, moby.ContainerRemoveOptions{})
  349. if err != nil {
  350. return created, err
  351. }
  352. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Done, "Recreated"))
  353. setDependentLifecycle(project, service.Name, forceRecreate)
  354. return created, err
  355. }
  356. // setDependentLifecycle define the Lifecycle strategy for all services to depend on specified service
  357. func setDependentLifecycle(project *types.Project, service string, strategy string) {
  358. for i, s := range project.Services {
  359. if utils.StringContains(s.GetDependencies(), service) {
  360. if s.Extensions == nil {
  361. s.Extensions = map[string]interface{}{}
  362. }
  363. s.Extensions[extLifecycle] = strategy
  364. project.Services[i] = s
  365. }
  366. }
  367. }
  368. func (s *composeService) startContainer(ctx context.Context, container moby.Container) error {
  369. w := progress.ContextWriter(ctx)
  370. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Restart"))
  371. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  372. if err != nil {
  373. return err
  374. }
  375. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Restarted"))
  376. return nil
  377. }
  378. func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  379. name string, number int, inherit *moby.Container, autoRemove bool, useNetworkAliases bool, attachStdin bool) (moby.Container, error) {
  380. var created moby.Container
  381. containerConfig, hostConfig, networkingConfig, err := s.getCreateOptions(ctx, project, service, number, inherit, autoRemove, attachStdin)
  382. if err != nil {
  383. return created, err
  384. }
  385. var plat *specs.Platform
  386. if service.Platform != "" {
  387. var p specs.Platform
  388. p, err = platforms.Parse(service.Platform)
  389. if err != nil {
  390. return created, err
  391. }
  392. plat = &p
  393. }
  394. response, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, plat, name)
  395. if err != nil {
  396. return created, err
  397. }
  398. inspectedContainer, err := s.apiClient.ContainerInspect(ctx, response.ID)
  399. if err != nil {
  400. return created, err
  401. }
  402. created = moby.Container{
  403. ID: inspectedContainer.ID,
  404. Labels: inspectedContainer.Config.Labels,
  405. Names: []string{inspectedContainer.Name},
  406. NetworkSettings: &moby.SummaryNetworkSettings{
  407. Networks: inspectedContainer.NetworkSettings.Networks,
  408. },
  409. }
  410. links := append(service.Links, service.ExternalLinks...)
  411. for _, netName := range service.NetworksByPriority() {
  412. netwrk := project.Networks[netName]
  413. cfg := service.Networks[netName]
  414. aliases := []string{getContainerName(project.Name, service, number)}
  415. if useNetworkAliases {
  416. aliases = append(aliases, service.Name)
  417. if cfg != nil {
  418. aliases = append(aliases, cfg.Aliases...)
  419. }
  420. }
  421. if val, ok := created.NetworkSettings.Networks[netwrk.Name]; ok {
  422. if shortIDAliasExists(created.ID, val.Aliases...) {
  423. continue
  424. }
  425. err = s.apiClient.NetworkDisconnect(ctx, netwrk.Name, created.ID, false)
  426. if err != nil {
  427. return created, err
  428. }
  429. }
  430. err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
  431. if err != nil {
  432. return created, err
  433. }
  434. }
  435. return created, err
  436. }
  437. func shortIDAliasExists(containerID string, aliases ...string) bool {
  438. for _, alias := range aliases {
  439. if alias == containerID[:12] {
  440. return true
  441. }
  442. }
  443. return false
  444. }
  445. func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
  446. var (
  447. ipv4Address string
  448. ipv6Address string
  449. ipam *network.EndpointIPAMConfig
  450. )
  451. if cfg != nil {
  452. ipv4Address = cfg.Ipv4Address
  453. ipv6Address = cfg.Ipv6Address
  454. ipam = &network.EndpointIPAMConfig{
  455. IPv4Address: ipv4Address,
  456. IPv6Address: ipv6Address,
  457. }
  458. }
  459. err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
  460. Aliases: aliases,
  461. IPAddress: ipv4Address,
  462. GlobalIPv6Address: ipv6Address,
  463. Links: links,
  464. IPAMConfig: ipam,
  465. })
  466. if err != nil {
  467. return err
  468. }
  469. return nil
  470. }
  471. func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string, fallbackRunning bool) (bool, error) {
  472. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
  473. if err != nil {
  474. return false, err
  475. }
  476. if len(containers) == 0 {
  477. return false, nil
  478. }
  479. for _, c := range containers {
  480. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  481. if err != nil {
  482. return false, err
  483. }
  484. if container.Config.Healthcheck == nil && fallbackRunning {
  485. // Container does not define a health check, but we can fall back to "running" state
  486. return container.State != nil && container.State.Status == "running", nil
  487. }
  488. if container.State == nil || container.State.Health == nil {
  489. return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
  490. }
  491. if container.State.Health.Status != moby.Healthy {
  492. return false, nil
  493. }
  494. }
  495. return true, nil
  496. }
  497. func (s *composeService) isServiceCompleted(ctx context.Context, project *types.Project, dep string) (bool, int, error) {
  498. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, dep)
  499. if err != nil {
  500. return false, 0, err
  501. }
  502. for _, c := range containers {
  503. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  504. if err != nil {
  505. return false, 0, err
  506. }
  507. if container.State != nil && container.State.Status == "exited" {
  508. return true, container.State.ExitCode, nil
  509. }
  510. }
  511. return false, 0, nil
  512. }
  513. func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
  514. err := s.waitDependencies(ctx, project, service.DependsOn)
  515. if err != nil {
  516. return err
  517. }
  518. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  519. Filters: filters.NewArgs(
  520. projectFilter(project.Name),
  521. serviceFilter(service.Name),
  522. oneOffFilter(false),
  523. ),
  524. All: true,
  525. })
  526. if err != nil {
  527. return err
  528. }
  529. if len(containers) == 0 {
  530. if scale, err := getScale(service); err != nil && scale == 0 {
  531. return nil
  532. }
  533. return fmt.Errorf("no containers to start")
  534. }
  535. w := progress.ContextWriter(ctx)
  536. eg, ctx := errgroup.WithContext(ctx)
  537. for _, container := range containers {
  538. if container.State == ContainerRunning {
  539. continue
  540. }
  541. container := container
  542. eg.Go(func() error {
  543. eventName := getContainerProgressName(container)
  544. w.Event(progress.StartingEvent(eventName))
  545. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  546. if err == nil {
  547. w.Event(progress.StartedEvent(eventName))
  548. }
  549. return err
  550. })
  551. }
  552. return eg.Wait()
  553. }