Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-09-12 04:37:51 +00:00
parent d99bbc3add
commit 314510cafb
9 changed files with 368 additions and 4 deletions
+2 -1
View File
@@ -13,10 +13,12 @@ require (
darvaza.org/slog/handlers/discard v0.4.5
github.com/gofrs/uuid/v5 v5.0.0
github.com/hack-pad/hackpadfs v0.2.1
github.com/libdns/cloudflare v0.1.0
github.com/libdns/libdns v0.2.1
github.com/mgechev/revive v1.3.3
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0
golang.org/x/net v0.14.0
gopkg.in/yaml.v3 v3.0.1
)
@@ -41,7 +43,6 @@ require (
github.com/rs/zerolog v1.30.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.12.0 // indirect
+3
View File
@@ -34,6 +34,9 @@ github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMI
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
github.com/libdns/cloudflare v0.1.0/go.mod h1:a44IP6J1YH6nvcNl1PverfJviADgXUnsozR3a7vBKN8=
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
+9
View File
@@ -9,6 +9,15 @@ type Region struct {
Regions []string `json:",omitempty" yaml:",omitempty"`
}
// Zones ...
func (r *Region) Zones() []string {
out := make([]string, len(r.zones))
for i := range r.zones {
out[i] = r.zones[i].Name
}
return out
}
func (m *Cluster) initRegions(_ *ScanOptions) error {
regions := make(map[string][]*Zone)
+32
View File
@@ -1,2 +1,34 @@
// Package dns manages DNS entries for the cluster
package dns
import "net/netip"
// // A Config defines a Region
//
// type Config struct {
// // Name is the identifier of this Region
// Name string
// // Regions are a list of (sub)regions that belong to this Region
// Regions []string
// // Zones are a list of Zones that directly belong to this Region
// Zones []string
// }
//
// type Region struct {
// Name string
// }
// Zone represents a set of machines with high affinity
type Zone struct {
Name string
Machines map[int]*Machine
}
// Machine represents a member of the cluster
type Machine struct {
ID int
Active bool
Addrs []netip.Addr
}
+41
View File
@@ -0,0 +1,41 @@
package dns
import (
"context"
"io/fs"
"net/netip"
)
// AddHost registers a machine
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
active bool, addrs ...netip.Addr) error {
//
if zone == "" || id <= 0 {
return fs.ErrInvalid
}
z, ok := mgr.zones[zone]
if !ok {
z = &Zone{
Name: zone,
Machines: make(map[int]*Machine),
}
mgr.zones[zone] = z
}
z.Machines[id] = &Machine{
ID: id,
Active: active,
Addrs: SortAddrSlice(addrs),
}
return nil
}
// AddRegion specifies a new region and the zones it contains
func (mgr *Manager) AddRegion(_ context.Context, region string, zones ...string) error {
mgr.l.Debug().WithField("region", region).WithField("zones", zones).Print()
mgr.regions[region] = append(mgr.regions[region], zones...)
return nil
}
+124
View File
@@ -0,0 +1,124 @@
package dns
import (
"errors"
"strings"
"sync"
"darvaza.org/core"
"darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/cluster"
"golang.org/x/net/publicsuffix"
)
// Manager is a DNS Manager instance
type Manager struct {
mu sync.Mutex
domain string
suffix string
zones map[string]*Zone
regions map[string][]string
p Provider
l slog.Logger
}
// ManagerOption configures a Manager
type ManagerOption func(*Manager) error
func newErrorManagerOption(err error, hint string) ManagerOption {
return func(*Manager) error {
if hint != "" {
err = core.Wrap(err, hint)
}
return err
}
}
// WithProvider attaches a libdns Provider to the Manager
func WithProvider(p Provider) ManagerOption {
var err error
if p == nil {
p, err = DefaultDNSProvider()
}
if err != nil {
return newErrorManagerOption(err, "WithProvider")
}
return func(mgr *Manager) error {
mgr.p = p
return nil
}
}
// WithLogger attaches a logger to the Manager
func WithLogger(log slog.Logger) ManagerOption {
if log == nil {
log = cluster.DefaultLogger()
}
return func(mgr *Manager) error {
mgr.l = log
return nil
}
}
func (mgr *Manager) setDefaults() error {
var opts []ManagerOption
if mgr.l == nil {
opts = append(opts, WithLogger(nil))
}
if mgr.domain == "" || mgr.suffix == "" {
return errors.New("domain not specified")
}
mgr.zones = make(map[string]*Zone)
mgr.regions = make(map[string][]string)
for _, opt := range opts {
if err := opt(mgr); err != nil {
return err
}
}
return nil
}
// WithDomain specifies where the manager operates
func WithDomain(domain string) ManagerOption {
base, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
return newErrorManagerOption(err, "publicsuffix")
}
suffix := strings.TrimSuffix(domain, base)
if suffix != "" {
suffix = "." + suffix[:len(suffix)-1]
}
return func(mgr *Manager) error {
mgr.domain = base
mgr.suffix = suffix
return nil
}
}
// NewManager creates a DNS manager with the
func NewManager(opts ...ManagerOption) (*Manager, error) {
mgr := new(Manager)
for _, opt := range opts {
if err := opt(mgr); err != nil {
return nil, err
}
}
if err := mgr.setDefaults(); err != nil {
return nil, err
}
return mgr, nil
}
+36
View File
@@ -0,0 +1,36 @@
package dns
import (
"fmt"
"os"
"github.com/libdns/cloudflare"
"github.com/libdns/libdns"
)
const (
// CloudflareAPIToken is the environment variable
// containing the API Token
CloudflareAPIToken = "CLOUDFLARE_DNS_API_TOKEN"
)
// Provider manages DNS entries
type Provider interface {
libdns.RecordGetter
libdns.RecordDeleter
}
// DefaultDNSProvider returns a cloudflare DNS provider
// using an API Token from env [CloudflareAPIToken]
func DefaultDNSProvider() (*cloudflare.Provider, error) {
s := os.Getenv(CloudflareAPIToken)
if s == "" {
return nil, fmt.Errorf("%q: %s", CloudflareAPIToken, "not found")
}
p := &cloudflare.Provider{
APIToken: s,
}
return p, nil
}
+86 -3
View File
@@ -1,6 +1,7 @@
package dns
import (
"fmt"
"net/netip"
"sort"
"time"
@@ -9,6 +10,14 @@ import (
"github.com/libdns/libdns"
)
// SortAddrSlice ...
func SortAddrSlice(s []netip.Addr) []netip.Addr {
sort.Slice(s, func(i, j int) bool {
return s[i].Less(s[j])
})
return s
}
// AddrRecord represents an A or AAAA record
type AddrRecord struct {
Name string
@@ -17,9 +26,7 @@ type AddrRecord struct {
// 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])
})
SortAddrSlice(rr.Addr)
}
// Export converts the record into libdns.Record
@@ -36,3 +43,79 @@ func (rr *AddrRecord) Export() []libdns.Record {
return out
}
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
var out []AddrRecord
cache := make(map[string][]netip.Addr)
// zones
for _, z := range mgr.zones {
// machines
s := mgr.genZoneMachineRecords(z)
out = append(out, s...)
// zone alias
addrs := mgr.genZoneAddresses(z)
name := z.Name
out = append(out, AddrRecord{
Name: name + mgr.suffix,
Addr: addrs,
})
// and cache for regions
cache[name] = addrs
}
// regions
for name, zones := range mgr.regions {
var addrs []netip.Addr
for _, z := range zones {
addrs = append(addrs, cache[z]...)
}
rec := AddrRecord{
Name: name + mgr.suffix,
Addr: addrs,
}
rec.Sort()
out = append(out, rec)
}
// sort
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
return out
}
func (*Manager) genZoneAddresses(z *Zone) []netip.Addr {
var out []netip.Addr
for _, p := range z.Machines {
if p.Active {
out = append(out, p.Addrs...)
}
}
SortAddrSlice(out)
return out
}
func (mgr *Manager) genZoneMachineRecords(z *Zone) []AddrRecord {
out := make([]AddrRecord, 0, len(z.Machines))
for _, p := range z.Machines {
rec := AddrRecord{
Name: fmt.Sprintf("%s-%v%s", z.Name, p.ID, mgr.suffix),
Addr: p.Addrs,
}
out = append(out, rec)
}
return out
}
+35
View File
@@ -0,0 +1,35 @@
package dns
import (
"bytes"
"fmt"
"io"
"time"
"github.com/libdns/libdns"
)
// WriteTo writes the DNS data for the cluster
func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
records := mgr.genAllAddrRecords()
for i := range records {
r := &records[i]
r.Name = fmt.Sprintf("%s.%s.", r.Name, mgr.domain)
}
for _, rr1 := range records {
for _, rr2 := range rr1.Export() {
writeRecord(&buf, rr2)
}
}
return buf.WriteTo(w)
}
func writeRecord(w io.Writer, rr libdns.Record) {
_, _ = fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s\n",
rr.Name, int(rr.TTL/time.Second),
rr.Type, rr.Value)
}