convergence.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 := fmt.Sprintf("%s_%s_%d", projectName, service.Name, number)
  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. var err error
  286. if config.Deploy != nil && config.Deploy.Replicas != nil {
  287. scale = int(*config.Deploy.Replicas)
  288. }
  289. if config.Scale != 0 {
  290. scale = config.Scale
  291. }
  292. if scale > 1 && config.ContainerName != "" {
  293. scale = -1
  294. err = fmt.Errorf(doubledContainerNameWarning,
  295. config.Name,
  296. config.ContainerName)
  297. }
  298. return scale, err
  299. }
  300. func (s *composeService) createContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  301. name string, number int, autoRemove bool, useNetworkAliases bool) (container moby.Container, err error) {
  302. w := progress.ContextWriter(ctx)
  303. eventName := "Container " + name
  304. w.Event(progress.CreatingEvent(eventName))
  305. container, err = s.createMobyContainer(ctx, project, service, name, number, nil, autoRemove, useNetworkAliases)
  306. if err != nil {
  307. return
  308. }
  309. w.Event(progress.CreatedEvent(eventName))
  310. return
  311. }
  312. func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  313. replaced moby.Container, inherit bool, timeout *time.Duration) (moby.Container, error) {
  314. var created moby.Container
  315. w := progress.ContextWriter(ctx)
  316. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Working, "Recreate"))
  317. err := s.apiClient.ContainerStop(ctx, replaced.ID, timeout)
  318. if err != nil {
  319. return created, err
  320. }
  321. name := getCanonicalContainerName(replaced)
  322. tmpName := fmt.Sprintf("%s_%s", replaced.ID[:12], name)
  323. err = s.apiClient.ContainerRename(ctx, replaced.ID, tmpName)
  324. if err != nil {
  325. return created, err
  326. }
  327. number, err := strconv.Atoi(replaced.Labels[api.ContainerNumberLabel])
  328. if err != nil {
  329. return created, err
  330. }
  331. var inherited *moby.Container
  332. if inherit {
  333. inherited = &replaced
  334. }
  335. name = getContainerName(project.Name, service, number)
  336. created, err = s.createMobyContainer(ctx, project, service, name, number, inherited, false, true)
  337. if err != nil {
  338. return created, err
  339. }
  340. err = s.apiClient.ContainerRemove(ctx, replaced.ID, moby.ContainerRemoveOptions{})
  341. if err != nil {
  342. return created, err
  343. }
  344. w.Event(progress.NewEvent(getContainerProgressName(replaced), progress.Done, "Recreated"))
  345. setDependentLifecycle(project, service.Name, forceRecreate)
  346. return created, err
  347. }
  348. // setDependentLifecycle define the Lifecycle strategy for all services to depend on specified service
  349. func setDependentLifecycle(project *types.Project, service string, strategy string) {
  350. for i, s := range project.Services {
  351. if utils.StringContains(s.GetDependencies(), service) {
  352. if s.Extensions == nil {
  353. s.Extensions = map[string]interface{}{}
  354. }
  355. s.Extensions[extLifecycle] = strategy
  356. project.Services[i] = s
  357. }
  358. }
  359. }
  360. func (s *composeService) startContainer(ctx context.Context, container moby.Container) error {
  361. w := progress.ContextWriter(ctx)
  362. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Restart"))
  363. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  364. if err != nil {
  365. return err
  366. }
  367. w.Event(progress.NewEvent(getContainerProgressName(container), progress.Done, "Restarted"))
  368. return nil
  369. }
  370. func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig,
  371. name string, number int, inherit *moby.Container, autoRemove bool, useNetworkAliases bool) (moby.Container, error) {
  372. var created moby.Container
  373. containerConfig, hostConfig, networkingConfig, err := s.getCreateOptions(ctx, project, service, number, inherit, autoRemove)
  374. if err != nil {
  375. return created, err
  376. }
  377. var plat *specs.Platform
  378. if service.Platform != "" {
  379. var p specs.Platform
  380. p, err = platforms.Parse(service.Platform)
  381. if err != nil {
  382. return created, err
  383. }
  384. plat = &p
  385. }
  386. response, err := s.apiClient.ContainerCreate(ctx, containerConfig, hostConfig, networkingConfig, plat, name)
  387. if err != nil {
  388. return created, err
  389. }
  390. inspectedContainer, err := s.apiClient.ContainerInspect(ctx, response.ID)
  391. if err != nil {
  392. return created, err
  393. }
  394. created = moby.Container{
  395. ID: inspectedContainer.ID,
  396. Labels: inspectedContainer.Config.Labels,
  397. Names: []string{inspectedContainer.Name},
  398. NetworkSettings: &moby.SummaryNetworkSettings{
  399. Networks: inspectedContainer.NetworkSettings.Networks,
  400. },
  401. }
  402. links := append(service.Links, service.ExternalLinks...)
  403. for _, netName := range service.NetworksByPriority() {
  404. netwrk := project.Networks[netName]
  405. cfg := service.Networks[netName]
  406. aliases := []string{getContainerName(project.Name, service, number)}
  407. if useNetworkAliases {
  408. aliases = append(aliases, service.Name)
  409. if cfg != nil {
  410. aliases = append(aliases, cfg.Aliases...)
  411. }
  412. }
  413. if val, ok := created.NetworkSettings.Networks[netwrk.Name]; ok {
  414. if shortIDAliasExists(created.ID, val.Aliases...) {
  415. continue
  416. }
  417. err = s.apiClient.NetworkDisconnect(ctx, netwrk.Name, created.ID, false)
  418. if err != nil {
  419. return created, err
  420. }
  421. }
  422. err = s.connectContainerToNetwork(ctx, created.ID, netwrk.Name, cfg, links, aliases...)
  423. if err != nil {
  424. return created, err
  425. }
  426. }
  427. return created, err
  428. }
  429. func shortIDAliasExists(containerID string, aliases ...string) bool {
  430. for _, alias := range aliases {
  431. if alias == containerID[:12] {
  432. return true
  433. }
  434. }
  435. return false
  436. }
  437. func (s *composeService) connectContainerToNetwork(ctx context.Context, id string, netwrk string, cfg *types.ServiceNetworkConfig, links []string, aliases ...string) error {
  438. var (
  439. ipv4Address string
  440. ipv6Address string
  441. ipam *network.EndpointIPAMConfig
  442. )
  443. if cfg != nil {
  444. ipv4Address = cfg.Ipv4Address
  445. ipv6Address = cfg.Ipv6Address
  446. ipam = &network.EndpointIPAMConfig{
  447. IPv4Address: ipv4Address,
  448. IPv6Address: ipv6Address,
  449. }
  450. }
  451. err := s.apiClient.NetworkConnect(ctx, netwrk, id, &network.EndpointSettings{
  452. Aliases: aliases,
  453. IPAddress: ipv4Address,
  454. GlobalIPv6Address: ipv6Address,
  455. Links: links,
  456. IPAMConfig: ipam,
  457. })
  458. if err != nil {
  459. return err
  460. }
  461. return nil
  462. }
  463. func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
  464. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
  465. if err != nil {
  466. return false, err
  467. }
  468. if len(containers) == 0 {
  469. return false, nil
  470. }
  471. for _, c := range containers {
  472. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  473. if err != nil {
  474. return false, err
  475. }
  476. if container.State == nil || container.State.Health == nil {
  477. return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
  478. }
  479. if container.State.Health.Status != moby.Healthy {
  480. return false, nil
  481. }
  482. }
  483. return true, nil
  484. }
  485. func (s *composeService) isServiceCompleted(ctx context.Context, project *types.Project, dep string) (bool, int, error) {
  486. containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, dep)
  487. if err != nil {
  488. return false, 0, err
  489. }
  490. for _, c := range containers {
  491. container, err := s.apiClient.ContainerInspect(ctx, c.ID)
  492. if err != nil {
  493. return false, 0, err
  494. }
  495. if container.State != nil && container.State.Status == "exited" {
  496. return true, container.State.ExitCode, nil
  497. }
  498. }
  499. return false, 0, nil
  500. }
  501. func (s *composeService) startService(ctx context.Context, project *types.Project, service types.ServiceConfig) error {
  502. err := s.waitDependencies(ctx, project, service)
  503. if err != nil {
  504. return err
  505. }
  506. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  507. Filters: filters.NewArgs(
  508. projectFilter(project.Name),
  509. serviceFilter(service.Name),
  510. oneOffFilter(false),
  511. ),
  512. All: true,
  513. })
  514. if err != nil {
  515. return err
  516. }
  517. if len(containers) == 0 {
  518. if scale, err := getScale(service); err != nil && scale == 0 {
  519. return nil
  520. }
  521. return fmt.Errorf("no containers to start")
  522. }
  523. w := progress.ContextWriter(ctx)
  524. eg, ctx := errgroup.WithContext(ctx)
  525. for _, container := range containers {
  526. if container.State == ContainerRunning {
  527. continue
  528. }
  529. container := container
  530. eg.Go(func() error {
  531. eventName := getContainerProgressName(container)
  532. w.Event(progress.StartingEvent(eventName))
  533. err := s.apiClient.ContainerStart(ctx, container.ID, moby.ContainerStartOptions{})
  534. if err == nil {
  535. w.Event(progress.StartedEvent(eventName))
  536. }
  537. return err
  538. })
  539. }
  540. return eg.Wait()
  541. }