瀏覽代碼

Convert services into TaskDefinition before creating resources

close #6

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 年之前
父節點
當前提交
17f3ff9db1
共有 2 個文件被更改,包括 20 次插入9 次删除
  1. 1 1
      ecs/pkg/amazon/roles.go
  2. 19 8
      ecs/pkg/amazon/up.go

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

@@ -13,7 +13,7 @@ const ECSTaskExecutionPolicy = "arn:aws:iam::aws:policy/service-role/AmazonECSTa
 var defaultTaskExecutionRole *string
 
 // GetEcsTaskExecutionRole retrieve the role ARN to apply for task execution
-func (c client) GetEcsTaskExecutionRole(spec types.ServiceConfig) (*string, error) {
+func (c client) GetEcsTaskExecutionRole(spec *types.ServiceConfig) (*string, error) {
 	if arn, ok := spec.Extras["x-ecs-TaskExecutionRole"]; ok {
 		s := arn.(string)
 		return &s, nil

+ 19 - 8
ecs/pkg/amazon/up.go

@@ -10,6 +10,22 @@ import (
 )
 
 func (c *client) ComposeUp(project *compose.Project) error {
+	type mapping struct {
+		service *types.ServiceConfig
+		task *ecs.RegisterTaskDefinitionInput
+	}
+	mappings := []mapping{}
+	for _, service := range project.Services {
+		task, err := convert.Convert(project, service)
+		if err != nil {
+			return err
+		}
+		mappings = append(mappings, mapping{
+			service: &service,
+			task:    task,
+		})
+	}
+
 	vpc, err := c.GetDefaultVPC()
 	if err != nil {
 		return err
@@ -29,8 +45,8 @@ func (c *client) ComposeUp(project *compose.Project) error {
 		return err
 	}
 
-	for _, service := range project.Services {
-		_, err = c.CreateService(project, service, securityGroup, subnets, logGroup)
+	for _, mapping := range mappings {
+		_, err = c.CreateService(project, mapping.service, mapping.task, securityGroup, subnets, logGroup)
 		if err != nil {
 			return err
 		}
@@ -38,12 +54,7 @@ func (c *client) ComposeUp(project *compose.Project) error {
 	return nil
 }
 
-func (c *client) CreateService(project *compose.Project, service types.ServiceConfig, securityGroup *string, subnets []*string, logGroup *string) (*string, error) {
-	task, err := convert.Convert(project, service)
-	if err != nil {
-		return nil, err
-	}
-
+func (c *client) CreateService(project *compose.Project, service *types.ServiceConfig, task *ecs.RegisterTaskDefinitionInput, securityGroup *string, subnets []*string, logGroup *string) (*string, error) {
 	role, err := c.GetEcsTaskExecutionRole(service)
 	if err != nil {
 		return nil, err