Compare commits

..

3 Commits

Author SHA1 Message Date
amery 3a1b00ad1b WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 00:01:26 +00:00
amery 7564bdcc4d dns: introduce AddrRecord{} to abstract A/AAAA entries
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 00:01:26 +00:00
amery 3ff62a1ef4 vscode: add Lookuper, publicsuffix and libdns to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 00:01:12 +00:00
7 changed files with 236 additions and 216 deletions
+1
View File
@@ -8,6 +8,7 @@
"jpictl",
"libdns",
"Lookuper",
"publicsuffix",
"Wrapf",
"zerolog"
]
+3 -11
View File
@@ -3,7 +3,6 @@ package main
import (
"context"
"os"
"time"
"github.com/spf13/cobra"
@@ -11,11 +10,7 @@ import (
"git.jpi.io/amery/jpictl/pkg/dns"
)
const (
DNSTimeout = 10 * time.Second
)
func newDNSManager(ctx context.Context, m *cluster.Cluster) (*dns.Manager, error) {
func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) {
domain := m.Domain
if m.Name != "" {
domain = m.Name + "." + domain
@@ -28,7 +23,7 @@ func newDNSManager(ctx context.Context, m *cluster.Cluster) (*dns.Manager, error
m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
err = mgr.AddHost(context.TODO(), z.Name, p.ID, true, p.PublicAddresses...)
return err != nil
})
@@ -56,10 +51,7 @@ var dnsWriteCmd = &cobra.Command{
return err
}
ctx, cancel := context.WithTimeout(context.Background(), DNSTimeout)
defer cancel()
mgr, err := newDNSManager(ctx, m)
mgr, err := newDNSManager(m)
if err != nil {
return err
}
+1 -1
View File
@@ -18,6 +18,7 @@ require (
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
)
@@ -42,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
+29 -25
View File
@@ -1,30 +1,34 @@
// Package dns manages DNS entries for the cluster
package dns
//// 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
//}
import "net/netip"
// // A Config defines a Region
//
//type Region struct {
// Name string
//}
//
//type Zone struct {
// Name string
//
// Machines map[int]*Machine
//}
//
//type Machine struct {
// ID int
//
// Active bool
// Addrs []netip.Addr
//}
// 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
}
+29 -24
View File
@@ -1,26 +1,31 @@
package dns
//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: addrs,
// }
//
// return nil
//}
//
import (
"context"
"net/netip"
)
// AddHost registers a machine
func (*Manager) AddHost(_ context.Context, _ string, _ int, _ bool, _ ...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: addrs,
// }
//
return nil
}
+119 -109
View File
@@ -1,116 +1,126 @@
package dns
// // Manager is a DNS Manager instance
// type Manager struct {
// mu sync.Mutex
//
// domain string
// suffix string
// zones map[string]*Zone
//
// 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
// }
// }
//
import (
"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
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()
// }
// func WithProvider(p Provider) ManagerOption {
// var err error
//
// if err != nil {
// return newErrorManagerOption(err, "WithProvider")
// }
// if p == nil {
// p, err = DefaultDNSProvider()
// }
//
// 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.p == nil {
// opts = append(opts, WithProvider(nil))
// }
//
// 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)
//
// 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
// }
// 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 (*Manager) setDefaults() error {
// var opts []ManagerOption
//
// if mgr.p == nil {
// opts = append(opts, WithProvider(nil))
// }
//
// 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)
//
// 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
}
+54 -46
View File
@@ -1,51 +1,59 @@
package dns
// func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
// var buf bytes.Buffer
//
// var zoneNames sort.StringSlice
//
// // sort zones
// for name := range mgr.zones {
// zoneNames = append(zoneNames, name)
// }
// sort.Sort(zoneNames)
//
// // records
// zones := make(map[string][]libdns.Record)
//
// for _, name := range zoneNames {
// z := mgr.zones[name]
//
// if buf.Len() > 0 {
// _, _ = buf.WriteRune('\n')
// }
//
// // servers
// err := mgr.writeZoneMachinesToBuffer(&buf, name, z)
// if err != nil {
// return 0, err
// }
//
// // zone alias
// _, _ = fmt.Fprintf(&buf, "; %s\n", name)
// records := mgr.genAliasRecords(name, true, z)
// mgr.Sort(records)
// mgr.writeRecordsToBuffer(&buf, "", records...)
//
// zones[name] = records
// }
//
// mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon")
// mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams")
// mgr.writeRegionToBuffer(&buf, zones, "de", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "eu", "ssd-ams", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "europe", "ssd-ams", "ssd-lon", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "earth", "ssd-ams", "ssd-lon", "htz-fsn")
//
// return buf.WriteTo(w)
// }
//
import (
"bytes"
"io"
)
// WriteTo writes the DNS data for the cluster
func (*Manager) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
//
// var zoneNames sort.StringSlice
//
// // sort zones
// for name := range mgr.zones {
// zoneNames = append(zoneNames, name)
// }
// sort.Sort(zoneNames)
//
// // records
// zones := make(map[string][]libdns.Record)
//
// for _, name := range zoneNames {
// z := mgr.zones[name]
//
// if buf.Len() > 0 {
// _, _ = buf.WriteRune('\n')
// }
//
// // servers
// err := mgr.writeZoneMachinesToBuffer(&buf, name, z)
// if err != nil {
// return 0, err
// }
//
// // zone alias
// _, _ = fmt.Fprintf(&buf, "; %s\n", name)
// records := mgr.genAliasRecords(name, true, z)
// mgr.Sort(records)
// mgr.writeRecordsToBuffer(&buf, "", records...)
//
// zones[name] = records
// }
//
// mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon")
// mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams")
// mgr.writeRegionToBuffer(&buf, zones, "de", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "eu", "ssd-ams", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "europe", "ssd-ams", "ssd-lon", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "earth", "ssd-ams", "ssd-lon", "htz-fsn")
return buf.WriteTo(w)
}
// revive:disable:line-length-limit
// func (mgr *Manager) writeRecordsToBuffer(buf *bytes.Buffer, rename string, records ...libdns.Record) {
// for _, rr := range records {
// name := core.IIf(rename != "", rename, rr.Name)