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", "jpictl",
"libdns", "libdns",
"Lookuper", "Lookuper",
"publicsuffix",
"Wrapf", "Wrapf",
"zerolog" "zerolog"
] ]
+3 -11
View File
@@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"os" "os"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -11,11 +10,7 @@ import (
"git.jpi.io/amery/jpictl/pkg/dns" "git.jpi.io/amery/jpictl/pkg/dns"
) )
const ( func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) {
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
@@ -28,7 +23,7 @@ func newDNSManager(ctx context.Context, 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(ctx, z.Name, p.ID, true, p.PublicAddresses...) err = mgr.AddHost(context.TODO(), z.Name, p.ID, true, p.PublicAddresses...)
return err != nil return err != nil
}) })
@@ -56,10 +51,7 @@ var dnsWriteCmd = &cobra.Command{
return err return err
} }
ctx, cancel := context.WithTimeout(context.Background(), DNSTimeout) mgr, err := newDNSManager(m)
defer cancel()
mgr, err := newDNSManager(ctx, m)
if err != nil { if err != nil {
return err return err
} }
+1 -1
View File
@@ -18,6 +18,7 @@ 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
) )
@@ -42,7 +43,6 @@ 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
+29 -25
View File
@@ -1,30 +1,34 @@
// Package dns manages DNS entries for the cluster // Package dns manages DNS entries for the cluster
package dns package dns
//// A Config defines a Region import "net/netip"
//type Config struct {
// // Name is the identifier of this Region // // A Config defines a 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 { // type Config struct {
// Name string // // Name is the identifier of this Region
//} // Name string
// // // Regions are a list of (sub)regions that belong to this Region
//type Zone struct { // Regions []string
// Name string // // Zones are a list of Zones that directly belong to this Region
// // 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
}
+29 -24
View File
@@ -1,26 +1,31 @@
package dns package dns
//func (mgr *Manager) AddHost(_ context.Context, zone string, id int, active bool, addrs ...netip.Addr) error { import (
// if zone == "" || id <= 0 { "context"
// return fs.ErrInvalid "net/netip"
// } )
//
// z, ok := mgr.zones[zone] // AddHost registers a machine
// if !ok { func (*Manager) AddHost(_ context.Context, _ string, _ int, _ bool, _ ...netip.Addr) error {
// z = &Zone{ // if zone == "" || id <= 0 {
// Name: zone, // return fs.ErrInvalid
// Machines: make(map[int]*Machine), // }
// } //
// // z, ok := mgr.zones[zone]
// mgr.zones[zone] = z // if !ok {
// } // z = &Zone{
// // Name: zone,
// z.Machines[id] = &Machine{ // Machines: make(map[int]*Machine),
// ID: id, // }
// Active: active, //
// Addrs: addrs, // mgr.zones[zone] = z
// } // }
// //
// return nil // z.Machines[id] = &Machine{
//} // ID: id,
// // Active: active,
// Addrs: addrs,
// }
//
return nil
}
+119 -109
View File
@@ -1,116 +1,126 @@
package dns package dns
// // Manager is a DNS Manager instance import (
// type Manager struct { "strings"
// mu sync.Mutex "sync"
//
// domain string "darvaza.org/core"
// suffix string "darvaza.org/slog"
// zones map[string]*Zone "git.jpi.io/amery/jpictl/pkg/cluster"
// "golang.org/x/net/publicsuffix"
// p Provider )
// l slog.Logger
// } // Manager is a DNS Manager instance
// type Manager struct {
// // ManagerOption configures a Manager mu sync.Mutex
// type ManagerOption func(*Manager) error
// domain string
// func newErrorManagerOption(err error, hint string) ManagerOption { suffix string
// return func(*Manager) error { zones map[string]*Zone
// if hint != "" {
// err = core.Wrap(err, hint) p Provider
// } 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
// //
// if p == nil { // func WithProvider(p Provider) ManagerOption {
// p, err = DefaultDNSProvider() // var err error
// }
// //
// if err != nil { // if p == nil {
// return newErrorManagerOption(err, "WithProvider") // p, err = DefaultDNSProvider()
// } // }
// //
// return func(mgr *Manager) error { // if err != nil {
// mgr.p = p // return newErrorManagerOption(err, "WithProvider")
// 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
}
+54 -46
View File
@@ -1,51 +1,59 @@
package dns package dns
// func (mgr *Manager) WriteTo(w io.Writer) (int64, error) { import (
// var buf bytes.Buffer "bytes"
// "io"
// var zoneNames sort.StringSlice )
//
// // sort zones // WriteTo writes the DNS data for the cluster
// for name := range mgr.zones { func (*Manager) WriteTo(w io.Writer) (int64, error) {
// zoneNames = append(zoneNames, name) var buf bytes.Buffer
// } //
// sort.Sort(zoneNames) // var zoneNames sort.StringSlice
// //
// // records // // sort zones
// zones := make(map[string][]libdns.Record) // for name := range mgr.zones {
// // zoneNames = append(zoneNames, name)
// for _, name := range zoneNames { // }
// z := mgr.zones[name] // sort.Sort(zoneNames)
// //
// if buf.Len() > 0 { // // records
// _, _ = buf.WriteRune('\n') // zones := make(map[string][]libdns.Record)
// } //
// // for _, name := range zoneNames {
// // servers // z := mgr.zones[name]
// err := mgr.writeZoneMachinesToBuffer(&buf, name, z) //
// if err != nil { // if buf.Len() > 0 {
// return 0, err // _, _ = buf.WriteRune('\n')
// } // }
// //
// // zone alias // // servers
// _, _ = fmt.Fprintf(&buf, "; %s\n", name) // err := mgr.writeZoneMachinesToBuffer(&buf, name, z)
// records := mgr.genAliasRecords(name, true, z) // if err != nil {
// mgr.Sort(records) // return 0, err
// mgr.writeRecordsToBuffer(&buf, "", records...) // }
// //
// zones[name] = records // // zone alias
// } // _, _ = fmt.Fprintf(&buf, "; %s\n", name)
// // records := mgr.genAliasRecords(name, true, z)
// mgr.writeRegionToBuffer(&buf, zones, "uk", "ssd-lon") // mgr.Sort(records)
// mgr.writeRegionToBuffer(&buf, zones, "nl", "ssd-ams") // mgr.writeRecordsToBuffer(&buf, "", records...)
// mgr.writeRegionToBuffer(&buf, zones, "de", "htz-fsn") //
// mgr.writeRegionToBuffer(&buf, zones, "eu", "ssd-ams", "htz-fsn") // zones[name] = records
// 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")
// return buf.WriteTo(w) // 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) { // 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)