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
+14 -18
View File
@@ -1,10 +1,7 @@
// 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 //// A Config defines a Region
//
//type Config struct { //type Config struct {
// // Name is the identifier of this Region // // Name is the identifier of this Region
// Name string // Name string
@@ -17,18 +14,17 @@ import "net/netip"
//type Region struct { //type Region struct {
// Name string // Name string
//} //}
//
// Zone represents a set of machines with high affinity //type Zone struct {
type Zone struct { // Name string
Name string //
// Machines map[int]*Machine
Machines map[int]*Machine //}
} //
//type Machine struct {
// Machine represents a member of the cluster // ID int
type Machine struct { //
ID int // Active bool
// Addrs []netip.Addr
Active bool //}
Addrs []netip.Addr //
}
+4 -9
View File
@@ -1,12 +1,6 @@
package dns package dns
import ( //func (mgr *Manager) AddHost(_ context.Context, zone string, id int, active bool, addrs ...netip.Addr) error {
"context"
"net/netip"
)
// AddHost registers a machine
func (*Manager) AddHost(_ context.Context, _ string, _ int, _ bool, _ ...netip.Addr) error {
// if zone == "" || id <= 0 { // if zone == "" || id <= 0 {
// return fs.ErrInvalid // return fs.ErrInvalid
// } // }
@@ -27,5 +21,6 @@ func (*Manager) AddHost(_ context.Context, _ string, _ int, _ bool, _ ...netip.A
// Addrs: addrs, // Addrs: addrs,
// } // }
// //
return nil // return nil
} //}
//
+76 -86
View File
@@ -1,41 +1,30 @@
package dns package dns
import ( // // Manager is a DNS Manager instance
"strings" // type Manager struct {
"sync" // mu sync.Mutex
"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
// //
// 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 { // func WithProvider(p Provider) ManagerOption {
// var err error // var err error
// //
@@ -52,20 +41,20 @@ func newErrorManagerOption(err error, hint string) ManagerOption {
// return nil // return nil
// } // }
// } // }
//
// WithLogger attaches a logger to the Manager // // WithLogger attaches a logger to the Manager
func WithLogger(log slog.Logger) ManagerOption { // func WithLogger(log slog.Logger) ManagerOption {
if log == nil { // if log == nil {
log = cluster.DefaultLogger() // log = cluster.DefaultLogger()
} // }
//
return func(mgr *Manager) error { // return func(mgr *Manager) error {
mgr.l = log // mgr.l = log
return nil // return nil
} // }
} // }
//
func (*Manager) setDefaults() error { // func (mgr *Manager) setDefaults() error {
// var opts []ManagerOption // var opts []ManagerOption
// //
// if mgr.p == nil { // if mgr.p == nil {
@@ -87,40 +76,41 @@ func (*Manager) setDefaults() error {
// return err // return err
// } // }
// } // }
return nil // return nil
} // }
//
// WithDomain specifies where the manager operates // // WithDomain specifies where the manager operates
func WithDomain(domain string) ManagerOption { // func WithDomain(domain string) ManagerOption {
base, err := publicsuffix.EffectiveTLDPlusOne(domain) // base, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil { // if err != nil {
return newErrorManagerOption(err, "publicsuffix") // return newErrorManagerOption(err, "publicsuffix")
} // }
//
suffix := strings.TrimSuffix(domain, base) // suffix := strings.TrimSuffix(domain, base)
if suffix != "" { // if suffix != "" {
suffix = "." + suffix[:len(suffix)-1] // suffix = "." + suffix[:len(suffix)-1]
} // }
//
return func(mgr *Manager) error { // return func(mgr *Manager) error {
mgr.domain = base // mgr.domain = base
mgr.suffix = suffix // mgr.suffix = suffix
return nil // return nil
} // }
} // }
//
// NewManager creates a DNS manager with the // // NewManager creates a DNS manager with the
func NewManager(opts ...ManagerOption) (*Manager, error) { // func NewManager(opts ...ManagerOption) (*Manager, error) {
mgr := new(Manager) // mgr := new(Manager)
//
for _, opt := range opts { // for _, opt := range opts {
if err := opt(mgr); err != nil { // if err := opt(mgr); err != nil {
return nil, err // return nil, err
} // }
} // }
//
if err := mgr.setDefaults(); err != nil { // if err := mgr.setDefaults(); err != nil {
return nil, err // return nil, err
} // }
return mgr, nil // return mgr, nil
} // }
//
+6 -14
View File
@@ -1,13 +1,7 @@
package dns package dns
import ( // func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
"bytes" // var buf bytes.Buffer
"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 // var zoneNames sort.StringSlice
// //
@@ -48,12 +42,10 @@ func (*Manager) WriteTo(w io.Writer) (int64, error) {
// mgr.writeRegionToBuffer(&buf, zones, "eu", "ssd-ams", "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, "europe", "ssd-ams", "ssd-lon", "htz-fsn")
// mgr.writeRegionToBuffer(&buf, zones, "earth", "ssd-ams", "ssd-lon", "htz-fsn") // mgr.writeRegionToBuffer(&buf, zones, "earth", "ssd-ams", "ssd-lon", "htz-fsn")
//
return buf.WriteTo(w) // 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)