1
0
Эх сурвалжийг харах

Compute resource names to avoid unsupported characters

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 жил өмнө
parent
commit
564c369c3e

+ 11 - 6
ecs/pkg/amazon/cloudformation.go

@@ -2,6 +2,7 @@ package amazon
 
 import (
 	"fmt"
+	"regexp"
 	"strings"
 
 	"github.com/sirupsen/logrus"
@@ -99,7 +100,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
 			return nil, err
 		}
 
-		taskExecutionRole := fmt.Sprintf("%sTaskExecutionRole", service.Name)
+		taskExecutionRole := fmt.Sprintf("%sTaskExecutionRole", normalizeResourceName(service.Name))
 		policy, err := c.getPolicy(definition)
 		if err != nil {
 			return nil, err
@@ -114,7 +115,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
 		}
 		definition.ExecutionRoleArn = cloudformation.Ref(taskExecutionRole)
 
-		taskDefinition := fmt.Sprintf("%sTaskDefinition", service.Name)
+		taskDefinition := fmt.Sprintf("%sTaskDefinition", normalizeResourceName(service.Name))
 		template.Resources[taskExecutionRole] = &iam.Role{
 			AssumeRolePolicyDocument: assumeRolePolicyDocument,
 			Policies:                 rolePolicies,
@@ -130,7 +131,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
 			// FIXME ECS only support HTTP(s) health checks, while Docker only support CMD
 		}
 
-		serviceRegistration := fmt.Sprintf("%sServiceDiscoveryEntry", service.Name)
+		serviceRegistration := fmt.Sprintf("%sServiceDiscoveryEntry", normalizeResourceName(service.Name))
 		records := []cloudmap.Service_DnsRecord{
 			{
 				TTL:  60,
@@ -166,7 +167,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
 			serviceSecurityGroups = append(serviceSecurityGroups, cloudformation.Ref(logicalName))
 		}
 
-		template.Resources[fmt.Sprintf("%sService", service.Name)] = &ecs.Service{
+		template.Resources[fmt.Sprintf("%sService", normalizeResourceName(service.Name))] = &ecs.Service{
 			Cluster:      cluster,
 			DesiredCount: 1,
 			LaunchType:   ecsapi.LaunchTypeFargate,
@@ -193,7 +194,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
 					Value: service.Name,
 				},
 			},
-			TaskDefinition: cloudformation.Ref(taskDefinition),
+			TaskDefinition: cloudformation.Ref(normalizeResourceName(taskDefinition)),
 		}
 	}
 	return template, nil
@@ -236,7 +237,11 @@ func convertNetwork(project *compose.Project, net string, vpc string) (string, c
 }
 
 func networkResourceName(project *compose.Project, network string) string {
-	return fmt.Sprintf("%s%sNetwork", project.Name, strings.Title(network))
+	return fmt.Sprintf("%s%sNetwork", normalizeResourceName(project.Name), normalizeResourceName(network))
+}
+
+func normalizeResourceName(s string) string {
+	return strings.Title(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(s, ""))
 }
 
 func (c client) getPolicy(taskDef *ecs.TaskDefinition) (*PolicyDocument, error) {

+ 1 - 1
ecs/pkg/amazon/convert.go

@@ -57,7 +57,7 @@ func Convert(project *compose.Project, service types.ServiceConfig) (*ecs.TaskDe
 					Options: map[string]string{
 						"awslogs-region":        cloudformation.Ref("AWS::Region"),
 						"awslogs-group":         cloudformation.Ref("LogGroup"),
-						"awslogs-stream-prefix": service.Name,
+						"awslogs-stream-prefix": project.Name,
 					},
 				},
 				Name:                   service.Name,

+ 28 - 28
ecs/pkg/amazon/testdata/simple/simple-cloudformation-conversion.golden

@@ -58,27 +58,7 @@
       },
       "Type": "AWS::Logs::LogGroup"
     },
