Compare commits

..

3 Commits

Author SHA1 Message Date
amery 4f534f7cf1 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-11 23:40:19 +00:00
amery 012b02cc9c dns: introduce AddrRecord{} to abstract A/AAAA entries
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-11 23:29:56 +00:00
amery 85b0766418 vscode: add Lookuper and libdns to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-11 21:46:20 +00:00
7 changed files with 216 additions and 236 deletions
-1
View File
@@ -8,7 +8,6 @@
"jpictl", "jpictl",
"libdns", "libdns",
"Lookuper", "Lookuper",
"publicsuffix",
"Wrapf", "Wrapf",
"zerolog" "zerolog"
] ]
+11 -3
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"os" "os"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -10,7 +11,11 @@ import (
"git.jpi.io/amery/jpictl/pkg/dns" "git.jpi.io/amery/jpictl/pkg/dns"
) )
func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) { const (
DNSTimeout = 10 * time.Second
)
func newDNSManager(ctx context.Context, m *cluster.Cluster) (*dns.Manager, error) {
domain := m.Domain domain := m.Domain
if m.Name != "" { if m.Name != "" {
domain = m.Name + "." + domain domain = m.Name + "." + domain
@@ -23,7 +28,7 @@ func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) {
m.ForEachZone(func(z *cluster.Zone) bool { m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool { z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(context.TODO(), z.Name, p.ID, true, p.PublicAddresses...) err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...)
return err != nil return err != nil
}) })
@@ -51,7 +56,10 @@ var dnsWriteCmd = &cobra.Command{
return err return err
} }
mgr, err := newDNSManager(m) ctx, cancel := context.WithTimeout(context.Background(), DNSTimeout)
defer cancel()
mgr, err := newDNSManager(ctx, m)
if err != nil { if err != nil {
return err return err
} }
+1 -1
View File
@@ -18,7 +18,6 @@ require (
github.com/mgechev/revive v1.3.3 github.com/mgechev/revive v1.3.3
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0 golang.org/x/crypto v0.12.0
golang.org/x/net v0.14.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
@@ -43,6 +42,7 @@ require (
github.com/rs/zerolog v1.30.0 // indirect github.com/rs/zerolog v1.30.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.12.0 // 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/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.12.0 // indirect golang.org/x/tools v0.12.0 // indirect
+25 -29
View File
@@ -1,34 +1,30 @@
// Package dns manages DNS entries for the cluster // Package dns manages DNS entries for the cluster
package dns package dns
import "net/netip" //// A Config defines a Region
//type Config struct {
// // A Config defines a Region // // 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 Config struct { //type Region struct {
// // Name is the identifier of this Region // Name string
// Name string //}
// // Regions are a list of (sub)regions that belong to this Region //
// Regions []string //type Zone struct {
// // Zones are a list of Zones that directly belong to this Region // Name string
// Zones []string //
// } // Machines map[int]*Machine
//}
//
//type Machine struct {
// ID int
//
// Active bool
// Addrs []netip.Addr
//}
// //
// 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
}
+24 -29
View File
@@ -1,31 +1,26 @@
package dns package dns
import ( //func (mgr *Manager) AddHost(_ context.Context, zone string, id int, active bool, addrs ...netip.Addr) error {
"context" // if zone == "" || id <= 0 {
"net/netip" // return fs.ErrInvalid
) // }
//
// AddHost registers a machine // z, ok := mgr.zones[zone]
func (*Manager) AddHost(_ context.Context, _ string, _ int, _ bool, _ ...netip.Addr) error { // if !ok {
// if zone == "" || id <= 0 { // z = &Zone{
// return fs.ErrInvalid // Name: zone,
// } // Machines: make(map[int]*Machine),
// // }
// z, ok := mgr.zones[zone] //
// if !ok { // mgr.zones[zone] = z
// z = &Zone{ // }
// Name: zone, //
// Machines: make(map[int]*Machine), // z.Machines[id] = &Machine{
// } // ID: id,
// // Active: active,
// mgr.zones[zone] = z // Addrs: addrs,
// } // }
// //
// z.Machines[id] = &Machine{ // return nil
// ID: id, //}
// Active: active, //
// Addrs: addrs,
// }
//
return nil
}
+109 -119
View File
@@ -1,126 +1,116 @@
package dns package dns
import ( // // Manager is a DNS Manager instance
"strings" // type Manager struct {
"sync" // mu sync.Mutex
//
"darvaza.org/core" // domain string
"darvaza.org/slog" // suffix string
"git.jpi.io/amery/jpictl/pkg/cluster" // zones map[string]*Zone
"golang.org/x/net/publicsuffix" //
) // p Provider
// l slog.Logger
// Manager is a DNS Manager instance // }
type Manager struct { //
mu sync.Mutex // // ManagerOption configures a Manager
// type ManagerOption func(*Manager) error
domain string //
suffix string // func newErrorManagerOption(err error, hint string) ManagerOption {
zones map[string]*Zone // return func(*Manager) error {
// if hint != "" {
p Provider // err = core.Wrap(err, hint)
l slog.Logger // }
} // return err
// }
// 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 // // WithProvider attaches a libdns Provider to the Manager
// func WithProvider(p Provider) ManagerOption {
// var err error
// //
// func WithProvider(p Provider) ManagerOption { // if p == nil {
// var err error // p, err = DefaultDNSProvider()
// }
// //
// if p == nil { // if err != nil {
// p, err = DefaultDNSProvider() // return newErrorManagerOption(err, "WithProvider")
// } // }
// //
// if err != nil { // return func(mgr *Manager) error {
// return newErrorManagerOption(err, "WithProvider") // 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
// }
// //
// 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
}
+46 -54
View File
@@ -1,59 +1,51 @@
package dns package dns
import ( // func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
"bytes" // var buf bytes.Buffer
"io" //
) // var zoneNames sort.StringSlice
//
// WriteTo writes the DNS data for the cluster // // sort zones
func (*Manager) WriteTo(w io.Writer) (int64, error) { // for name := range mgr.zones {
var buf bytes.Buffer // zoneNames = append(zoneNames, name)
// // }
// var zoneNames sort.StringSlice // sort.Sort(zoneNames)
// //
// // sort zones // // records
// for name := range mgr.zones { // zones := make(map[string][]libdns.Record)
// zoneNames = append(zoneNames, name) //
// } // for _, name := range zoneNames {
// sort.Sort(zoneNames) // z := mgr.zones[name]
// //
// // records // if buf.Len() > 0 {
// zones := make(map[string][]libdns.Record) // _, _ = buf.WriteRune('\n')
// // }
// for _, name := range zoneNames { //
// z := mgr.zones[name] // // servers
// // err := mgr.writeZoneMachinesToBuffer(&buf, name, z)
// if buf.Len() > 0 { // if err != nil {
// _, _ = buf.WriteRune('\n') // return 0, err
// } // }
// //
// // servers // // zone alias
// err := mgr.writeZoneMachinesToBuffer(&buf, name, z) // _, _ = fmt.Fprintf(&buf, "; %s\n", name)
// if err != nil { // records := mgr.genAliasRecords(name, true, z)
// return 0, err // mgr.Sort(records)
// } // mgr.writeRecordsToBuffer(&buf, "", records...)
// //
// // zone alias // zones[name] = records
// _, _ = fmt.Fprintf(&buf, "; %s\n", name) // }
// records := mgr.genAliasRecords(name, true, z) //
// mgr.Sort(records) // mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon")
// mgr.writeRecordsToBuffer(&buf, "", records...) // mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams")
// // mgr.writeRegionToBuffer(&buf, zones, "de", "htz-fsn")
// zones[name] = records // 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")
// mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon") //
// mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams") // return buf.WriteTo(w)
// 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) { // func (mgr *Manager) writeRecordsToBuffer(buf *bytes.Buffer, rename string, records ...libdns.Record) {
// for _, rr := range records { // for _, rr := range records {
// name := core.IIf(rename != "", rename, rr.Name) // name := core.IIf(rename != "", rename, rr.Name)