Compare commits

...

10 Commits

Author SHA1 Message Date
amery 422e119f88 Merge pull request 'zones: add structured logs to zone scanning' (#14)
Reviewed-on: #14
2023-09-11 01:46:09 +02:00
Nagy Károly Gábriel d1198328f6 jpictl: introduce log verbosity flag
Signed-off-by: Nagy Károly Gábriel <k@jpi.io>
2023-09-10 13:12:50 +03:00
amery 7795610caf Merge pull request 'zones: fix jpictl dump by explicitly omitting Machine.logger and Zone.logger' (#15)
Reviewed-on: #15
2023-09-08 21:01:25 +02:00
amery 32046fc1ec zones: fix jpictl dump by explicitly omitting Machine.logger and Zone.logger
if they were fields, as in Zones, they would be ignored automatically.
but they aren't

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 13:12:56 +00:00
amery 2016b27707 zones: add structured logs to zone scanning
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:50:22 +00:00
amery 159ccf59ac Merge pull request 'zones: improve scan validations' (#11)
Reviewed-on: #11
2023-09-08 14:45:57 +02:00
amery 6a071ba5f0 zones: ignore unknown wireguard endpoints
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:44:59 +00:00
amery 3e90e3ab1e zones: ErrUnknownNode and ErrInvalidNode
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:44:59 +00:00
amery 90dd0c1239 zones: ignore machine-less zones
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:44:57 +00:00
amery 033ca2f20e zones: validate Machine names
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:43:00 +00:00
12 changed files with 137 additions and 45 deletions
+3 -2
View File
@@ -52,8 +52,9 @@ const encoding = YAMLEncoding
// Command // Command
var dumpCmd = &cobra.Command{ var dumpCmd = &cobra.Command{
Use: "dump", Use: "dump",
Short: "generates a text representation of the config", Short: "generates a text representation of the config",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
var buf bytes.Buffer var buf bytes.Buffer
var enc Encoder var enc Encoder
+3 -2
View File
@@ -8,8 +8,9 @@ import (
// Command // Command
var envCmd = &cobra.Command{ var envCmd = &cobra.Command{
Use: "env", Use: "env",
Short: "generates environment variables for shell scripts", Short: "generates environment variables for shell scripts",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {
+3 -2
View File
@@ -19,8 +19,9 @@ var gatewayCmd = &cobra.Command{
// gateway set // gateway set
var gatewaySetCmd = &cobra.Command{ var gatewaySetCmd = &cobra.Command{
Use: "set", Use: "set",
Short: "gateway set sets machines as gateways", Short: "gateway set sets machines as gateways",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, args []string) error { RunE: func(_ *cobra.Command, args []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {
-6
View File
@@ -3,15 +3,9 @@ package main
import ( import (
"fmt" "fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog" "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() // fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) { func fatal(err error, msg string, args ...any) {
l := log.Fatal() l := log.Fatal()
+22 -4
View File
@@ -2,6 +2,8 @@
package main package main
import ( import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -10,13 +12,29 @@ const (
CmdName = "jpictl" CmdName = "jpictl"
) )
var rootCmd = &cobra.Command{ var (
Use: CmdName, log = zerolog.New(nil, slog.Error)
Short: "control tool for jpi.cloud", verbosity int
} rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
)
func main() { func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fatal(err, "") 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))
}
+3 -2
View File
@@ -6,8 +6,9 @@ import (
// Command // Command
var writeCmd = &cobra.Command{ var writeCmd = &cobra.Command{
Use: "write", Use: "write",
Short: "rewrites all config files", Short: "rewrites all config files",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error { RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false) m, err := cfg.LoadZones(false)
if err != nil { if err != nil {
+16
View File
@@ -0,0 +1,16 @@
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")
)
+2 -2
View File
@@ -9,8 +9,8 @@ import (
// A Machine is a machine on a Zone // A Machine is a machine on a Zone
type Machine struct { type Machine struct {
zone *Zone zone *Zone
logger logger `toml:"-" json:"-" yaml:"-"`
ID int `toml:"id"` ID int `toml:"id"`
Name string `toml:"-" json:"-" yaml:"-"` Name string `toml:"-" json:"-" yaml:"-"`
+22 -10
View File
@@ -2,6 +2,7 @@ package zones
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"os" "os"
@@ -121,23 +122,30 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress() addr := wg.GetAddress()
zoneID, nodeID, ok := Rings[ring].Decode(addr) zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok { if !ok {
return fmt.Errorf("%s: invalid wg%v address: %s", m.Name, ring, addr) return fmt.Errorf("%s: invalid address", addr)
} }
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil { if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr) return core.Wrapf(err, "%s: invalid address", addr)
return err
} }
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil { if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr) return core.Wrap(err, "interface")
return err
} }
for _, peer := range wg.Peer { for _, peer := range wg.Peer {
if err := m.applyWireguardPeerConfig(ring, peer); err != nil { err := m.applyWireguardPeerConfig(ring, peer)
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr) switch {
return err 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")
} }
} }
@@ -158,6 +166,10 @@ func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
cur, _ := m.getRingInfo(ring) cur, _ := m.getRingInfo(ring)
if cur == nil { if cur == nil {
// first, append // first, append
m.debug().
WithField("node", m.Name).
WithField("ring", ring).
Print("found")
m.Rings = append(m.Rings, new) m.Rings = append(m.Rings, new)
return nil return nil
} }
@@ -183,8 +195,10 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
switch { switch {
case !found: case !found:
// unknown // unknown
return core.Wrap(ErrUnknownNode, pc.Endpoint.Host)
case ring == 1 && m.zone != peer.zone: case ring == 1 && m.zone != peer.zone:
// invalid zone // invalid zone
return core.Wrap(ErrInvalidNode, peer.Name)
default: default:
// apply RingInfo // apply RingInfo
ri := &RingInfo{ ri := &RingInfo{
@@ -197,8 +211,6 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
return peer.applyRingInfo(ring, ri) return peer.applyRingInfo(ring, ri)
} }
return fmt.Errorf("%q: invalid peer endpoint", pc.Endpoint.Host)
} }
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error { func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
+22 -3
View File
@@ -4,7 +4,10 @@ import (
"context" "context"
"net/netip" "net/netip"
"strconv" "strconv"
"strings"
"time" "time"
"darvaza.org/core"
) )
// LookupNetIP uses the DNS Resolver to get the public addresses associated // LookupNetIP uses the DNS Resolver to get the public addresses associated
@@ -30,21 +33,32 @@ func (m *Machine) UpdatePublicAddresses() error {
func (m *Machine) init() error { func (m *Machine) init() error {
if err := m.setID(); err != nil { if err := m.setID(); err != nil {
return err return core.Wrap(err, m.Name)
} }
for i := 0; i < RingsCount; i++ { for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil { if err := m.tryReadWireguardKeys(i); err != nil {
return err return core.Wrap(err, m.Name)
} }
} }
return nil return nil
} }
func (m *Machine) setID() error { func (m *Machine) setID() error {
zoneName := m.zone.Name 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) id, err := strconv.ParseInt(suffix, 10, 8)
if err != nil { if err != nil {
return err return err
@@ -57,6 +71,11 @@ func (m *Machine) setID() error {
func (m *Machine) scan(opts *ScanOptions) error { func (m *Machine) scan(opts *ScanOptions) error {
for i := 0; i < RingsCount; i++ { for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil { if err := m.tryApplyWireguardConfig(i); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", i).
Print()
return err return err
} }
} }
+39 -10
View File
@@ -3,6 +3,8 @@ package zones
import ( import (
"io/fs" "io/fs"
"sort" "sort"
"darvaza.org/core"
) )
func (m *Zones) scan(opts *ScanOptions) error { func (m *Zones) scan(opts *ScanOptions) error {
@@ -31,23 +33,40 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
for _, e := range entries { for _, e := range entries {
if e.IsDir() { if e.IsDir() {
z := &Zone{ z, err := m.newZone(e.Name())
zones: m, switch {
logger: m, case err != nil:
Name: e.Name(), 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)
} }
if err := z.scan(); err != nil {
return err
}
m.Zones = append(m.Zones, z)
} }
} }
return nil 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 { func (m *Zones) scanMachines(opts *ScanOptions) error {
var err error var err error
m.ForEachMachine(func(p *Machine) bool { m.ForEachMachine(func(p *Machine) bool {
@@ -138,7 +157,17 @@ func (z *Zone) scan() error {
Name: e.Name(), Name: e.Name(),
} }
m.debug().
WithField("node", m.Name).
WithField("zone", z.Name).
Print("found")
if err := m.init(); err != nil { if err := m.init(); err != nil {
m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err return err
} }
+2 -2
View File
@@ -87,8 +87,8 @@ func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int)
// Zone represents one zone in a cluster // Zone represents one zone in a cluster
type Zone struct { type Zone struct {
zones *Zones zones *Zones
logger logger `toml:"-" json:"-" yaml:"-"`
ID int `toml:"id"` ID int `toml:"id"`
Name string `toml:"name"` Name string `toml:"name"`