diff --git a/ecs/pkg/amazon/backend/cloudformation_test.go b/ecs/pkg/amazon/backend/cloudformation_test.go index a364bfaff..e7efad187 100644 --- a/ecs/pkg/amazon/backend/cloudformation_test.go +++ b/ecs/pkg/amazon/backend/cloudformation_test.go @@ -117,6 +117,44 @@ services: assert.Check(t, s.DesiredCount == 10) } +func TestTaskSizeConvert(t *testing.T) { + template := convertYaml(t, "test", ` +version: "3" +services: + test: + image: nginx + deploy: + resources: + limits: + cpus: '0.5' + memory: 2048M + reservations: + cpus: '0.5' + memory: 2048M +`) + def := template.Resources["TestTaskDefinition"].(*ecs.TaskDefinition) + assert.Equal(t, def.Cpu, "512") + assert.Equal(t, def.Memory, "2048") + + template = convertYaml(t, "test", ` +version: "3" +services: + test: + image: nginx + deploy: + resources: + limits: + cpus: '4' + memory: 8192M + reservations: + cpus: '4' + memory: 8192M +`) + def = template.Resources["TestTaskDefinition"].(*ecs.TaskDefinition) + assert.Equal(t, def.Cpu, "4096") + assert.Equal(t, def.Memory, "8192") +} + func TestLoadBalancerTypeNetwork(t *testing.T) { template := convertYaml(t, "test", ` version: "3" diff --git a/ecs/pkg/amazon/backend/compatibility.go b/ecs/pkg/amazon/backend/compatibility.go index 85a00128a..35d58d836 100644 --- a/ecs/pkg/amazon/backend/compatibility.go +++ b/ecs/pkg/amazon/backend/compatibility.go @@ -16,6 +16,12 @@ var compatibleComposeAttributes = []string{ "services.depends_on", "services.deploy", "services.deploy.replicas", + "services.deploy.resources.limits", + "services.deploy.resources.limits.cpus", + "services.deploy.resources.limits.memory", + "services.deploy.resources.reservations", + "services.deploy.resources.reservations.cpus", + "services.deploy.resources.reservations.memory", "services.entrypoint", "services.environment", "service.image", diff --git a/ecs/pkg/amazon/backend/convert.go b/ecs/pkg/amazon/backend/convert.go index 6fdc0abc3..6bd66255d 100644 --- a/ecs/pkg/amazon/backend/convert.go +++ b/ecs/pkg/amazon/backend/convert.go @@ -21,6 +21,10 @@ func Convert(project *types.Project, service types.ServiceConfig) (*ecs.TaskDefi if err != nil { return nil, err } + _, memReservation, err := toContainerReservation(service) + if err != nil { + return nil, err + } credential := getRepoCredentials(service) // override resolve.conf search directive to also search .local @@ -60,6 +64,7 @@ func Convert(project *types.Project, service types.ServiceConfig) (*ecs.TaskDefi "awslogs-stream-prefix": project.Name, }, }, + MemoryReservation: memReservation, Name: service.Name, PortMappings: toPortMappings(service.Ports), Privileged: service.Privileged, @@ -111,6 +116,8 @@ func toSystemControls(sysctls types.Mapping) []ecs.TaskDefinition_SystemControl return sys } +const Mb = 1024 * 1024 + func toLimits(service types.ServiceConfig) (string, string, error) { // All possible cpu/mem values for Fargate cpuToMem := map[int64][]types.UnitBytes{ @@ -142,9 +149,9 @@ func toLimits(service types.ServiceConfig) (string, string, error) { } for cpu, mem := range cpuToMem { - if v <= cpu*1024*1024 { + if v <= cpu*Mb { for _, m := range mem { - if limits.MemoryBytes <= m*1024*1024 { + if limits.MemoryBytes <= m*Mb { cpuLimit = strconv.FormatInt(cpu, 10) memLimit = strconv.FormatInt(int64(m), 10) return cpuLimit, memLimit, nil @@ -155,6 +162,21 @@ func toLimits(service types.ServiceConfig) (string, string, error) { return "", "", fmt.Errorf("unable to find cpu/mem for the required resources") } +func toContainerReservation(service types.ServiceConfig) (string, int, error) { + cpuReservation := ".0" + memReservation := 0 + + if service.Deploy == nil { + return cpuReservation, memReservation, nil + } + + reservations := service.Deploy.Resources.Reservations + if reservations == nil { + return cpuReservation, memReservation, nil + } + return reservations.NanoCPUs, int(reservations.MemoryBytes / Mb), nil +} + func toRequiresCompatibilities(isolation string) []*string { if isolation == "" { return nil @@ -206,8 +228,6 @@ func toUlimits(ulimits map[string]*types.UlimitsConfig) []ecs.TaskDefinition_Uli return u } -const Mb = 1024 * 1024 - func toLinuxParameters(service types.ServiceConfig) *ecs.TaskDefinition_LinuxParameters { return &ecs.TaskDefinition_LinuxParameters{ Capabilities: toKernelCapabilities(service.CapAdd, service.CapDrop),