1
0

compatibility.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 ecs
  14. import (
  15. "fmt"
  16. "github.com/compose-spec/compose-go/compatibility"
  17. "github.com/compose-spec/compose-go/errdefs"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/sirupsen/logrus"
  20. )
  21. func (b *ecsAPIService) checkCompatibility(project *types.Project) error {
  22. var checker compatibility.Checker = &fargateCompatibilityChecker{
  23. AllowList: compatibility.AllowList{
  24. Supported: compatibleComposeAttributes,
  25. },
  26. projet: project,
  27. }
  28. compatibility.Check(project, checker)
  29. for _, err := range checker.Errors() {
  30. if errdefs.IsIncompatibleError(err) {
  31. return err
  32. }
  33. logrus.Warn(err.Error())
  34. }
  35. if !compatibility.IsCompatible(checker) {
  36. return fmt.Errorf("compose file is incompatible with Amazon ECS")
  37. }
  38. return nil
  39. }
  40. type fargateCompatibilityChecker struct {
  41. compatibility.AllowList
  42. projet *types.Project
  43. }
  44. var compatibleComposeAttributes = []string{
  45. "services.command",
  46. "services.container_name",
  47. "services.cap_drop",
  48. "services.depends_on",
  49. "services.deploy",
  50. "services.deploy.placement",
  51. "services.deploy.placement.constraints",
  52. "services.deploy.replicas",
  53. "services.deploy.resources.limits",
  54. "services.deploy.resources.limits.cpus",
  55. "services.deploy.resources.limits.memory",
  56. "services.deploy.resources.reservations",
  57. "services.deploy.resources.reservations.cpus",
  58. "services.deploy.resources.reservations.memory",
  59. "services.deploy.resources.reservations.devices",
  60. "services.deploy.resources.reservations.devices.capabilities",
  61. "services.deploy.resources.reservations.devices.count",
  62. "services.deploy.resources.reservations.devices.driver",
  63. "services.deploy.resources.reservations.generic_resources",
  64. "services.deploy.resources.reservations.generic_resources.discrete_resource_spec",
  65. "services.deploy.update_config",
  66. "services.deploy.update_config.parallelism",
  67. "services.entrypoint",
  68. "services.environment",
  69. "services.env_file",
  70. "services.healthcheck",
  71. "services.healthcheck.interval",
  72. "services.healthcheck.retries",
  73. "services.healthcheck.start_period",
  74. "services.healthcheck.test",
  75. "services.healthcheck.timeout",
  76. "services.image",
  77. "services.init",
  78. "services.logging",
  79. "services.logging.options",
  80. "services.networks",
  81. "services.ports",
  82. "services.ports.mode",
  83. "services.ports.target",
  84. "services.ports.protocol",
  85. "services.secrets",
  86. "services.secrets.source",
  87. "services.secrets.target",
  88. "services.user",
  89. "services.volumes",
  90. "services.volumes.read_only",
  91. "services.volumes.target",
  92. "services.working_dir",
  93. "secrets.external",
  94. "secrets.name",
  95. "secrets.file",
  96. "volumes",
  97. "volumes.external",
  98. "volumes.name",
  99. "volumes.driver_opts",
  100. "networks.external",
  101. "networks.name",
  102. }
  103. func (c *fargateCompatibilityChecker) CheckImage(service *types.ServiceConfig) {
  104. if service.Image == "" {
  105. c.Incompatible("service %s doesn't define a Docker image to run", service.Name)
  106. }
  107. }
  108. func (c *fargateCompatibilityChecker) CheckPortsPublished(p *types.ServicePortConfig) {
  109. if p.Published == 0 {
  110. p.Published = p.Target
  111. }
  112. if p.Published != p.Target {
  113. c.Incompatible("published port can't be set to a distinct value than container port")
  114. }
  115. }
  116. func (c *fargateCompatibilityChecker) CheckVolumesSource(config *types.ServiceVolumeConfig) {
  117. if config.Type == types.VolumeTypeBind {
  118. c.Incompatible("ECS Fargate does not support bind mounts from host")
  119. }
  120. if config.Type == types.VolumeTypeTmpfs {
  121. c.Incompatible("ECS Fargate does not support tmpfs")
  122. }
  123. }
  124. func (c *fargateCompatibilityChecker) CheckCapAdd(service *types.ServiceConfig) {
  125. add := []string{}
  126. for _, cap := range service.CapAdd {
  127. switch cap {
  128. case "SYS_PTRACE":
  129. add = append(add, cap)
  130. default:
  131. c.Incompatible("ECS doesn't allow to add capability %s", cap)
  132. }
  133. }
  134. service.CapAdd = add
  135. }
  136. func (c *fargateCompatibilityChecker) CheckLoggingDriver(config *types.LoggingConfig) {
  137. if config.Driver != "" && config.Driver != "awslogs" {
  138. c.Unsupported("services.logging.driver %s is not supported", config.Driver)
  139. }
  140. }
  141. func (c *fargateCompatibilityChecker) CheckUlimits(service *types.ServiceConfig) {
  142. for k := range service.Ulimits {
  143. if k != "nofile" {
  144. c.Unsupported("services.ulimits.%s is not supported by Fargate", k)
  145. delete(service.Ulimits, k)
  146. }
  147. }
  148. }
  149. func (c *fargateCompatibilityChecker) CheckDeployResourcesDevicesCapabilities(s string, r types.DeviceRequest) {
  150. for _, cap := range r.Capabilities {
  151. if cap != "gpu" {
  152. c.Unsupported("services.deploy.resources.%s.devices.capabilities = %s", s, cap)
  153. }
  154. }
  155. }
  156. func (c *fargateCompatibilityChecker) CheckDeployResourcesDevicesDriver(s string, r types.DeviceRequest) {
  157. if r.Driver != "" && r.Driver != "nvidia" {
  158. c.Unsupported("services.deploy.resources.%s.devices.driver = %s", s, r.Driver)
  159. }
  160. }