Browse Source

Create CloudMap private namespace and register services

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 5 years ago
parent
commit
0eab586106
3 changed files with 22 additions and 1 deletions
  1. 1 1
      ecs/Makefile
  2. 0 0
      ecs/cmd/main/main.go
  3. 21 0
      ecs/pkg/amazon/cloudformation.go

+ 1 - 1
ecs/Makefile

@@ -2,7 +2,7 @@ clean:
 	rm -rf dist/
 
 build:
-	go build -v -o dist/docker-ecs cmd/main.go
+	go build -v -o dist/docker-ecs cmd/main/main.go
 
 test: ## Run tests
 	go test ./... -v

+ 0 - 0
ecs/cmd/main.go → ecs/cmd/main/main.go


+ 21 - 0
ecs/pkg/amazon/cloudformation.go

@@ -13,6 +13,7 @@ import (
 	"github.com/awslabs/goformation/v4/cloudformation/ec2"
 	"github.com/awslabs/goformation/v4/cloudformation/ecs"
 	"github.com/awslabs/goformation/v4/cloudformation/iam"
+	cloudmap "github.com/awslabs/goformation/v4/cloudformation/servicediscovery"
 	"github.com/docker/ecs-plugin/pkg/compose"
 )
 
@@ -54,6 +55,13 @@ func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudfo
 		LogGroupName: logGroup,
 	}
 
+	// Private DNS namespace will allow DNS name for the services to be <service>.<project>.local
+	template.Resources["CloudMap"] = &cloudmap.PrivateDnsNamespace{
+		Description: fmt.Sprintf("Service Map for Docker Compose project %s", project.Name),
+		Name:        fmt.Sprintf("%s.local", project.Name),
+		Vpc:         vpc,
+	}
+
 	for _, service := range project.Services {
 		definition, err := Convert(project, service)
 		if err != nil {
@@ -89,6 +97,19 @@ func (c client) Convert(ctx context.Context, project *compose.Project) (*cloudfo
 			ServiceName:        service.Name,
 			TaskDefinition:     cloudformation.Ref(taskDefinition),
 		}
+
+		var healthCheck *cloudmap.Service_HealthCheckConfig
+		if service.HealthCheck != nil && !service.HealthCheck.Disable {
+			// FIXME ECS only support HTTP(s) health checks, while Docker only support CMD
+		}
+
+		serviceRegistration := fmt.Sprintf("%sServiceRegistration", service.Name)
+		template.Resources[serviceRegistration] = &cloudmap.Service{
+			Description:       fmt.Sprintf("%q registration in Service Map", service.Name),
+			HealthCheckConfig: healthCheck,
+			Name:              serviceRegistration,
+			NamespaceId:       cloudformation.Ref("CloudMap"),
+		}
 	}
 	return template, nil
 }