From f2430afa0652e4ea0322cc443f2827d86ae9493b Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 23 Sep 2020 10:04:19 +0200 Subject: [PATCH] Fix CPU limit computation targetting EC2 Signed-off-by: Nicolas De Loof --- ecs/cloudformation.go | 16 ++++++++++------ ecs/cloudformation_test.go | 16 ++++++++++++++++ ecs/convert.go | 9 ++++++++- ecs/ec2.go | 2 +- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/ecs/cloudformation.go b/ecs/cloudformation.go index 3fe1168ac..68630e1a9 100644 --- a/ecs/cloudformation.go +++ b/ecs/cloudformation.go @@ -222,10 +222,8 @@ func (b *ecsAPIService) convert(project *types.Project) (*cloudformation.Templat for _, port := range service.Ports { protocol := strings.ToUpper(port.Protocol) if getLoadBalancerType(project) == elbv2.LoadBalancerTypeEnumApplication { - protocol = elbv2.ProtocolEnumHttps - if port.Published == 80 { - protocol = elbv2.ProtocolEnumHttp - } + // we don't set Https as a certificate must be specified for HTTPS listeners + protocol = elbv2.ProtocolEnumHttp } if loadBalancerARN != "" { targetGroupName := createTargetGroup(project, service, port, template, protocol) @@ -254,9 +252,11 @@ func (b *ecsAPIService) convert(project *types.Project) (*cloudformation.Templat return nil, nil, err } + assignPublicIP := ecsapi.AssignPublicIpEnabled launchType := ecsapi.LaunchTypeFargate platformVersion := "1.4.0" // LATEST which is set to 1.3.0 (?) which doesn’t allow efs volumes. if requireEC2(service) { + assignPublicIP = ecsapi.AssignPublicIpDisabled launchType = ecsapi.LaunchTypeEc2 platformVersion = "" // The platform version must be null when specifying an EC2 launch type } @@ -277,7 +277,7 @@ func (b *ecsAPIService) convert(project *types.Project) (*cloudformation.Templat LoadBalancers: serviceLB, NetworkConfiguration: &ecs.Service_NetworkConfiguration{ AwsvpcConfiguration: &ecs.Service_AwsVpcConfiguration{ - AssignPublicIp: ecsapi.AssignPublicIpEnabled, + AssignPublicIp: assignPublicIP, SecurityGroups: serviceSecurityGroups, Subnets: []string{ cloudformation.Ref(parameterSubnet1Id), @@ -560,11 +560,15 @@ func convertNetwork(project *types.Project, net types.NetworkConfig, vpc string, for _, service := range project.Services { if _, ok := service.Networks[net.Name]; ok { for _, port := range service.Ports { + protocol := strings.ToUpper(port.Protocol) + if protocol == "" { + protocol = "-1" + } ingresses = append(ingresses, ec2.SecurityGroup_Ingress{ CidrIp: "0.0.0.0/0", Description: fmt.Sprintf("%s:%d/%s", service.Name, port.Target, port.Protocol), FromPort: int(port.Target), - IpProtocol: strings.ToUpper(port.Protocol), + IpProtocol: protocol, ToPort: int(port.Target), }) } diff --git a/ecs/cloudformation_test.go b/ecs/cloudformation_test.go index 1831ef6b1..d2d55ce7f 100644 --- a/ecs/cloudformation_test.go +++ b/ecs/cloudformation_test.go @@ -299,6 +299,22 @@ services: def = template.Resources["TestTaskDefinition"].(*ecs.TaskDefinition) assert.Equal(t, def.Cpu, "4000") assert.Equal(t, def.Memory, "792") + + template = convertYaml(t, ` +services: + test: + image: nginx + deploy: + resources: + reservations: + generic_resources: + - discrete_resource_spec: + kind: gpus + value: 2 +`) + def = template.Resources["TestTaskDefinition"].(*ecs.TaskDefinition) + assert.Equal(t, def.Cpu, "") + assert.Equal(t, def.Memory, "") } func TestTaskSizeConvertFailure(t *testing.T) { model := loadConfig(t, ` diff --git a/ecs/convert.go b/ecs/convert.go index e878e930d..a4ad823b5 100644 --- a/ecs/convert.go +++ b/ecs/convert.go @@ -329,7 +329,14 @@ func toLimits(service types.ServiceConfig) (string, string, error) { } if requireEC2(service) { // just return configured limits expressed in Mb and CPU units - return fmt.Sprint(cpu), fmt.Sprint(mem / miB), nil + var cpuLimit, memLimit string + if cpu > 0 { + cpuLimit = fmt.Sprint(cpu) + } + if mem > 0 { + memLimit = fmt.Sprint(mem / miB) + } + return cpuLimit, memLimit, nil } // All possible cpu/mem values for Fargate diff --git a/ecs/ec2.go b/ecs/ec2.go index cdaf58fd6..f6c3cc76e 100644 --- a/ecs/ec2.go +++ b/ecs/ec2.go @@ -1,5 +1,5 @@ /* - Copyright 2020 Docker, Inc. + Copyright 2020 Docker Compose CLI authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.