Skip to content

Commit

Permalink
aws: delete CLBs after migration to NLB
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Nov 5, 2022
1 parent cdd6fe3 commit 66eb76a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/model/awsmodel/api_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (b *APILoadBalancerBuilder) Build(c *fi.ModelBuilderContext) error {
Lifecycle: b.Lifecycle,

LoadBalancerName: fi.String(loadBalancerName),
CLBName: fi.String("api." + b.ClusterName()),
SubnetMappings: nlbSubnetMappings,
Listeners: nlbListeners,
TargetGroups: make([]*awstasks.TargetGroup, 0),
Expand Down
1 change: 1 addition & 0 deletions pkg/model/awsmodel/bastion.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func (b *BastionModelBuilder) Build(c *fi.ModelBuilderContext) error {
Lifecycle: b.Lifecycle,

LoadBalancerName: fi.String(loadBalancerName),
CLBName: fi.String("bastion." + b.ClusterName()),
SubnetMappings: nlbSubnetMappings,
Listeners: nlbListeners,
TargetGroups: make([]*awstasks.TargetGroup, 0),
Expand Down
65 changes: 65 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/network_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/route53"
"k8s.io/klog/v2"
Expand All @@ -48,6 +49,7 @@ type NetworkLoadBalancer struct {
// (NLB is restricted as to names, so we have limited choices!)
// We use the Name tag to find the existing NLB.
LoadBalancerName *string
CLBName *string

DNSName *string
HostedZoneId *string
Expand All @@ -74,6 +76,7 @@ type NetworkLoadBalancer struct {

var _ fi.CompareWithID = &NetworkLoadBalancer{}
var _ fi.TaskNormalize = &NetworkLoadBalancer{}
var _ fi.ProducesDeletions = &NetworkLoadBalancer{}

func (e *NetworkLoadBalancer) CompareWithID() *string {
return e.Name
Expand Down Expand Up @@ -254,6 +257,7 @@ func (e *NetworkLoadBalancer) Find(c *fi.Context) (*NetworkLoadBalancer, error)

actual := &NetworkLoadBalancer{}
actual.Name = e.Name
actual.CLBName = e.CLBName
actual.LoadBalancerName = lb.LoadBalancerName
actual.DNSName = lb.DNSName
actual.HostedZoneId = lb.CanonicalHostedZoneId // CanonicalHostedZoneNameID
Expand Down Expand Up @@ -937,3 +941,64 @@ func (e *NetworkLoadBalancer) CloudformationAttrCanonicalHostedZoneNameID() *clo
func (e *NetworkLoadBalancer) CloudformationAttrDNSName() *cloudformation.Literal {
return cloudformation.GetAtt("AWS::ElasticLoadBalancingV2::LoadBalancer", *e.Name, "DNSName")
}

// FindDeletions schedules deletion of the corresponding legacy classic load balancer when it no longer has targets.
func (e *NetworkLoadBalancer) FindDeletions(context *fi.Context) ([]fi.Deletion, error) {
if e.CLBName == nil {
return nil, nil
}

cloud := context.Cloud.(awsup.AWSCloud)

lb, err := cloud.FindELBByNameTag(fi.StringValue(e.CLBName))
if err != nil {
return nil, err
}
if lb == nil {
return nil, nil
}

// Testing shows that the instances are deregistered immediately after the apply_cluster.
// TODO: Figure out how to delay deregistration until instances are terminated.
//if len(lb.Instances) > 0 {
// klog.V(2).Infof("CLB %s has targets; not scheduling deletion", *lb.LoadBalancerName)
// return nil, nil
//}

actual := &deleteClassicLoadBalancer{}
actual.LoadBalancerName = lb.LoadBalancerName

klog.V(4).Infof("Found CLB %+v", actual)

return []fi.Deletion{actual}, nil
}

type deleteClassicLoadBalancer struct {
// LoadBalancerName is the name in ELB, possibly different from our name
// (ELB is restricted as to names, so we have limited choices!)
LoadBalancerName *string
}

func (d deleteClassicLoadBalancer) Delete(t fi.Target) error {
awsTarget, ok := t.(*awsup.AWSAPITarget)
if !ok {
return fmt.Errorf("unexpected target type for deletion: %T", t)
}

_, err := awsTarget.Cloud.ELB().DeleteLoadBalancer(&elb.DeleteLoadBalancerInput{
LoadBalancerName: d.LoadBalancerName,
})
if err != nil {
return fmt.Errorf("deleting classic LoadBalancer: %w", err)
}

return nil
}

func (d deleteClassicLoadBalancer) TaskName() string {
return "ClassicLoadBalancer"
}

func (d deleteClassicLoadBalancer) Item() string {
return *d.LoadBalancerName
}

0 comments on commit 66eb76a

Please sign in to comment.