create different methods to get lb type and security groups

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-05 15:14:50 +02:00 committed by Nicolas De Loof
parent 45dc8eda80
commit c0f1a8bf18
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E

View File

@ -90,8 +90,7 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
// Private DNS namespace will allow DNS name for the services to be <service>.<project>.local // Private DNS namespace will allow DNS name for the services to be <service>.<project>.local
c.createCloudMap(project, template) c.createCloudMap(project, template)
loadBalancerType, albSecurityGroups := c.getLoadBalancerType(project, networks) loadBalancerARN := c.createLoadBalancer(project, template)
loadBalancer := c.createLoadBalancer(project, template, loadBalancerType, albSecurityGroups)
for _, service := range project.Services { for _, service := range project.Services {
definition, err := Convert(project, service) definition, err := Convert(project, service)
@ -125,14 +124,14 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
if len(service.Ports) > 0 { if len(service.Ports) > 0 {
for _, port := range service.Ports { for _, port := range service.Ports {
protocol := strings.ToUpper(port.Protocol) protocol := strings.ToUpper(port.Protocol)
if loadBalancerType == elbv2.LoadBalancerTypeEnumApplication { if c.getLoadBalancerType(project) == elbv2.LoadBalancerTypeEnumApplication {
protocol = elbv2.ProtocolEnumHttps protocol = elbv2.ProtocolEnumHttps
if port.Published == 80 { if port.Published == 80 {
protocol = elbv2.ProtocolEnumHttp protocol = elbv2.ProtocolEnumHttp
} }
} }
targetGroupName := c.createTargetGroup(project, service, port, template, protocol) targetGroupName := c.createTargetGroup(project, service, port, template, protocol)
listenerName := c.createListener(service, port, template, targetGroupName, loadBalancer, protocol) listenerName := c.createListener(service, port, template, targetGroupName, loadBalancerARN, protocol)
dependsOn = append(dependsOn, listenerName) dependsOn = append(dependsOn, listenerName)
serviceLB = append(serviceLB, ecs.Service_LoadBalancer{ serviceLB = append(serviceLB, ecs.Service_LoadBalancer{
ContainerName: service.Name, ContainerName: service.Name,
@ -185,30 +184,39 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
return template, nil return template, nil
} }
func (c client) getLoadBalancerType(project *compose.Project, networks map[string]string) (string, []string) { func (c client) getLoadBalancerType(project *compose.Project) string {
for _, service := range project.Services { for _, service := range project.Services {
for _, port := range service.Ports { for _, port := range service.Ports {
if port.Published != 80 && port.Published != 443 { if port.Published != 80 && port.Published != 443 {
return elbv2.LoadBalancerTypeEnumNetwork, []string{} return elbv2.LoadBalancerTypeEnumNetwork
} }
} }
} }
return elbv2.LoadBalancerTypeEnumApplication
albSecurityGroups := []string{}
for _, network := range project.Networks {
if !network.Internal {
albSecurityGroups = append(albSecurityGroups, networks[network.Name])
}
}
albSecurityGroups = uniqueStrings(albSecurityGroups)
return elbv2.LoadBalancerTypeEnumApplication, albSecurityGroups
} }
func (c client) createLoadBalancer(project *compose.Project, template *cloudformation.Template, loadBalancerType string, securityGroups []string) string { func (c client) getLoadBalancerSecurityGroups(project *compose.Project, template *cloudformation.Template) []string {
securityGroups := []string{}
for _, network := range project.Networks {
if !network.Internal {
net := convertNetwork(project, network, cloudformation.Ref(ParameterVPCId), template)
securityGroups = append(securityGroups, net)
}
}
return uniqueStrings(securityGroups)
}
func (c client) createLoadBalancer(project *compose.Project, template *cloudformation.Template) string {
loadBalancerName := fmt.Sprintf("%sLoadBalancer", strings.Title(project.Name)) loadBalancerName := fmt.Sprintf("%sLoadBalancer", strings.Title(project.Name))
// Create LoadBalancer if `ParameterLoadBalancerName` is not set // Create LoadBalancer if `ParameterLoadBalancerName` is not set
template.Conditions["CreateLoadBalancer"] = cloudformation.Equals("", cloudformation.Ref(ParameterLoadBalancerARN)) template.Conditions["CreateLoadBalancer"] = cloudformation.Equals("", cloudformation.Ref(ParameterLoadBalancerARN))
loadBalancerType := c.getLoadBalancerType(project)
securityGroups := []string{}
if loadBalancerType == elbv2.LoadBalancerTypeEnumApplication {
securityGroups = c.getLoadBalancerSecurityGroups(project, template)
}
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{ template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{
Name: loadBalancerName, Name: loadBalancerName,
Scheme: elbv2.LoadBalancerSchemeEnumInternetFacing, Scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
@ -226,9 +234,7 @@ func (c client) createLoadBalancer(project *compose.Project, template *cloudform
Type: loadBalancerType, Type: loadBalancerType,
AWSCloudFormationCondition: "CreateLoadBalancer", AWSCloudFormationCondition: "CreateLoadBalancer",
} }
loadBalancerRef := cloudformation.If("CreateLoadBalancer", cloudformation.Ref(loadBalancerName), cloudformation.Ref(ParameterLoadBalancerARN)) return cloudformation.If("CreateLoadBalancer", cloudformation.Ref(loadBalancerName), cloudformation.Ref(ParameterLoadBalancerARN))
return loadBalancerRef
} }
func (c client) createListener(service types.ServiceConfig, port types.ServicePortConfig, template *cloudformation.Template, targetGroupName string, loadBalancerARN string, protocol string) string { func (c client) createListener(service types.ServiceConfig, port types.ServicePortConfig, template *cloudformation.Template, targetGroupName string, loadBalancerARN string, protocol string) string {