-    "TestSimpleConvertDefaultNetwork": {
-      "Properties": {
-        "GroupDescription": "TestSimpleConvert default Security Group",
-        "GroupName": "TestSimpleConvertDefaultNetwork",
-        "Tags": [
-          {
-            "Key": "com.docker.compose.project",
-            "Value": "TestSimpleConvert"
-          },
-          {
-            "Key": "com.docker.compose.network",
-            "Value": "default"
-          }
-        ],
-        "VpcId": {
-          "Ref": "ParameterVPCId"
-        }
-      },
-      "Type": "AWS::EC2::SecurityGroup"
-    },
-    "simpleService": {
+    "SimpleService": {
       "Properties": {
         "Cluster": {
           "Fn::If": [
@@ -117,7 +97,7 @@
           {
             "RegistryArn": {
               "Fn::GetAtt": [
-                "simpleServiceDiscoveryEntry",
+                "SimpleServiceDiscoveryEntry",
                 "Arn"
               ]
             }
@@ -134,12 +114,12 @@
           }
         ],
         "TaskDefinition": {
-          "Ref": "simpleTaskDefinition"
+          "Ref": "SimpleTaskDefinition"
         }
       },
       "Type": "AWS::ECS::Service"
     },
-    "simpleServiceDiscoveryEntry": {
+    "SimpleServiceDiscoveryEntry": {
       "Properties": {
         "Description": "\"simple\" service discovery entry in Cloud Map",
         "DnsConfig": {
@@ -158,7 +138,7 @@
       },
       "Type": "AWS::ServiceDiscovery::Service"
     },
-    "simpleTaskDefinition": {
+    "SimpleTaskDefinition": {
       "Properties": {
         "ContainerDefinitions": [
           {
@@ -191,7 +171,7 @@
                 "awslogs-region": {
                   "Ref": "AWS::Region"
                 },
-                "awslogs-stream-prefix": "simple"
+                "awslogs-stream-prefix": "TestSimpleConvert"
               }
             },
             "Name": "simple"
@@ -199,7 +179,7 @@
         ],
         "Cpu": "256",
         "ExecutionRoleArn": {
-          "Ref": "simpleTaskExecutionRole"
+          "Ref": "SimpleTaskExecutionRole"
         },
         "Family": "TestSimpleConvert-simple",
         "Memory": "512",
@@ -210,7 +190,7 @@
       },
       "Type": "AWS::ECS::TaskDefinition"
     },
-    "simpleTaskExecutionRole": {
+    "SimpleTaskExecutionRole": {
       "Properties": {
         "AssumeRolePolicyDocument": {
           "Statement": [
@@ -232,6 +212,26 @@
         ]
       },
       "Type": "AWS::IAM::Role"
+    },
+    "TestSimpleConvertDefaultNetwork": {
+      "Properties": {
+        "GroupDescription": "TestSimpleConvert default Security Group",
+        "GroupName": "TestSimpleConvertDefaultNetwork",
+        "Tags": [
+          {
+            "Key": "com.docker.compose.project",
+            "Value": "TestSimpleConvert"
+          },
+          {
+            "Key": "com.docker.compose.network",
+            "Value": "default"
+          }
+        ],
+        "VpcId": {
+          "Ref": "ParameterVPCId"
+        }
+      },
+      "Type": "AWS::EC2::SecurityGroup"
     }
   }
 }

+ 28 - 28
ecs/pkg/amazon/testdata/simple/simple-cloudformation-with-overrides-conversion.golden

@@ -58,27 +58,7 @@
       },
       "Type": "AWS::Logs::LogGroup"
     },
-    "TestSimpleWithOverridesDefaultNetwork": {
-      "Properties": {
-        "GroupDescription": "TestSimpleWithOverrides default Security Group",
-        "GroupName": "TestSimpleWithOverridesDefaultNetwork",
-        "Tags": [
-          {
-            "Key": "com.docker.compose.project",
-            "Value": "TestSimpleWithOverrides"
-          },
-          {
-            "Key": "com.docker.compose.network",
-            "Value": "default"
-          }
-        ],
-        "VpcId": {
-          "Ref": "ParameterVPCId"
-        }
-      },
-      "Type": "AWS::EC2::SecurityGroup"
-    },
-    "simpleService": {
+    "SimpleService": {
       "Properties": {
         "Cluster": {
           "Fn::If": [
@@ -117,7 +97,7 @@
           {
             "RegistryArn": {
               "Fn::GetAtt": [
-                "simpleServiceDiscoveryEntry",
+                "SimpleServiceDiscoveryEntry",
                 "Arn"
               ]
             }
@@ -134,12 +114,12 @@
           }
         ],
         "TaskDefinition": {
-          "Ref": "simpleTaskDefinition"
+          "Ref": "SimpleTaskDefinition"
         }
       },
       "Type": "AWS::ECS::Service"
     },
-    "simpleServiceDiscoveryEntry": {
+    "SimpleServiceDiscoveryEntry": {
       "Properties": {
         "Description": "\"simple\" service discovery entry in Cloud Map",
         "DnsConfig": {
@@ -158,7 +138,7 @@
       },
       "Type": "AWS::ServiceDiscovery::Service"
     },
-    "simpleTaskDefinition": {
+    "SimpleTaskDefinition": {
       "Properties": {
         "ContainerDefinitions": [
           {
@@ -191,7 +171,7 @@
                 "awslogs-region": {
                   "Ref": "AWS::Region"
                 },
-                "awslogs-stream-prefix": "simple"
+                "awslogs-stream-prefix": "TestSimpleWithOverrides"
               }
             },
             "Name": "simple"
@@ -199,7 +179,7 @@
         ],
         "Cpu": "256",
         "ExecutionRoleArn": {
-          "Ref": "simpleTaskExecutionRole"
+          "Ref": "SimpleTaskExecutionRole"
         },
         "Family": "TestSimpleWithOverrides-simple",
         "Memory": "512",
@@ -210,7 +190,7 @@
       },
       "Type": "AWS::ECS::TaskDefinition"
     },
-    "simpleTaskExecutionRole": {
+    "SimpleTaskExecutionRole": {
       "Properties": {
         "AssumeRolePolicyDocument": {
           "Statement": [
@@ -232,6 +212,26 @@
         ]
       },
       "Type": "AWS::IAM::Role"
+    },
+    "TestSimpleWithOverridesDefaultNetwork": {
+      "Properties": {
+        "GroupDescription": "TestSimpleWithOverrides default Security Group",
+        "GroupName": "TestSimpleWithOverridesDefaultNetwork",
+        "Tags": [
+          {
+            "Key": "com.docker.compose.project",
+            "Value": "TestSimpleWithOverrides"
+          },
+          {
+            "Key": "com.docker.compose.network",
+            "Value": "default"
+          }
+        ],
+        "VpcId": {
+          "Ref": "ParameterVPCId"
+        }
+      },
+      "Type": "AWS::EC2::SecurityGroup"
     }
   }
 }