From a798c95963d67883e3f0aa0c070dae24a910fac5 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 27 May 2020 14:34:17 +0200 Subject: [PATCH] Register services with a known port with SRV record see https://github.com/docker/docker_aws/issues/15#issuecomment-634357859 Signed-off-by: Nicolas De Loof --- ecs/pkg/amazon/cloudformation.go | 33 ++++++++++++------- .../simple-cloudformation-conversion.golden | 2 +- ...formation-with-overrides-conversion.golden | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/ecs/pkg/amazon/cloudformation.go b/ecs/pkg/amazon/cloudformation.go index eec6b854..48960592 100644 --- a/ecs/pkg/amazon/cloudformation.go +++ b/ecs/pkg/amazon/cloudformation.go @@ -131,18 +131,31 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err } serviceRegistration := fmt.Sprintf("%sServiceDiscoveryEntry", service.Name) + records := []cloudmap.Service_DnsRecord{ + { + TTL: 60, + Type: cloudmapapi.RecordTypeA, + }, + } + serviceRegistry := ecs.Service_ServiceRegistry{ + RegistryArn: cloudformation.GetAtt(serviceRegistration, "Arn"), + } + + if len(service.Ports) > 0 { + records = append(records, cloudmap.Service_DnsRecord{ + TTL: 60, + Type: cloudmapapi.RecordTypeSrv, + }) + serviceRegistry.Port = int(service.Ports[0].Target) + } + template.Resources[serviceRegistration] = &cloudmap.Service{ Description: fmt.Sprintf("%q service discovery entry in Cloud Map", service.Name), HealthCheckConfig: healthCheck, Name: service.Name, NamespaceId: cloudformation.Ref("CloudMap"), DnsConfig: &cloudmap.Service_DnsConfig{ - DnsRecords: []cloudmap.Service_DnsRecord{ - { - TTL: 300, - Type: cloudmapapi.RecordTypeA, - }, - }, + DnsRecords: records, RoutingPolicy: cloudmapapi.RoutingPolicyMultivalue, }, } @@ -169,12 +182,8 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err }, SchedulingStrategy: ecsapi.SchedulingStrategyReplica, ServiceName: service.Name, - ServiceRegistries: []ecs.Service_ServiceRegistry{ - { - RegistryArn: cloudformation.GetAtt(serviceRegistration, "Arn"), - }, - }, - TaskDefinition: cloudformation.Ref(taskDefinition), + ServiceRegistries: []ecs.Service_ServiceRegistry{serviceRegistry}, + TaskDefinition: cloudformation.Ref(taskDefinition), } } return template, nil diff --git a/ecs/pkg/amazon/testdata/simple/simple-cloudformation-conversion.golden b/ecs/pkg/amazon/testdata/simple/simple-cloudformation-conversion.golden index 0050bca0..868dcdd0 100644 --- a/ecs/pkg/amazon/testdata/simple/simple-cloudformation-conversion.golden +++ b/ecs/pkg/amazon/testdata/simple/simple-cloudformation-conversion.golden @@ -135,7 +135,7 @@ "DnsConfig": { "DnsRecords": [ { - "TTL": 300, + "TTL": 60, "Type": "A" } ], diff --git a/ecs/pkg/amazon/testdata/simple/simple-cloudformation-with-overrides-conversion.golden b/ecs/pkg/amazon/testdata/simple/simple-cloudformation-with-overrides-conversion.golden index 328d627a..fe41a67f 100644 --- a/ecs/pkg/amazon/testdata/simple/simple-cloudformation-with-overrides-conversion.golden +++ b/ecs/pkg/amazon/testdata/simple/simple-cloudformation-with-overrides-conversion.golden @@ -135,7 +135,7 @@ "DnsConfig": { "DnsRecords": [ { - "TTL": 300, + "TTL": 60, "Type": "A" } ],