Compare commits

..

9 Commits

Author SHA1 Message Date
amery b8aba9e05e WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 16:26:39 +00:00
amery 6f5ca3f235 Merge pull request 'zones: add methods to work with files at the root of m/' (#6)
Reviewed-on: #6
2023-08-28 18:24:59 +02:00
amery 296d4007ff zones: add methods to work with files at the root of m/
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 16:15:37 +00:00
amery 1a03404a07 Merge pull request 'zone.ScanOptions, custom resolver and prevent unnecessary DNS calls' (#5)
Reviewed-on: #5
2023-08-28 18:10:39 +02:00
amery 6e46d23b45 jpictl: only load Machine.PublicAddresses for jpictl dump
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:49:26 +00:00
amery 94daf5ad59 zones: export Machine.LookupNetIP() and Machine.UpdatePublicAddresses()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:49:26 +00:00
amery 0989dec5e8 zones: add ResolvePublicAddresses() ScanOption to prevent early LookupIP calls
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:49:26 +00:00
amery 216bf5aa29 zones: WithLookuper()/WithResolver()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:49:26 +00:00
amery 9af88f6593 zones: introduce ScanOption/ScanOptions for New()/NewFS()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-28 15:49:26 +00:00
14 changed files with 304 additions and 55 deletions
+4 -2
View File
@@ -14,6 +14,8 @@ var cfg = &Config{
}
// LoadZones loads all zones and machines in the config directory
func (cfg *Config) LoadZones() (*zones.Zones, error) {
return zones.New(cfg.Base, cfg.Domain)
func (cfg *Config) LoadZones(resolve bool) (*zones.Zones, error) {
return zones.New(cfg.Base, cfg.Domain,
zones.ResolvePublicAddresses(resolve),
)
}
+1 -1
View File
@@ -58,7 +58,7 @@ var dumpCmd = &cobra.Command{
var buf bytes.Buffer
var enc Encoder
m, err := cfg.LoadZones()
m, err := cfg.LoadZones(true)
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()
m, err := cfg.LoadZones(false)
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()
m, err := cfg.LoadZones(false)
if err != nil {
return err
}
+1
View File
@@ -8,6 +8,7 @@ require (
darvaza.org/sidecar v0.0.0-20230721122716-b9c54b8adbaf
darvaza.org/slog v0.5.2
github.com/burntSushi/toml v0.3.1
github.com/gofrs/uuid/v5 v5.0.0
github.com/hack-pad/hackpadfs v0.2.1
github.com/mgechev/revive v1.3.2
github.com/spf13/cobra v1.7.0
+2
View File
@@ -26,6 +26,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
+2
View File
@@ -0,0 +1,2 @@
// Package ceph deals with ceph config
package ceph
+70
View File
@@ -0,0 +1,70 @@
package ceph
import (
"bytes"
"io"
"net/netip"
"github.com/gofrs/uuid/v5"
"gopkg.in/gcfg.v1"
)
// Config represents a ceph.conf file
type Config struct {
Global GlobalConfig
}
// WriteTo writes a Wireguard [Config] onto the provided [io.Writer]
func (*Config) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
return buf.WriteTo(w)
}
// GlobalConfig represents the [global] section of a ceph.conf file
type GlobalConfig struct {
FSID uuid.UUID
Monitors []string
MonitorsAddr []netip.Addr
}
type intermediateConfig struct {
Global intermediateGlobalConfig
}
func (p intermediateConfig) Export() (*Config, error) {
var out Config
var err error
// [global]
out.Global, err = p.Global.Export()
if err != nil {
return nil, err
}
return &out, nil
}
type intermediateGlobalConfig struct {
FSID uuid.UUID
MonInitialMembers string
MonHost string
ClusterNetwork string
}
func (intermediateGlobalConfig) Export() (GlobalConfig, error) {
var out GlobalConfig
var err error
return out, err
}
func NewConfigFromReader(r io.Reader) (*Config, error) {
var temp intermediateConfig
if err := gcfg.ReadInto(&temp, r); err != nil {
return nil, err
}
return temp.Export()
}
+30
View File
@@ -0,0 +1,30 @@
package zones
import (
"bytes"
"git.jpi.io/amery/jpictl/pkg/ceph"
)
// GetCephConfig reads the ceph.conf file
func (m *Zones) GetCephConfig() (*ceph.Config, error) {
data, err := m.ReadFile("ceph.conf")
if err != nil {
return nil, err
}
r := bytes.NewReader(data)
return ceph.NewConfigFromReader(r)
}
// WriteCephConfig writes the ceph.conf file
func (m *Zones) WriteCephConfig(cfg *ceph.Config) error {
f, err := m.CreateTruncFile("ceph.conf")
if err != nil {
return err
}
defer f.Close()
_, err = cfg.WriteTo(f)
return err
}
+12 -6
View File
@@ -7,8 +7,9 @@ import (
"time"
)
func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
timeout := 2 * time.Second
// LookupNetIP uses the DNS Resolver to get the public addresses associated
// to a Machine
func (m *Machine) LookupNetIP(timeout time.Duration) ([]netip.Addr, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
@@ -16,8 +17,9 @@ func (m *Machine) lookupNetIP() ([]netip.Addr, error) {
return m.zone.zones.resolver.LookupNetIP(ctx, "ip", m.FullName())
}
func (m *Machine) updatePublicAddresses() error {
addrs, err := m.lookupNetIP()
// UpdatePublicAddresses uses the DNS Resolver to set Machine.PublicAddresses
func (m *Machine) UpdatePublicAddresses() error {
addrs, err := m.LookupNetIP(2 * time.Second)
if err != nil {
return err
}
@@ -52,12 +54,16 @@ func (m *Machine) setID() error {
return nil
}
func (m *Machine) scan() error {
func (m *Machine) scan(opts *ScanOptions) error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {
return err
}
}
return m.updatePublicAddresses()
if !opts.DontResolvePublicAddresses {
return m.UpdatePublicAddresses()
}
return nil
}
+109
View File
@@ -0,0 +1,109 @@
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...)
}
+25 -9
View File
@@ -2,18 +2,21 @@ package zones
import (
"io/fs"
"log"
"os"
"sort"
)
func (m *Zones) scan() error {
for _, fn := range []func() error{
func (m *Zones) scan(opts *ScanOptions) error {
for _, fn := range []func(*ScanOptions) error{
m.scanDirectory,
m.scanMachines,
m.scanZoneIDs,
m.scanSort,
m.scanGateways,
m.scanCephMonitors,
} {
if err := fn(); err != nil {
if err := fn(opts); err != nil {
return err
}
}
@@ -21,7 +24,7 @@ func (m *Zones) scan() error {
return nil
}
func (m *Zones) scanDirectory() error {
func (m *Zones) scanDirectory(_ *ScanOptions) error {
// each directory is a zone
entries, err := fs.ReadDir(m.dir, ".")
if err != nil {
@@ -46,16 +49,16 @@ func (m *Zones) scanDirectory() error {
return nil
}
func (m *Zones) scanMachines() error {
func (m *Zones) scanMachines(opts *ScanOptions) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
err = p.scan()
err = p.scan(opts)
return err != nil
})
return err
}
func (m *Zones) scanZoneIDs() error {
func (m *Zones) scanZoneIDs(_ *ScanOptions) error {
var hasMissing bool
var lastZoneID int
@@ -85,7 +88,7 @@ func (m *Zones) scanZoneIDs() error {
return nil
}
func (m *Zones) scanSort() error {
func (m *Zones) scanSort(_ *ScanOptions) error {
sort.SliceStable(m.Zones, func(i, j int) bool {
id1 := m.Zones[i].ID
id2 := m.Zones[j].ID
@@ -111,7 +114,7 @@ func (m *Zones) scanSort() error {
return nil
}
func (m *Zones) scanGateways() error {
func (m *Zones) scanGateways(_ *ScanOptions) error {
var err error
m.ForEachZone(func(z *Zone) bool {
@@ -121,6 +124,19 @@ func (m *Zones) scanGateways() error {
return err
}
func (m *Zones) scanCephMonitors() error {
cfg, err := m.GetCephConfig()
switch {
case os.IsNotExist(err):
err = nil
case err != nil:
return err
}
log.Print(cfg)
return nil
}
func (z *Zone) scan() error {
// each directory is a machine
entries, err := fs.ReadDir(z.zones.dir, z.Name)
-35
View File
@@ -3,11 +3,8 @@ package zones
import (
"io/fs"
"path/filepath"
"sort"
"github.com/hack-pad/hackpadfs/os"
"darvaza.org/resolver"
)
@@ -190,35 +187,3 @@ 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
@@ -0,0 +1,46 @@
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)
}