create unique load balancer per app and cleanup

Signed-off-by: aiordache <anca.iordache@docker.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
aiordache 2020-06-03 13:06:21 +02:00 committed by Nicolas De Loof
parent fc9b10fc91
commit ae3101fe12
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
1 changed files with 55 additions and 63 deletions

View File

@ -136,17 +136,15 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
} }
serviceRegistration := fmt.Sprintf("%sServiceDiscoveryEntry", normalizeResourceName(service.Name)) serviceRegistration := fmt.Sprintf("%sServiceDiscoveryEntry", normalizeResourceName(service.Name))
records := []cloudmap.Service_DnsRecord{
{
TTL: 60,
Type: cloudmapapi.RecordTypeA,
},
}
serviceRegistry := ecs.Service_ServiceRegistry{ serviceRegistry := ecs.Service_ServiceRegistry{
RegistryArn: cloudformation.GetAtt(serviceRegistration, "Arn"), RegistryArn: cloudformation.GetAtt(serviceRegistration, "Arn"),
} }
loadBalancers := []ecs.Service_LoadBalancer{} serviceSecurityGroups := []string{}
for net := range service.Networks {
logicalName := networkResourceName(project, net)
serviceSecurityGroups = append(serviceSecurityGroups, cloudformation.Ref(logicalName))
}
template.Resources[serviceRegistration] = &cloudmap.Service{ template.Resources[serviceRegistration] = &cloudmap.Service{
Description: fmt.Sprintf("%q service discovery entry in Cloud Map", service.Name), Description: fmt.Sprintf("%q service discovery entry in Cloud Map", service.Name),
@ -154,7 +152,12 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
Name: service.Name, Name: service.Name,
NamespaceId: cloudformation.Ref("CloudMap"), NamespaceId: cloudformation.Ref("CloudMap"),
DnsConfig: &cloudmap.Service_DnsConfig{ DnsConfig: &cloudmap.Service_DnsConfig{
DnsRecords: records, DnsRecords: []cloudmap.Service_DnsRecord{
{
TTL: 60,
Type: cloudmapapi.RecordTypeA,
},
},
RoutingPolicy: cloudmapapi.RoutingPolicyMultivalue, RoutingPolicy: cloudmapapi.RoutingPolicyMultivalue,
}, },
} }
@ -165,47 +168,55 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
} }
dependsOn := []string{} dependsOn := []string{}
loadBalancers := []ecs.Service_LoadBalancer{}
if len(service.Ports) > 0 { if len(service.Ports) > 0 {
records = append(records, cloudmap.Service_DnsRecord{
TTL: 60,
Type: cloudmapapi.RecordTypeSrv,
})
//serviceRegistry.Port = int(service.Ports[0].Target)
// add targetgroup for each published port
for _, port := range service.Ports { for _, port := range service.Ports {
targetGroupName := fmt.Sprintf( loadBalancerType := "network"
"%s%s%sTargetGroup",
normalizeResourceName(service.Name),
strings.ToUpper(port.Protocol),
string(port.Published),
)
listenerName := fmt.Sprintf(
"%s%s%sListener",
normalizeResourceName(service.Name),
strings.ToUpper(port.Protocol),
string(port.Published),
)
loadBalancerName := fmt.Sprintf(
"%s%s%sLoadBalancer",
normalizeResourceName(service.Name),
strings.ToUpper(port.Protocol),
string(port.Published),
)
dependsOn = append(dependsOn, listenerName)
lbType := "network"
lbSecGroups := []string{}
protocolType := strings.ToUpper(port.Protocol) protocolType := strings.ToUpper(port.Protocol)
targetType := elbv2.TargetTypeEnumInstance targetType := elbv2.TargetTypeEnumInstance
loadBalancerSecGroups := []string{}
if port.Published == 80 || port.Published == 443 { if port.Published == 80 || port.Published == 443 {
lbType = "application" loadBalancerType = "application"
lbSecGroups = serviceSecurityGroups loadBalancerSecGroups = serviceSecurityGroups
protocolType = "HTTPS" protocolType = "HTTPS"
targetType = elbv2.TargetTypeEnumIp targetType = elbv2.TargetTypeEnumIp
if port.Published == 80 { if port.Published == 80 {
protocolType = "HTTP" protocolType = "HTTP"
} }
} }
loadBalancerName := fmt.Sprintf(
"%s%sLB",
strings.Title(project.Name),
strings.ToUpper(loadBalancerType[0:1]),
)
// create load baalncer if it doesn't exist
if _, ok := template.Resources[loadBalancerName]; !ok {
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{
Name: loadBalancerName,
Scheme: "internet-facing",
SecurityGroups: loadBalancerSecGroups,
Subnets: []string{
cloudformation.Ref(ParameterSubnet1Id),
cloudformation.Ref(ParameterSubnet2Id),
},
Tags: []tags.Tag{
{
Key: ProjectTag,
Value: project.Name,
},
},
Type: loadBalancerType,
}
}
targetGroupName := fmt.Sprintf(
"%s%s%sTargetGroup",
normalizeResourceName(service.Name),
strings.ToUpper(port.Protocol),
string(port.Published),
)
template.Resources[targetGroupName] = &elasticloadbalancingv2.TargetGroup{ template.Resources[targetGroupName] = &elasticloadbalancingv2.TargetGroup{
Name: targetGroupName, Name: targetGroupName,
Port: int(port.Target), Port: int(port.Target),
@ -215,36 +226,17 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
Key: ProjectTag, Key: ProjectTag,
Value: project.Name, Value: project.Name,
}, },
{
Key: ServiceTag,
Value: service.Name,
},
}, },
VpcId: cloudformation.Ref(ParameterVPCId), VpcId: cloudformation.Ref(ParameterVPCId),
TargetType: targetType, TargetType: targetType,
} }
listenerName := fmt.Sprintf(
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{ "%s%s%sListener",
Name: loadBalancerName, normalizeResourceName(service.Name),
Scheme: "internet-facing", strings.ToUpper(port.Protocol),
SecurityGroups: lbSecGroups, string(port.Published),
Subnets: []string{ )
cloudformation.Ref(ParameterSubnet1Id), dependsOn = append(dependsOn, listenerName)
cloudformation.Ref(ParameterSubnet2Id),
},
Tags: []tags.Tag{
{
Key: ProjectTag,
Value: project.Name,
},
{
Key: ServiceTag,
Value: service.Name,
},
},
Type: lbType,
}
template.Resources[listenerName] = &elasticloadbalancingv2.Listener{ template.Resources[listenerName] = &elasticloadbalancingv2.Listener{
DefaultActions: []elasticloadbalancingv2.Listener_Action{ DefaultActions: []elasticloadbalancingv2.Listener_Action{
{ {