Introduct option to re-use LoadBalancer

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-04-16 16:18:06 +02:00
parent a44ee2a4ed
commit dd48cc4599
5 changed files with 39 additions and 15 deletions

View File

@ -76,7 +76,19 @@ func ComposeCommand(clusteropts *clusterOptions) *cobra.Command {
return cmd
}
type upOptions struct {
loadBalancerArn string
}
func (o upOptions) LoadBalancerArn() *string {
if o.loadBalancerArn == "" {
return nil
}
return &o.loadBalancerArn
}
func UpCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
opts := upOptions{}
cmd := &cobra.Command{
Use: "up",
RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
@ -84,22 +96,30 @@ func UpCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions)
if err != nil {
return err
}
return client.ComposeUp(project)
return client.ComposeUp(project, opts.LoadBalancerArn())
}),
}
cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
return cmd
}
func DownCommand(clusteropts *clusterOptions, opts *compose.ProjectOptions) *cobra.Command {
type downOptions struct {
KeepLoadBalancer bool
}
func DownCommand(clusteropts *clusterOptions, projectOpts *compose.ProjectOptions) *cobra.Command {
opts := downOptions{}
cmd := &cobra.Command{
Use: "down",
RunE: compose.WithProject(opts, func(project *compose.Project, args []string) error {
RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
client, err := amazon.NewClient(clusteropts.profile, clusteropts.cluster, clusteropts.region)
if err != nil {
return err
}
return client.ComposeDown(project)
return client.ComposeDown(project, opts.KeepLoadBalancer)
}),
}
cmd.Flags().BoolVar(&opts.KeepLoadBalancer, "keep-load-balancer", false, "Keep Load Balancer for further use")
return cmd
}

View File

@ -8,8 +8,8 @@ import (
"github.com/sirupsen/logrus"
)
func (c *client) ComposeDown(project *compose.Project) error {
err := c.DeleteLoadBalancer(project)
func (c *client) ComposeDown(project *compose.Project, keepLoadBalancer bool) error {
err := c.DeleteLoadBalancer(project, keepLoadBalancer)
if err != nil {
return err
}

View File

@ -31,7 +31,7 @@ func (c client) CreateLoadBalancer(project *compose.Project, subnets []*string)
return alb.LoadBalancers[0].LoadBalancerArn, nil
}
func (c client) DeleteLoadBalancer(project *compose.Project) error {
func (c client) DeleteLoadBalancer(project *compose.Project, keepLoadBalancer bool) error {
logrus.Debug("Delete Load Balancer")
// FIXME We can tag LoadBalancer but not search by tag ?
loadBalancer, err := c.ELB.DescribeLoadBalancers(&elbv2.DescribeLoadBalancersInput{
@ -52,7 +52,9 @@ func (c client) DeleteLoadBalancer(project *compose.Project) error {
return err
}
_, err = c.ELB.DeleteLoadBalancer(&elbv2.DeleteLoadBalancerInput{LoadBalancerArn: arn})
if !keepLoadBalancer {
_, err = c.ELB.DeleteLoadBalancer(&elbv2.DeleteLoadBalancerInput{LoadBalancerArn: arn})
}
return err
}

View File

@ -10,7 +10,7 @@ import (
"github.com/sirupsen/logrus"
)
func (c *client) ComposeUp(project *compose.Project) error {
func (c *client) ComposeUp(project *compose.Project, loadBalancerArn *string) error {
type mapping struct {
service *types.ServiceConfig
task *ecs.RegisterTaskDefinitionInput
@ -41,9 +41,11 @@ func (c *client) ComposeUp(project *compose.Project) error {
return err
}
loadBalancer, err := c.CreateLoadBalancer(project, subnets)
if err != nil {
return err
if loadBalancerArn == nil {
loadBalancerArn, err = c.CreateLoadBalancer(project, subnets)
if err != nil {
return err
}
}
logGroup, err := c.GetOrCreateLogGroup(project)
@ -65,7 +67,7 @@ func (c *client) ComposeUp(project *compose.Project) error {
TargetGroupArn: targetgroup,
})
err = c.CreateListener(port, loadBalancer, targetgroup)
err = c.CreateListener(port, loadBalancerArn, targetgroup)
if err != nil {
return err
}

View File

@ -1,6 +1,6 @@
package compose
type API interface {
ComposeUp(project *Project) error
ComposeDown(project *Project) error
ComposeUp(project *Project, loadBalancerArn *string) error
ComposeDown(project *Project, keepLoadBalancer bool) error
}