Skip to content

Commit

Permalink
Fix finding NS record of the DNS zone (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDog authored Feb 7, 2024
1 parent bb28fe5 commit eba8338
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix finding NS record of the DNS zone. This only happens in rare cases.

## [0.14.4] - 2024-01-31

### Changed
Expand Down
20 changes: 13 additions & 7 deletions pkg/aws/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ func (r *Route53) DeleteHostedZone(ctx context.Context, logger logr.Logger, zone
return nil
}

func (r *Route53) GetHostedZoneNSRecords(ctx context.Context, logger logr.Logger, zoneId string) (*resolver.DNSRecord, error) {
logger = logger.WithValues("zoneId", zoneId)
func (r *Route53) GetHostedZoneNSRecord(ctx context.Context, logger logr.Logger, zoneId string, zoneName string) (*resolver.DNSRecord, error) {
logger = logger.WithValues("zoneId", zoneId, "zoneName", zoneName)

var resourceRecordSet *route53.ResourceRecordSet

Expand All @@ -206,19 +206,25 @@ func (r *Route53) GetHostedZoneNSRecords(ctx context.Context, logger logr.Logger
logger.Info("Requesting NS record")

listResourceRecordSetsOutput, err := r.client.ListResourceRecordSetsWithContext(ctx, &route53.ListResourceRecordSetsInput{
HostedZoneId: awssdk.String(zoneId),
MaxItems: awssdk.String("1"), // First entry is always NS record
HostedZoneId: awssdk.String(zoneId),
MaxItems: awssdk.String("1"),
StartRecordType: awssdk.String(route53.RRTypeNs),

// `StartRecordType` must be specified together with `StartRecordName`, so we specify the zone domain
StartRecordName: awssdk.String(zoneName),
})
if err != nil {
return nil, errors.WithStack(err)
}

// Ensure the above assumptions for request behavior hold
if len(listResourceRecordSetsOutput.ResourceRecordSets) != 1 {
return nil, errors.New("logic error - did not receive exactly one resource record")
return nil, errors.New("logic error - did not receive exactly one resource record set")
}
if *listResourceRecordSetsOutput.ResourceRecordSets[0].Type != route53.RRTypeNs {
return nil, errors.New("logic error - did not receive a resource record set of type NS")
return nil, errors.Errorf(
"logic error - did not receive a resource record set of type NS (got name %q and type %q)",
*listResourceRecordSetsOutput.ResourceRecordSets[0].Name,
*listResourceRecordSetsOutput.ResourceRecordSets[0].Type)
}
if len(listResourceRecordSetsOutput.ResourceRecordSets[0].ResourceRecords) == 0 {
return nil, errors.New("did not receive any NS resource record")
Expand Down
2 changes: 1 addition & 1 deletion pkg/resolver/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Route53Client interface {
CreateHostedZone(ctx context.Context, logger logr.Logger, dnsZone DnsZone) (string, error)
DeleteHostedZone(ctx context.Context, logger logr.Logger, zoneId string) error
GetHostedZoneIdByName(ctx context.Context, logger logr.Logger, zoneName string) (string, error)
GetHostedZoneNSRecords(ctx context.Context, logger logr.Logger, zoneId string) (*DNSRecord, error)
GetHostedZoneNSRecord(ctx context.Context, logger logr.Logger, zoneId string, zoneName string) (*DNSRecord, error)
AddDelegationToParentZone(ctx context.Context, logger logr.Logger, parentZoneId string, resourceRecord *DNSRecord) error
DeleteDelegationFromParentZone(ctx context.Context, logger logr.Logger, parentZoneId string, resourceRecord *DNSRecord) error
AddDnsRecordsToHostedZone(ctx context.Context, logger logr.Logger, hostedZoneId string, dnsRecords []DNSRecord) error
Expand Down
86 changes: 44 additions & 42 deletions pkg/resolver/resolverfakes/fake_route53client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/resolver/zoner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (d *Zoner) CreateHostedZone(ctx context.Context, logger logr.Logger, cluste
return errors.WithStack(err)
}

nsRecord, err := route53Client.GetHostedZoneNSRecords(ctx, logger, hostedZoneId)
nsRecord, err := route53Client.GetHostedZoneNSRecord(ctx, logger, hostedZoneId, dnsZoneToCreate.DnsName)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (d *Zoner) DeleteHostedZone(ctx context.Context, logger logr.Logger, cluste
return errors.WithStack(err)
}

nsRecord, err := route53Client.GetHostedZoneNSRecords(ctx, logger, hostedZoneId)
nsRecord, err := route53Client.GetHostedZoneNSRecord(ctx, logger, hostedZoneId, hostedZoneName)
if err != nil {
return errors.WithStack(err)
}
Expand Down

0 comments on commit eba8338

Please sign in to comment.