mirror of https://github.com/docker/compose.git
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:
parent
fc9b10fc91
commit
ae3101fe12
|
@ -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,69 +168,36 @@ 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(
|
||||||
template.Resources[targetGroupName] = &elasticloadbalancingv2.TargetGroup{
|
"%s%sLB",
|
||||||
Name: targetGroupName,
|
strings.Title(project.Name),
|
||||||
Port: int(port.Target),
|
strings.ToUpper(loadBalancerType[0:1]),
|
||||||
Protocol: protocolType,
|
)
|
||||||
Tags: []tags.Tag{
|
// create load baalncer if it doesn't exist
|
||||||
{
|
if _, ok := template.Resources[loadBalancerName]; !ok {
|
||||||
Key: ProjectTag,
|
|
||||||
Value: project.Name,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Key: ServiceTag,
|
|
||||||
Value: service.Name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
VpcId: cloudformation.Ref(ParameterVPCId),
|
|
||||||
TargetType: targetType,
|
|
||||||
}
|
|
||||||
|
|
||||||
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{
|
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{
|
||||||
Name: loadBalancerName,
|
Name: loadBalancerName,
|
||||||
Scheme: "internet-facing",
|
Scheme: "internet-facing",
|
||||||
SecurityGroups: lbSecGroups,
|
SecurityGroups: loadBalancerSecGroups,
|
||||||
Subnets: []string{
|
Subnets: []string{
|
||||||
cloudformation.Ref(ParameterSubnet1Id),
|
cloudformation.Ref(ParameterSubnet1Id),
|
||||||
cloudformation.Ref(ParameterSubnet2Id),
|
cloudformation.Ref(ParameterSubnet2Id),
|
||||||
|
@ -237,14 +207,36 @@ 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,
|
|
||||||
},
|
},
|
||||||
},
|
Type: loadBalancerType,
|
||||||
Type: lbType,
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
targetGroupName := fmt.Sprintf(
|
||||||
|
"%s%s%sTargetGroup",
|
||||||
|
normalizeResourceName(service.Name),
|
||||||
|
strings.ToUpper(port.Protocol),
|
||||||
|
string(port.Published),
|
||||||
|
)
|
||||||
|
template.Resources[targetGroupName] = &elasticloadbalancingv2.TargetGroup{
|
||||||
|
Name: targetGroupName,
|
||||||
|
Port: int(port.Target),
|
||||||
|
Protocol: protocolType,
|
||||||
|
Tags: []tags.Tag{
|
||||||
|
{
|
||||||
|
Key: ProjectTag,
|
||||||
|
Value: project.Name,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
VpcId: cloudformation.Ref(ParameterVPCId),
|
||||||
|
TargetType: targetType,
|
||||||
|
}
|
||||||
|
listenerName := fmt.Sprintf(
|
||||||
|
"%s%s%sListener",
|
||||||
|
normalizeResourceName(service.Name),
|
||||||
|
strings.ToUpper(port.Protocol),
|
||||||
|
string(port.Published),
|
||||||
|
)
|
||||||
|
dependsOn = append(dependsOn, listenerName)
|
||||||
template.Resources[listenerName] = &elasticloadbalancingv2.Listener{
|
template.Resources[listenerName] = &elasticloadbalancingv2.Listener{
|
||||||
DefaultActions: []elasticloadbalancingv2.Listener_Action{
|
DefaultActions: []elasticloadbalancingv2.Listener_Action{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue