convergence.go 17 KB

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