dns: introduce AddrRecord{} to abstract A/AAAA entries

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-09-11 23:26:47 +00:00
parent bead6184f5
commit d99bbc3add
4 changed files with 43 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
// Package dns manages DNS entries for the cluster
package dns
+38
View File
@@ -0,0 +1,38 @@
package dns
import (
"net/netip"
"sort"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// AddrRecord represents an A or AAAA record
type AddrRecord struct {
Name string
Addr []netip.Addr
}
// Sort sorts the addresses of the record
func (rr *AddrRecord) Sort() {
sort.Slice(rr.Addr, func(i, j int) bool {
return rr.Addr[i].Less(rr.Addr[j])
})
}
// Export converts the record into libdns.Record
func (rr *AddrRecord) Export() []libdns.Record {
out := make([]libdns.Record, len(rr.Addr))
for i, addr := range rr.Addr {
out[i] = libdns.Record{
Name: rr.Name,
TTL: time.Second * 1,
Type: core.IIf(addr.Is6(), "AAAA", "A"),
Value: addr.String(),
}
}
return out
}