Skip to content

Commit

Permalink
feat: Add support for NS records (#98)
Browse files Browse the repository at this point in the history
* add support for NS records

* update deployment values

* update deployment values

* update example deployments
  • Loading branch information
mircea-pavel-anton authored Oct 26, 2024
1 parent c195471 commit 2e5776c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
3 changes: 3 additions & 0 deletions deploy/records/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ resources:
- aaaa-record.yaml
- cname-record.yaml
- complex-record.yaml
- mx-record.yaml
- ns-record.yaml
- srv-record.yaml
- text-record.yaml
1 change: 0 additions & 1 deletion deploy/records/ns-record.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ spec:
recordType: NS
targets:
- ns1.example.com
- ns2.example.com
3 changes: 2 additions & 1 deletion deploy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ provider:
webhook:
image:
repository: ghcr.io/mirceanton/external-dns-provider-mikrotik
tag: pr-97
tag: pr-98
pullPolicy: Always
env:
- name: LOG_FORMAT
Expand Down Expand Up @@ -66,6 +66,7 @@ extraArgs:
- --managed-record-types=TXT
- --managed-record-types=MX
- --managed-record-types=SRV
- --managed-record-types=NS

rbac:
create: true
Expand Down
17 changes: 16 additions & 1 deletion internal/mikrotik/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type DNSRecord struct {
SrvTarget string `json:"srv-target,omitempty"` // SRV -> provider-specific
SrvPriority string `json:"srv-priority,omitempty"` // SRV -> provider-specific
SrvWeight string `json:"srv-weight,omitempty"` // SRV -> provider-specific
NS string `json:"ns,omitempty"` // NS -> provider-specific

// Additional fields for other record types that are not currently supported
// NS string `json:"ns,omitempty"` // NS -> provider-specific
// ForwardTo string `json:"forward-to,omitempty"` // FWD
}

Expand Down Expand Up @@ -125,6 +125,13 @@ func NewDNSRecord(endpoint *endpoint.Endpoint) (*DNSRecord, error) {
record.SrvTarget = target
log.Debugf("SRV target set to: %s", record.SrvTarget)

case "NS":
if err := validateDomain(endpoint.Targets[0]); err != nil {
return nil, err
}
record.NS = endpoint.Targets[0]
log.Debugf("NS set to: %s", record.NS)

default:
return nil, fmt.Errorf("unsupported DNS type: %s", endpoint.RecordType)
}
Expand Down Expand Up @@ -247,6 +254,14 @@ func (r *DNSRecord) toExternalDNSEndpoint() (*endpoint.Endpoint, error) {
log.Debugf("SRV weight set to: %s", r.SrvWeight)
log.Debugf("SRV port set to: %s", r.SrvPort)
log.Debugf("SRV target set to: %s", r.SrvTarget)

case "NS":
if err := validateDomain(r.NS); err != nil {
return nil, err
}
ep.Targets = endpoint.NewTargets(r.NS)
log.Debugf("NS set to: %s", r.NS)

default:
return nil, fmt.Errorf("unsupported DNS type: %s", ep.RecordType)
}
Expand Down
84 changes: 84 additions & 0 deletions internal/mikrotik/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,48 @@ func TestDNSRecordToExternalDNSEndpoint(t *testing.T) {
expectError: true,
},

// ===============================================================
// NS RECORD TEST CASES
// ===============================================================
{
name: "Valid NS record",
record: &DNSRecord{
Name: "example.com",
Type: "NS",
NS: "ns1.example.net",
TTL: "1h",
},
expected: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "NS",
Targets: endpoint.NewTargets("ns1.example.net"),
RecordTTL: endpoint.TTL(3600),
},
expectError: false,
},
{
name: "Invalid NS record (empty NS field)",
record: &DNSRecord{
Name: "example.com",
Type: "NS",
NS: "",
TTL: "1h",
},
expected: nil,
expectError: true,
},
{
name: "Invalid NS record (malformed NS domain)",
record: &DNSRecord{
Name: "example.com",
Type: "NS",
NS: "invalid_domain..com",
TTL: "1h",
},
expected: nil,
expectError: true,
},

// ===============================================================
// PROVIDER-SPECIFIC DATA TEST CASES
// ===============================================================
Expand Down Expand Up @@ -1159,6 +1201,48 @@ func TestExternalDNSEndpointToDNSRecord(t *testing.T) {
expectError: true,
},

// ===============================================================
// NS RECORD TEST CASES
// ===============================================================
{
name: "Valid NS record",
endpoint: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "NS",
Targets: endpoint.NewTargets("ns1.example.net"),
RecordTTL: endpoint.TTL(3600),
},
expected: &DNSRecord{
Name: "example.com",
Type: "NS",
NS: "ns1.example.net",
TTL: "1h",
},
expectError: false,
},
{
name: "Invalid NS record (empty NS field)",
endpoint: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "NS",
Targets: endpoint.NewTargets(""),
RecordTTL: endpoint.TTL(3600),
},
expected: nil,
expectError: true,
},
{
name: "Invalid NS record (malformed NS domain)",
endpoint: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "NS",
Targets: endpoint.NewTargets("invalid_domain..com"),
RecordTTL: endpoint.TTL(3600),
},
expected: nil,
expectError: true,
},

// ===============================================================
// PROVIDER-SPECIFIC DATA TEST CASES
// ===============================================================
Expand Down

0 comments on commit 2e5776c

Please sign in to comment.