compatibility.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. compatibility.AllowList{
  24. Supported: compatibleComposeAttributes,
  25. },
  26. }
  27. compatibility.Check(project, checker)
  28. for _, err := range checker.Errors() {
  29. if errdefs.IsIncompatibleError(err) {
  30. return err
  31. }
  32. logrus.Warn(err.Error())
  33. }
  34. if !compatibility.IsCompatible(checker) {
  35. return fmt.Errorf("compose file is incompatible with Amazon ECS")
  36. }
  37. return nil
  38. }
  39. type fargateCompatibilityChecker struct {
  40. compatibility.AllowList
  41. }
  42. var compatibleComposeAttributes = []string{
  43. "services.command",
  44. "services.container_name",
  45. "services.cap_drop",
  46. "services.depends_on",
  47. "services.deploy",
  48. "services.deploy.replicas",
  49. "services.deploy.resources.limits",
  50. "services.deploy.resources.limits.cpus",
  51. "services.deploy.resources.limits.memory",
  52. "services.deploy.resources.reservations",
  53. "services.deploy.resources.reservations.cpus",
  54. "services.deploy.resources.reservations.memory",
  55. "services.deploy.resources.reservations.generic_resources",
  56. "services.deploy.resources.reservations.generic_resources.discrete_resource_spec",
  57. "services.deploy.update_config",
  58. "services.deploy.update_config.parallelism",
  59. "services.entrypoint",
  60. "services.environment",
  61. "services.env_file",
  62. "services.healthcheck",
  63. "services.healthcheck.interval",
  64. "services.healthcheck.retries",
  65. "services.healthcheck.start_period",
  66. "services.healthcheck.test",
  67. "services.healthcheck.timeout",
  68. "services.image",
  69. "services.init",
  70. "services.logging",
  71. "services.logging.options",
  72. "services.networks",
  73. "services.ports",
  74. "services.ports.mode",
  75. "services.ports.target",
  76. "services.ports.protocol",
  77. "services.secrets",
  78. "services.secrets.source",
  79. "services.secrets.target",
  80. "services.user",
  81. "services.volumes",
  82. "services.volumes.read_only",
  83. "services.volumes.source",
  84. "services.volumes.target",
  85. "services.working_dir",
  86. "secrets.external",
  87. "secrets.name",
  88. "secrets.file",
  89. "volumes",
  90. "volumes.external",
  91. "volumes.name",
  92. "networks.external",
  93. "networks.name",
  94. }
  95. func (c *fargateCompatibilityChecker) CheckImage(service *types.ServiceConfig) {
  96. if service.Image == "" {
  97. c.Incompatible("service %s doesn't define a Docker image to run", service.Name)
  98. }
  99. }
  100. func (c *fargateCompatibilityChecker) CheckPortsPublished(p *types.ServicePortConfig) {
  101. if p.Published == 0 {
  102. p.Published = p.Target
  103. }
  104. if p.Published != p.Target {
  105. c.Incompatible("published port can't be set to a distinct value than container port")
  106. }
  107. }
  108. func (c *fargateCompatibilityChecker) CheckCapAdd(service *types.ServiceConfig) {
  109. add := []string{}
  110. for _, cap := range service.CapAdd {
  111. switch cap {
  112. case "SYS_PTRACE":
  113. add = append(add, cap)
  114. default:
  115. c.Incompatible("ECS doesn't allow to add capability %s", cap)
  116. }
  117. }
  118. service.CapAdd = add
  119. }
  120. func (c *fargateCompatibilityChecker) CheckLoggingDriver(config *types.LoggingConfig) {
  121. if config.Driver != "" && config.Driver != "awslogs" {
  122. c.Unsupported("services.logging.driver %s is not supported", config.Driver)
  123. }
  124. }
  125. func (c *fargateCompatibilityChecker) CheckUlimits(service *types.ServiceConfig) {
  126. for k := range service.Ulimits {
  127. if k != "nofile" {
  128. c.Unsupported("services.ulimits.%s is not supported by Fargate", k)
  129. delete(service.Ulimits, k)
  130. }
  131. }
  132. }