Compare commits

..

2 Commits

Author SHA1 Message Date
amery 379794cedd zones: introduce (private) logger interface
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 13:57:18 +00:00
amery 24f5232ff6 zones: introduce WithLogger() scan option
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-07 13:57:18 +00:00
14 changed files with 137 additions and 154 deletions
+2 -3
View File
@@ -52,9 +52,8 @@ const encoding = YAMLEncoding
// Command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "generates a text representation of the config",
PreRun: setVerbosity,
Use: "dump",
Short: "generates a text representation of the config",
RunE: func(_ *cobra.Command, _ []string) error {
var buf bytes.Buffer
var enc Encoder
+2 -3
View File
@@ -8,9 +8,8 @@ import (
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
PreRun: setVerbosity,
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
+2 -3
View File
@@ -19,9 +19,8 @@ var gatewayCmd = &cobra.Command{
// gateway set
var gatewaySetCmd = &cobra.Command{
Use: "set",
Short: "gateway set sets machines as gateways",
PreRun: setVerbosity,
Use: "set",
Short: "gateway set sets machines as gateways",
RunE: func(_ *cobra.Command, args []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
+6
View File
@@ -3,9 +3,15 @@ package main
import (
"fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
)
// TODO: make log level configurable via flags
var (
log = zerolog.New(nil, slog.Debug)
)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) {
l := log.Fatal()
+4 -22
View File
@@ -2,8 +2,6 @@
package main
import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@@ -12,29 +10,13 @@ const (
CmdName = "jpictl"
)
var (
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
)
var rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
func main() {
if err := rootCmd.Execute(); err != nil {
fatal(err, "")
}
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v", "increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = log.WithLevel(slog.LogLevel(desired))
}
+2 -3
View File
@@ -6,9 +6,8 @@ import (
// Command
var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
PreRun: setVerbosity,
Use: "write",
Short: "rewrites all config files",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
+92 -12
View File
@@ -1,12 +1,89 @@
package zones
import (
"net/netip"
"os"
"strings"
"darvaza.org/slog"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/ceph"
)
// CephMissingMonitorError is an error that contains ceph
// monitors present in ceph.conf but not found on the cluster
type CephMissingMonitorError struct {
Names []string
Addrs []netip.Addr
}
func (err *CephMissingMonitorError) appendName(name string) {
err.Names = append(err.Names, name)
}
func (err *CephMissingMonitorError) appendAddr(addr netip.Addr) {
err.Addrs = append(err.Addrs, addr)
}
// OK tells if this instance actual shouldn't be treated as an error
func (err CephMissingMonitorError) OK() bool {
switch {
case len(err.Names) > 0:
return false
case len(err.Addrs) > 0:
return false
default:
return true
}
}
func (err CephMissingMonitorError) Error() string {
if !err.OK() {
var buf strings.Builder
_, _ = buf.WriteString("missing:")
err.writeNames(&buf)
err.writeAddrs(&buf)
return buf.String()
}
// no error
return ""
}
func (err *CephMissingMonitorError) writeNames(w *strings.Builder) {
if len(err.Names) > 0 {
_, _ = w.WriteString(" mon_initial_members:")
for i, name := range err.Names {
if i != 0 {
_, _ = w.WriteRune(',')
}
_, _ = w.WriteString(name)
}
}
}
func (err *CephMissingMonitorError) writeAddrs(w *strings.Builder) {
if len(err.Addrs) > 0 {
_, _ = w.WriteString(" mon_host:")
for i, addr := range err.Addrs {
if i != 0 {
_, _ = w.WriteRune(',')
}
_, _ = w.WriteString(addr.String())
}
}
}
// AsError returns nil if the instance is actually OK
func (err *CephMissingMonitorError) AsError() error {
if err == nil || err.OK() {
return nil
}
return err
}
type cephScanTODO struct {
names map[string]bool
addrs map[string]bool
@@ -34,24 +111,26 @@ func (todo *cephScanTODO) checkMachine(p *Machine) bool {
return false
}
func (todo *cephScanTODO) LogMissing(log slog.Logger) {
func (todo *cephScanTODO) Missing() error {
var check CephMissingMonitorError
for name, found := range todo.names {
if !found {
log.Warn().
WithField("subsystem", "ceph").
WithField("monitor", name).
Print("unknown monitor")
check.appendName(name)
}
}
for addr, found := range todo.addrs {
if !found {
log.Warn().
WithField("subsystem", "ceph").
WithField("monitor", addr).
Print("unknown monitor")
var a netip.Addr
// it wouldn't be on the map if it wasn't valid
_ = a.UnmarshalText([]byte(addr))
check.appendAddr(a)
}
}
return check.AsError()
}
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
@@ -90,8 +169,9 @@ func (m *Zones) scanCephMonitors(_ *ScanOptions) error {
p.CephMonitor = todo.checkMachine(p)
return false
})
todo.LogMissing(m.log)
if err := todo.Missing(); err != nil {
return core.Wrap(err, "ceph")
}
}
// make sure every zone has one
-16
View File
@@ -1,16 +0,0 @@
package zones
import "errors"
var (
// ErrInvalidName indicates the name isn't valid
ErrInvalidName = errors.New("invalid name")
// ErrUnknownNode indicates there is a reference to a node
// we don't have on the tree
ErrUnknownNode = errors.New("node does not exist")
// ErrInvalidNode indicates the nodes can't be used for
// the intended purpose
ErrInvalidNode = errors.New("invalid node")
)
+1 -3
View File
@@ -9,9 +9,7 @@ import (
// A Machine is a machine on a Zone
type Machine struct {
zone *Zone
logger `toml:"-" json:"-" yaml:"-"`
zone *Zone
ID int `toml:"id"`
Name string `toml:"-" json:"-" yaml:"-"`
+10 -22
View File
@@ -2,7 +2,6 @@ package zones
import (
"bytes"
"errors"
"fmt"
"os"
@@ -122,30 +121,23 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress()
zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid address", addr)
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return core.Wrapf(err, "%s: invalid address", addr)
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
return core.Wrap(err, "interface")
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
for _, peer := range wg.Peer {
err := m.applyWireguardPeerConfig(ring, peer)
switch {
case errors.Is(err, ErrUnknownNode):
// ignore unknown peers
m.warn(nil).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("peer", peer.Endpoint.Host).
WithField("ring", ring).
Print("ignoring unknown endpoint")
case err != nil:
return core.Wrap(err, "peer")
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
}
}
@@ -166,10 +158,6 @@ func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
cur, _ := m.getRingInfo(ring)
if cur == nil {
// first, append
m.debug().
WithField("node", m.Name).
WithField("ring", ring).
Print("found")
m.Rings = append(m.Rings, new)
return nil
}
@@ -195,10 +183,8 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
switch {
case !found:
// unknown
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
case ring == 1 && m.zone != peer.zone:
// invalid zone
return core.Wrap(ErrInvalidNode, peer.Name)
default:
// apply RingInfo
ri := &RingInfo{
@@ -211,6 +197,8 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
return peer.applyRingInfo(ring, ri)
}
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
}
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
+3 -22
View File
@@ -4,10 +4,7 @@ import (
"context"
"net/netip"
"strconv"
"strings"
"time"
"darvaza.org/core"
)
// LookupNetIP uses the DNS Resolver to get the public addresses associated
@@ -33,32 +30,21 @@ func (m *Machine) UpdatePublicAddresses() error {
func (m *Machine) init() error {
if err := m.setID(); err != nil {
return core.Wrap(err, m.Name)
return err
}
for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil {
return core.Wrap(err, m.Name)
return err
}
}
return nil
}
func (m *Machine) setID() error {
zoneName := m.zone.Name
suffix := m.Name[len(zoneName)+1:]
l := len(zoneName)
switch {
case len(m.Name) < l+2:
return ErrInvalidName
case !strings.HasPrefix(m.Name, zoneName):
return ErrInvalidName
case m.Name[l] != '-':
return ErrInvalidName
}
suffix := m.Name[l+1:]
id, err := strconv.ParseInt(suffix, 10, 8)
if err != nil {
return err
@@ -71,11 +57,6 @@ func (m *Machine) setID() error {
func (m *Machine) scan(opts *ScanOptions) error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", i).
Print()
return err
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ type ScanOptions struct {
// Logger specifies the logger to be used. otherwise
// the scanner will be mute
slog.Logger
Logger slog.Logger
}
// ResolvePublicAddresses instructs the scanner to use
+11 -42
View File
@@ -3,8 +3,6 @@ package zones
import (
"io/fs"
"sort"
"darvaza.org/core"
)
func (m *Zones) scan(opts *ScanOptions) error {
@@ -33,40 +31,22 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
for _, e := range entries {
if e.IsDir() {
z, err := m.newZone(e.Name())
switch {
case err != nil:
return core.Wrap(err, e.Name())
case z.Machines.Len() == 0:
z.warn(nil).
WithField("zone", z.Name).
Print("empty")
default:
m.Zones = append(m.Zones, z)
z := &Zone{
zones: m,
Name: e.Name(),
}
if err := z.scan(); err != nil {
return err
}
m.Zones = append(m.Zones, z)
}
}
return nil
}
func (m *Zones) newZone(name string) (*Zone, error) {
z := &Zone{
zones: m,
logger: m,
Name: name,
}
z.debug().
WithField("zone", z.Name).
Print("found")
if err := z.scan(); err != nil {
return nil, err
}
return z, nil
}
func (m *Zones) scanMachines(opts *ScanOptions) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
@@ -152,22 +132,11 @@ func (z *Zone) scan() error {
for _, e := range entries {
if e.IsDir() {
m := &Machine{
zone: z,
logger: z,
Name: e.Name(),
zone: z,
Name: e.Name(),
}
m.debug().
WithField("node", m.Name).
WithField("zone", z.Name).
Print("found")
if err := m.init(); err != nil {
m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err
}
+1 -2
View File
@@ -87,8 +87,7 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int)
// Zone represents one zone in a cluster
type Zone struct {
zones *Zones
logger `toml:"-" json:"-" yaml:"-"`
zones *Zones
ID int `toml:"id"`
Name string `toml:"name"`