Compare commits

..

12 Commits

Author SHA1 Message Date
amery ab8ffdd507 Merge pull request 'zones: warn but not fail when scanning finds unknown monitors' (#13)
Reviewed-on: #13
2023-09-11 15:15:19 +02:00
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 c038ad4431 zones: warn but not fail when scanning finds unknown monitors
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-08 12:49:00 +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
13 changed files with 149 additions and 137 deletions
+3 -2
View File
@@ -52,8 +52,9 @@ const encoding = YAMLEncoding
// Command
var dumpCmd = &cobra.Command{
Use: "dump",
Short: "generates a text representation of the config",
Use: "dump",
Short: "generates a text representation of the config",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
var buf bytes.Buffer
var enc Encoder
+3 -2
View File
@@ -8,8 +8,9 @@ import (
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
Use: "env",
Short: "generates environment variables for shell scripts",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
+3 -2
View File
@@ -19,8 +19,9 @@ var gatewayCmd = &cobra.Command{
// gateway set
var gatewaySetCmd = &cobra.Command{
Use: "set",
Short: "gateway set sets machines as gateways",
Use: "set",
Short: "gateway set sets machines as gateways",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, args []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
-6
View File
@@ -3,15 +3,9 @@ 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()
+22 -4
View File
@@ -2,6 +2,8 @@
package main
import (
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@@ -10,13 +12,29 @@ const (
CmdName = "jpictl"
)
var rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
var (
log = zerolog.New(nil, slog.Error)
verbosity int
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))
}
+3 -2
View File
@@ -6,8 +6,9 @@ import (
// Command
var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
Use: "write",
Short: "rewrites all config files",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
+12 -92
View File
@@ -1,89 +1,12 @@
package zones
import (
"net/netip"
"os"
"strings"
"darvaza.org/core"
"darvaza.org/slog"
"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
@@ -111,26 +34,24 @@ func (todo *cephScanTODO) checkMachine(p *Machine) bool {
return false
}
func (todo *cephScanTODO) Missing() error {
var check CephMissingMonitorError
func (todo *cephScanTODO) LogMissing(log slog.Logger) {
for name, found := range todo.names {
if !found {
check.appendName(name)
log.Warn().
WithField("subsystem", "ceph").
WithField("monitor", name).
Print("unknown monitor")
}
}
for addr, found := range todo.addrs {
if !found {
var a netip.Addr
// it wouldn't be on the map if it wasn't valid
_ = a.UnmarshalText([]byte(addr))
check.appendAddr(a)
log.Warn().
WithField("subsystem", "ceph").
WithField("monitor", addr).
Print("unknown monitor")
}
}
return check.AsError()
}
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
@@ -169,9 +90,8 @@ func (m *Zones) scanCephMonitors(_ *ScanOptions) error {
p.CephMonitor = todo.checkMachine(p)
return false
})
if err := todo.Missing(); err != nil {
return core.Wrap(err, "ceph")
}
todo.LogMissing(m.log)
}
// make sure every zone has one
+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
type Machine struct {
zone *Zone
logger
zone *Zone
logger `toml:"-" json:"-" yaml:"-"`
ID int `toml:"id"`
Name string `toml:"-" json:"-" yaml:"-"`
+22 -10
View File
@@ -2,6 +2,7 @@ package zones
import (
"bytes"
"errors"
"fmt"
"os"
@@ -121,23 +122,30 @@ 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 wg%v address: %s", m.Name, ring, addr)
return fmt.Errorf("%s: invalid address", addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
return core.Wrapf(err, "%s: invalid address", addr)
}
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
return core.Wrap(err, "interface")
}
for _, peer := range wg.Peer {
if err := m.applyWireguardPeerConfig(ring, peer); err != nil {
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr)
return err
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")
}
}
@@ -158,6 +166,10 @@ 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
}
@@ -183,8 +195,10 @@ 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{
@@ -197,8 +211,6 @@ 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 {
+22 -3
View File
@@ -4,7 +4,10 @@ import (
"context"
"net/netip"
"strconv"
"strings"
"time"
"darvaza.org/core"
)
// 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 {
if err := m.setID(); err != nil {
return err
return core.Wrap(err, m.Name)
}
for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil {
return err
return core.Wrap(err, m.Name)
}
}
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
@@ -57,6 +71,11 @@ 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
}
}
+39 -10
View File
@@ -3,6 +3,8 @@ package zones
import (
"io/fs"
"sort"
"darvaza.org/core"
)
func (m *Zones) scan(opts *ScanOptions) error {
@@ -31,23 +33,40 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
for _, e := range entries {
if e.IsDir() {
z := &Zone{
zones: m,
logger: m,
Name: e.Name(),
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)
}
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 {
@@ -138,7 +157,17 @@ func (z *Zone) scan() error {
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
}
+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
type Zone struct {
zones *Zones
logger
zones *Zones
logger `toml:"-" json:"-" yaml:"-"`
ID int `toml:"id"`
Name string `toml:"name"`