Browse Source

Fix CPU limit computation targetting EC2

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 years ago
parent
commit
f2430afa06
4 changed files with 35 additions and 8 deletions
  1. 10 6
      ecs/cloudformation.go
  2. 16 0
      ecs/cloudformation_test.go
  3. 8 1
      ecs/convert.go
  4. 1 1
      ecs/ec2.go

+ 10 - 6
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),
 					})
 				}

+ 16 - 0
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, `

+ 8 - 1
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

+ 1 - 1
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.