Skip to content

Commit

Permalink
Add NS record support for Azure DNS
Browse files Browse the repository at this point in the history
Azure supports NS records on public DNS zones: https://learn.microsoft.com/en-us/rest/api/dns/record-sets/create-or-update?view=rest-dns-2018-05-01&tabs=HTTP#create-ns-recordset
This PR adds this support to external dns. The implementation is similar to the other records.

Besides the unit tests this PR was also tested by creating a DNSEndpoint with the following spec:
```
    endpoints:
    - dnsName: cloud.k8gb.io
      recordTTL: 5
      recordType: NS
      targets:
      - gslb-ns-eu-cloud.k8gb.io
      - gslb-ns-us-cloud.k8gb.io
```
The creation of the NS record in Azure was successful:
```
az network dns record-set ns list --resource-group rg-k8gb  --zone-name "$EDGE_DNS_ZONE" --output json
[
  {...},
  {
    "NSRecords": [
      {
        "nsdname": "gslb-ns-eu-cloud.k8gb.io"
      },
      {
        "nsdname": "gslb-ns-us-cloud.k8gb.io"
      }
    ],
    "TTL": 5,
    "etag": "97a7199f-3be9-47bd-ab00-37013b775180",
    "fqdn": "cloud.k8gb.io.",
    "id": "/subscriptions/<redacted>/resourceGroups/rg-k8gb/providers/Microsoft.Network/dnszones/k8gb.io/NS/cloud",
    "name": "cloud",
    "provisioningState": "Succeeded",
    "resourceGroup": "rg-k8gb",
    "targetResource": {},
    "trafficManagementProfile": {},
    "type": "Microsoft.Network/dnszones/NS"
  }
]
```

This change was already attempted in #2835, but it was never merged due to inactivity.

Signed-off-by: Andre Aguas <andre.aguas@protonmail.com>
  • Loading branch information
abaguas committed Nov 4, 2024
1 parent b2ec522 commit c2dc4bd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
23 changes: 23 additions & 0 deletions provider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ func (p *AzureProvider) newRecordSet(endpoint *endpoint.Endpoint) (dns.RecordSet
MxRecords: mxRecords,
},
}, nil
case dns.RecordTypeNS:
nsRecords := make([]*dns.NsRecord, len(endpoint.Targets))
for i, target := range endpoint.Targets {
nsRecords[i] = &dns.NsRecord{
Nsdname: to.Ptr(target),
}
}
return dns.RecordSet{
Properties: &dns.RecordSetProperties{
TTL: to.Ptr(ttl),
NsRecords: nsRecords,
},
}, nil
case dns.RecordTypeTXT:
return dns.RecordSet{
Properties: &dns.RecordSetProperties{
Expand Down Expand Up @@ -460,6 +473,16 @@ func extractAzureTargets(recordSet *dns.RecordSet) []string {
return targets
}

// Check for NS records
nsRecords := properties.NsRecords
if len(nsRecords) > 0 && (nsRecords)[0].Nsdname != nil {
targets := make([]string, len(nsRecords))
for i, nsRecord := range nsRecords {
targets[i] = *nsRecord.Nsdname
}
return targets
}

// Check for TXT records
txtRecords := properties.TxtRecords
if len(txtRecords) > 0 && (txtRecords)[0].Value != nil {
Expand Down
8 changes: 4 additions & 4 deletions provider/azure/azure_privatedns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ func TestAzurePrivateDNSApplyChanges(t *testing.T) {
}

func TestAzurePrivateDNSApplyChangesDryRun(t *testing.T) {
recordsClient := mockRecordSetsClient{}
recordsClient := mockPrivateRecordSetsClient{}

testAzureApplyChangesInternal(t, true, &recordsClient)
testAzurePrivateDNSApplyChangesInternal(t, true, &recordsClient)

validateAzureEndpoints(t, recordsClient.deletedEndpoints, []*endpoint.Endpoint{})

Expand Down Expand Up @@ -471,9 +471,9 @@ func TestAzurePrivateDNSNameFilter(t *testing.T) {
}

func TestAzurePrivateDNSApplyChangesZoneName(t *testing.T) {
recordsClient := mockRecordSetsClient{}
recordsClient := mockPrivateRecordSetsClient{}

testAzureApplyChangesInternalZoneName(t, false, &recordsClient)
testAzurePrivateDNSApplyChangesInternalZoneName(t, false, &recordsClient)

validateAzureEndpoints(t, recordsClient.deletedEndpoints, []*endpoint.Endpoint{
endpoint.NewEndpoint("deleted.foo.example.com", endpoint.RecordTypeA, ""),
Expand Down
Loading

0 comments on commit c2dc4bd

Please sign in to comment.