Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lionello committed Jan 9, 2025
1 parent b3b154a commit 56d3730
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/pkg/cli/client/byoc/aws/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func prepareDomainDelegation(ctx context.Context, projectDomain string, r53Clien
term.Debugf("Zone %q not found, delegation set will be created", projectDomain)

// Case 1: The zone doesn't exist: we'll create/get a delegation set and let CD/Pulumi create the hosted zone
delegationSet, err = prepareDomainDelegationFromDelegationSet(ctx, r53Client)
delegationSet, err = getOrCreateDelegationSet(ctx, r53Client)
if err != nil {
return nil, "", err
}
} else {
// Case 2: Get the NS records for the existing subdomain zone
delegationSet, err = prepareDomainDelegationFromZone(ctx, zone, r53Client)
delegationSet, err = getOrCreateDelegationSetByZone(ctx, zone, r53Client)
if err != nil {
return nil, "", err
}
Expand All @@ -54,7 +54,7 @@ func prepareDomainDelegation(ctx context.Context, projectDomain string, r53Clien
return delegationSet.NameServers, delegationSetId, nil
}

func prepareDomainDelegationFromDelegationSet(ctx context.Context, r53Client aws.Route53API) (*types.DelegationSet, error) {
func getOrCreateDelegationSet(ctx context.Context, r53Client aws.Route53API) (*types.DelegationSet, error) {
// Avoid creating a new delegation set if one already exists
delegationSet, err := aws.GetDelegationSet(ctx, r53Client)
// Create a new delegation set if it doesn't exist
Expand All @@ -69,7 +69,7 @@ func prepareDomainDelegationFromDelegationSet(ctx context.Context, r53Client aws
return delegationSet, err
}

func prepareDomainDelegationFromZone(ctx context.Context, zone *types.HostedZone, r53Client aws.Route53API) (*types.DelegationSet, error) {
func getOrCreateDelegationSetByZone(ctx context.Context, zone *types.HostedZone, r53Client aws.Route53API) (*types.DelegationSet, error) {
projectDomain := dns.Normalize(*zone.Name)
nsServers, err := aws.ListResourceRecords(ctx, *zone.Id, projectDomain, types.RRTypeNs, r53Client)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions src/pkg/clouds/aws/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"context"
"errors"
"math/rand"
"time"

"github.com/DefangLabs/defang/src/pkg/dns"
Expand Down Expand Up @@ -50,17 +51,17 @@ func GetDelegationSetByZone(ctx context.Context, zoneId *string, r53 Route53API)
}

func GetDelegationSet(ctx context.Context, r53 Route53API) (*types.DelegationSet, error) {
params := &route53.ListReusableDelegationSetsInput{
MaxItems: ptr.Int32(1),
}
params := &route53.ListReusableDelegationSetsInput{}
resp, err := r53.ListReusableDelegationSets(ctx, params)
if err != nil {
return nil, err
}
if len(resp.DelegationSets) == 0 {
return nil, ErrNoDelegationSetFound
}
return &resp.DelegationSets[0], nil
// Return a random delegation set, to work around the 100 zones-per-delegation-set limit,
// because we can't easily tell how many zones are using each delegation set.
return &resp.DelegationSets[rand.Intn(len(resp.DelegationSets))], nil
}

func GetHostedZoneByName(ctx context.Context, domain string, r53 Route53API) (*types.HostedZone, error) {
Expand Down

0 comments on commit 56d3730

Please sign in to comment.