mirror of
https://github.com/docker/compose.git
synced 2025-07-25 22:54:54 +02:00
allow user defined LB
Signed-off-by: aiordache <anca.iordache@docker.com> Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
fbb5bdac6e
commit
3194cc9b16
@ -25,10 +25,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ParameterClusterName = "ParameterClusterName"
|
ParameterClusterName = "ParameterClusterName"
|
||||||
ParameterVPCId = "ParameterVPCId"
|
ParameterVPCId = "ParameterVPCId"
|
||||||
ParameterSubnet1Id = "ParameterSubnet1Id"
|
ParameterSubnet1Id = "ParameterSubnet1Id"
|
||||||
ParameterSubnet2Id = "ParameterSubnet2Id"
|
ParameterSubnet2Id = "ParameterSubnet2Id"
|
||||||
|
ParameterLoadBalancerARN = "ParameterLoadBalancerARN"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Convert a compose project into a CloudFormation template
|
// Convert a compose project into a CloudFormation template
|
||||||
@ -66,6 +67,11 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
|
|||||||
Description: "SubnetId, for Availability Zone 2 in the region in your VPC",
|
Description: "SubnetId, for Availability Zone 2 in the region in your VPC",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template.Parameters[ParameterLoadBalancerARN] = cloudformation.Parameter{
|
||||||
|
Type: "String",
|
||||||
|
Description: "Name of the LoadBalancer to connect to (optional)",
|
||||||
|
}
|
||||||
|
|
||||||
// Create Cluster is `ParameterClusterName` parameter is not set
|
// Create Cluster is `ParameterClusterName` parameter is not set
|
||||||
template.Conditions["CreateCluster"] = cloudformation.Equals("", cloudformation.Ref(ParameterClusterName))
|
template.Conditions["CreateCluster"] = cloudformation.Equals("", cloudformation.Ref(ParameterClusterName))
|
||||||
|
|
||||||
@ -172,10 +178,19 @@ func (c client) Convert(project *compose.Project) (*cloudformation.Template, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c client) createLoadBalancer(project *compose.Project, template *cloudformation.Template) string {
|
func (c client) createLoadBalancer(project *compose.Project, template *cloudformation.Template) string {
|
||||||
loadBalancerName := fmt.Sprintf("%sLoadBalancer", strings.Title(project.Name))
|
|
||||||
template.Resources[loadBalancerName] = &elasticloadbalancingv2.LoadBalancer{
|
loadBalancerType := "network"
|
||||||
|
loadBalancerName := fmt.Sprintf(
|
||||||
|
"%s%sLB",
|
||||||
|
strings.Title(project.Name),
|
||||||
|
strings.ToUpper(loadBalancerType[0:1]),
|
||||||
|
)
|
||||||
|
// Create LoadBalancer if `ParameterLoadBalancerName` is not set
|
||||||
|
template.Conditions["CreateLoadBalancer"] = cloudformation.Equals("", cloudformation.Ref(ParameterLoadBalancerARN))
|
||||||
|
|
||||||
|
loadBalancer := &elasticloadbalancingv2.LoadBalancer{
|
||||||
Name: loadBalancerName,
|
Name: loadBalancerName,
|
||||||
Scheme: elbv2.LoadBalancerSchemeEnumInternetFacing,
|
Scheme: "internet-facing",
|
||||||
Subnets: []string{
|
Subnets: []string{
|
||||||
cloudformation.Ref(ParameterSubnet1Id),
|
cloudformation.Ref(ParameterSubnet1Id),
|
||||||
cloudformation.Ref(ParameterSubnet2Id),
|
cloudformation.Ref(ParameterSubnet2Id),
|
||||||
@ -186,12 +201,16 @@ func (c client) createLoadBalancer(project *compose.Project, template *cloudform
|
|||||||
Value: project.Name,
|
Value: project.Name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: elbv2.LoadBalancerTypeEnumNetwork,
|
Type: loadBalancerType,
|
||||||
|
AWSCloudFormationCondition: "CreateLoadBalancer",
|
||||||
}
|
}
|
||||||
return loadBalancerName
|
template.Resources[loadBalancerName] = loadBalancer
|
||||||
|
loadBalancerRef := 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, loadBalancerName string, protocol string) string {
|
func (c client) createListener(service types.ServiceConfig, port types.ServicePortConfig, template *cloudformation.Template, targetGroupName string, loadBalancerARN string, protocol string) string {
|
||||||
listenerName := fmt.Sprintf(
|
listenerName := fmt.Sprintf(
|
||||||
"%s%s%dListener",
|
"%s%s%dListener",
|
||||||
normalizeResourceName(service.Name),
|
normalizeResourceName(service.Name),
|
||||||
@ -213,7 +232,7 @@ func (c client) createListener(service types.ServiceConfig, port types.ServicePo
|
|||||||
Type: elbv2.ActionTypeEnumForward,
|
Type: elbv2.ActionTypeEnumForward,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
LoadBalancerArn: cloudformation.Ref(loadBalancerName),
|
LoadBalancerArn: loadBalancerARN,
|
||||||
Protocol: protocol,
|
Protocol: protocol,
|
||||||
Port: int(port.Published),
|
Port: int(port.Published),
|
||||||
}
|
}
|
||||||
|
@ -400,3 +400,25 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string
|
|||||||
}
|
}
|
||||||
return publicIPs, nil
|
return publicIPs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s sdk) LoadBalancerExists(ctx context.Context, name string) (bool, error) {
|
||||||
|
logrus.Debug("Check if cluster was already created: ", name)
|
||||||
|
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
|
||||||
|
Names: []*string{aws.String(name)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return len(lbs.LoadBalancers) > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s sdk) GetLoadBalancerARN(ctx context.Context, name string) (string, error) {
|
||||||
|
logrus.Debug("Check if cluster was already created: ", name)
|
||||||
|
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
|
||||||
|
Names: []*string{aws.String(name)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return *lbs.LoadBalancers[0].LoadBalancerArn, nil
|
||||||
|
}
|
||||||
|
@ -42,11 +42,17 @@ func (c *client) ComposeUp(ctx context.Context, project *compose.Project) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lb, err := c.GetLoadBalancer(ctx, project)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
parameters := map[string]string{
|
parameters := map[string]string{
|
||||||
ParameterClusterName: c.Cluster,
|
ParameterClusterName: c.Cluster,
|
||||||
ParameterVPCId: vpc,
|
ParameterVPCId: vpc,
|
||||||
ParameterSubnet1Id: subNets[0],
|
ParameterSubnet1Id: subNets[0],
|
||||||
ParameterSubnet2Id: subNets[1],
|
ParameterSubnet2Id: subNets[1],
|
||||||
|
ParameterLoadBalancerARN: lb,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.api.CreateStack(ctx, project.Name, template, parameters)
|
err = c.api.CreateStack(ctx, project.Name, template, parameters)
|
||||||
@ -77,6 +83,22 @@ func (c client) GetVPC(ctx context.Context, project *compose.Project) (string, e
|
|||||||
return defaultVPC, nil
|
return defaultVPC, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c client) GetLoadBalancer(ctx context.Context, project *compose.Project) (string, error) {
|
||||||
|
//check compose file for custom VPC selected
|
||||||
|
if lb, ok := project.Extras[ExtensionLB]; ok {
|
||||||
|
lbName := lb.(string)
|
||||||
|
ok, err := c.api.LoadBalancerExists(ctx, lbName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("Load Balancer does not exist: %s", lb)
|
||||||
|
}
|
||||||
|
return c.api.GetLoadBalancerARN(ctx, lbName)
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
type upAPI interface {
|
type upAPI interface {
|
||||||
waitAPI
|
waitAPI
|
||||||
GetDefaultVPC(ctx context.Context) (string, error)
|
GetDefaultVPC(ctx context.Context) (string, error)
|
||||||
@ -86,4 +108,7 @@ type upAPI interface {
|
|||||||
ClusterExists(ctx context.Context, name string) (bool, error)
|
ClusterExists(ctx context.Context, name string) (bool, error)
|
||||||
StackExists(ctx context.Context, name string) (bool, error)
|
StackExists(ctx context.Context, name string) (bool, error)
|
||||||
CreateStack(ctx context.Context, name string, template *cloudformation.Template, parameters map[string]string) error
|
CreateStack(ctx context.Context, name string, template *cloudformation.Template, parameters map[string]string) error
|
||||||
|
|
||||||
|
LoadBalancerExists(ctx context.Context, name string) (bool, error)
|
||||||
|
GetLoadBalancerARN(ctx context.Context, name string) (string, error)
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,5 @@ const (
|
|||||||
ExtensionSecurityGroup = "x-aws-securitygroup"
|
ExtensionSecurityGroup = "x-aws-securitygroup"
|
||||||
ExtensionVPC = "x-aws-vpc"
|
ExtensionVPC = "x-aws-vpc"
|
||||||
ExtensionPullCredentials = "x-aws-pull_credentials"
|
ExtensionPullCredentials = "x-aws-pull_credentials"
|
||||||
|
ExtensionLB = "x-aws-lb"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user