Compare commits

..

2 Commits

Author SHA1 Message Date
amery 6bf897f681 zones: Env: allow multiple gateways on a Zone
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:52:40 +01:00
amery 0a3e63e1a2 zones: Env: minor tidy up
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:52:40 +01:00
10 changed files with 58 additions and 191 deletions
+2 -4
View File
@@ -14,8 +14,6 @@ var cfg = &Config{
}
// LoadZones loads all zones and machines in the config directory
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
return zones.New(cfg.Base, cfg.Domain,
zones.ResolvePublicAddresses(resolve),
)
func (cfg *Config) LoadZones() (*zones.Zones, error) {
return zones.New(cfg.Base, cfg.Domain)
}
+1 -1
View File
@@ -58,7 +58,7 @@ var dumpCmd = &cobra.Command{
var buf bytes.Buffer
var enc Encoder
m, err := cfg.LoadZones(true)
m, err := cfg.LoadZones()
if err != nil {
return err
}
+1 -1
View File
@@ -11,7 +11,7 @@ var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
m, err := cfg.LoadZones()
if err != nil {
return err
}
+1 -1
View File
@@ -9,7 +9,7 @@ var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
m, err := cfg.LoadZones()
if err != nil {
return err
}
+3 -8
View File
@@ -65,16 +65,11 @@ func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any
var s string
if n := len(value); n > 0 {
var buf bytes.Buffer
ss := make([]string, len(value))
for i, v := range value {
if i != 0 {
_, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%v", v)
ss[i] = fmt.Sprintf("%v", v)
}
s = buf.String()
s = strings.Join(ss, " ")
}
m.writeEnvVar(w, s, name, args...)
+6 -12
View File
@@ -7,9 +7,8 @@ import (
"time"
)
// LookupNetIP uses the DNS Resolver to get the public addresses associated
// to a Machine
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
timeout := 2 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
@@ -17,9 +16,8 @@ func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
}
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
func (m *Machine) UpdatePublicAddresses() error {
addrs, err := m.LookupNetIP(2 * time.Second)
func (m *Machine) updatePublicAddresses() error {
addrs, err := m.lookupNetIP()
if err != nil {
return err
}
@@ -54,16 +52,12 @@ func (m *Machine) setID() error {
return nil
}
func (m *Machine) scan(opts *ScanOptions) error {
func (m *Machine) scan() error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {
return err
}
}
if !opts.DontResolvePublicAddresses {
return m.UpdatePublicAddresses()
}
return nil
return m.updatePublicAddresses()
}
-109
View File
@@ -1,109 +0,0 @@
package zones
import (
"io/fs"
"path/filepath"
"darvaza.org/resolver"
"github.com/hack-pad/hackpadfs/os"
)
// A ScanOption preconfigures the Zones before scanning
type ScanOption func(*Zones, *ScanOptions) error
// ScanOptions contains flags used by the initial scan
type ScanOptions struct {
// DontResolvePublicAddresses indicates we shouldn't
// pre-populate Machine.PublicAddresses during the
// initial scan
DontResolvePublicAddresses bool
}
// ResolvePublicAddresses instructs the scanner to use
// the DNS resolver to get PublicAddresses of nodes.
// Default is true
func ResolvePublicAddresses(resolve bool) ScanOption {
return func(m *Zones, opt *ScanOptions) error {
opt.DontResolvePublicAddresses = !resolve
return nil
}
}
// WithLookuper specifies what resolver.Lookuper to use to
// find public addresses
func WithLookuper(h resolver.Lookuper) ScanOption {
return func(m *Zones, opt *ScanOptions) error {
if h == nil {
return fs.ErrInvalid
}
m.resolver = resolver.NewResolver(h)
return nil
}
}
// WithResolver specifies what resolver to use to find
// public addresses. if nil is passed, the [net.Resolver] will be used.
// The default is using Cloudflare's 1.1.1.1.
func WithResolver(h resolver.Resolver) ScanOption {
return func(m *Zones, opt *ScanOptions) error {
if h == nil {
h = resolver.SystemResolver(true)
}
m.resolver = h
return nil
}
}
func (m *Zones) setDefaults(opt *ScanOptions) error {
if m.resolver == nil {
h := resolver.NewCloudflareLookuper()
if err := WithLookuper(h)(m, opt); err != nil {
return err
}
}
return nil
}
// NewFS builds a [Zones] tree using the given directory
func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Zones, error) {
var scanOptions ScanOptions
z := &Zones{
dir: dir,
domain: domain,
}
for _, opt := range opts {
if err := opt(z, &scanOptions); err != nil {
return nil, err
}
}
if err := z.setDefaults(&scanOptions); err != nil {
return nil, err
}
if err := z.scan(&scanOptions); err != nil {
return nil, err
}
return z, nil
}
// New builds a [Zones] tree using the given directory
func New(dir, domain string, opts ...ScanOption) (*Zones, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
if err != nil {
return nil, err
}
return NewFS(base, domain, opts...)
}
+9 -9
View File
@@ -5,15 +5,15 @@ import (
"sort"
)
func (m *Zones) scan(opts *ScanOptions) error {
for _, fn := range []func(*ScanOptions) error{
func (m *Zones) scan() error {
for _, fn := range []func() error{
m.scanDirectory,
m.scanMachines,
m.scanZoneIDs,
m.scanSort,
m.scanGateways,
} {
if err := fn(opts); err != nil {
if err := fn(); err != nil {
return err
}
}
@@ -21,7 +21,7 @@ func (m *Zones) scan(opts *ScanOptions) error {
return nil
}
func (m *Zones) scanDirectory(_ *ScanOptions) error {
func (m *Zones) scanDirectory() error {
// each directory is a zone
entries, err := fs.ReadDir(m.dir, ".")
if err != nil {
@@ -46,16 +46,16 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
return nil
}
func (m *Zones) scanMachines(opts *ScanOptions) error {
func (m *Zones) scanMachines() error {
var err error
m.ForEachMachine(func(p *Machine) bool {
err = p.scan(opts)
err = p.scan()
return err != nil
})
return err
}
func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
func (m *Zones) scanZoneIDs() error {
var hasMissing bool
var lastZoneID int
@@ -85,7 +85,7 @@ func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
return nil
}
func (m *Zones) scanSort(_ *ScanOptions) error {
func (m *Zones) scanSort() error {
sort.SliceStable(m.Zones, func(i, j int) bool {
id1 := m.Zones[i].ID
id2 := m.Zones[j].ID
@@ -111,7 +111,7 @@ func (m *Zones) scanSort(_ *ScanOptions) error {
return nil
}
func (m *Zones) scanGateways(_ *ScanOptions) error {
func (m *Zones) scanGateways() error {
var err error
m.ForEachZone(func(z *Zone) bool {
+35
View File
@@ -3,8 +3,11 @@ package zones
import (
"io/fs"
"path/filepath"
"sort"
"github.com/hack-pad/hackpadfs/os"
"darvaza.org/resolver"
)
@@ -187,3 +190,35 @@ func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
return out, out != nil
}
// NewFS builds a [Zones] tree using the given directory
func NewFS(dir fs.FS, domain string) (*Zones, error) {
lockuper := resolver.NewCloudflareLookuper()
z := &Zones{
dir: dir,
resolver: resolver.NewResolver(lockuper),
domain: domain,
}
if err := z.scan(); err != nil {
return nil, err
}
return z, nil
}
// New builds a [Zones] tree using the given directory
func New(dir, domain string) (*Zones, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
if err != nil {
return nil, err
}
return NewFS(base, domain)
}
-46
View File
@@ -1,46 +0,0 @@
package zones
import (
"fmt"
"io"
"os"
fs "github.com/hack-pad/hackpadfs"
)
// OpenFile opens a file on the cluster's config directory with the specified flags
func (m *Zones) OpenFile(name string, flags int, args ...any) (fs.File, error) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.OpenFile(m.dir, name, flags, 0644)
}
// CreateTruncFile creates or truncates a file on the cluster's config directory
func (m *Zones) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
}
// CreateFile creates a file on the cluster's config directory
func (m *Zones) CreateFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE, args...)
}
func (m *Zones) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
if err != nil {
return nil, err
}
return f.(io.WriteCloser), nil
}
// ReadFile reads a file from the cluster's config directory
func (m *Zones) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.ReadFile(m.dir, name)
}