Skip to content

Commit

Permalink
refactor: wait for load balancer readiness using a private field
Browse files Browse the repository at this point in the history
This approach is more explicit than looking at the names of the target
groups, and using a private field is simpler.
  • Loading branch information
justinsb committed Jan 29, 2024
1 parent b84ab1e commit dd3de6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pkg/model/awsmodel/api_loadbalancer.go
Expand Up @@ -193,6 +193,12 @@ func (b *APILoadBalancerBuilder) Build(c *fi.CloudupModelBuilderContext) error {
Type: fi.PtrTo("network"),
}

// Wait for all load balancer components to be created (including network interfaces needed for NoneDNS).
// Limiting this to clusters using NoneDNS because load balancer creation is quite slow.
if b.Cluster.UsesNoneDNS() {
nlb.SetWaitForLoadBalancerReady(true)
}

clb = &awstasks.ClassicLoadBalancer{
Name: fi.PtrTo("api." + b.ClusterName()),
Lifecycle: b.Lifecycle,
Expand Down
30 changes: 16 additions & 14 deletions upup/pkg/fi/cloudup/awstasks/network_load_balancer.go
Expand Up @@ -76,6 +76,13 @@ type NetworkLoadBalancer struct {
// WellKnownServices indicates which services are supported by this resource.
// This field is internal and is not rendered to the cloud.
WellKnownServices []wellknownservices.WellKnownService

// waitForLoadBalancerReady controls whether we wait for the load balancer to be ready before completing the "Render" operation.
waitForLoadBalancerReady bool
}

func (e *NetworkLoadBalancer) SetWaitForLoadBalancerReady(v bool) {
e.waitForLoadBalancerReady = v
}

var _ fi.CompareWithID = &NetworkLoadBalancer{}
Expand Down Expand Up @@ -610,22 +617,17 @@ func (_ *NetworkLoadBalancer) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Ne
e.HostedZoneId = lb.CanonicalHostedZoneId
e.VPC = &VPC{ID: lb.VpcId}
loadBalancerArn = fi.ValueOf(lb.LoadBalancerArn)

}

// Wait for all load balancer components to be created (including network interfaces needed for NoneDNS).
// Limiting this to clusters using NoneDNS because load balancer creation is quite slow.
for _, tg := range e.TargetGroups {
if strings.HasPrefix(fi.ValueOf(tg.Name), "kops-controller") {
klog.Infof("Waiting for load balancer %q to be created...", loadBalancerName)
request := &elbv2.DescribeLoadBalancersInput{
Names: []*string{&loadBalancerName},
}
err := t.Cloud.ELBV2().WaitUntilLoadBalancerAvailable(request)
if err != nil {
return fmt.Errorf("error waiting for NLB %q: %w", loadBalancerName, err)
}
break
if e.waitForLoadBalancerReady {
klog.Infof("Waiting for load balancer %q to be created...", loadBalancerName)
request := &elbv2.DescribeLoadBalancersInput{
Names: []*string{&loadBalancerName},
}

err := t.Cloud.ELBV2().WaitUntilLoadBalancerAvailable(request)
if err != nil {
return fmt.Errorf("error waiting for NLB %q: %w", loadBalancerName, err)
}
}

Expand Down

0 comments on commit dd3de6f

Please sign in to comment.