From 2016b27707ba86cbbe59e54f1ca79af93a437800 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 7 Sep 2023 17:57:23 +0000 Subject: [PATCH 1/2] zones: add structured logs to zone scanning Signed-off-by: Alejandro Mery --- pkg/zones/machine_rings.go | 21 ++++++++++++++------- pkg/zones/machine_scan.go | 5 +++++ pkg/zones/scan.go | 20 +++++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/pkg/zones/machine_rings.go b/pkg/zones/machine_rings.go index af811e3..4baaaee 100644 --- a/pkg/zones/machine_rings.go +++ b/pkg/zones/machine_rings.go @@ -122,17 +122,15 @@ 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 { @@ -140,9 +138,14 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error { 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: - err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr) - return err + return core.Wrap(err, "peer") } } @@ -163,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 } diff --git a/pkg/zones/machine_scan.go b/pkg/zones/machine_scan.go index aca379e..cb5a1be 100644 --- a/pkg/zones/machine_scan.go +++ b/pkg/zones/machine_scan.go @@ -71,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 } } diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index 20a73ff..c7b4eef 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -37,7 +37,11 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error { switch { case err != nil: return core.Wrap(err, e.Name()) - case z.Machines.Len() > 0: + case z.Machines.Len() == 0: + z.warn(nil). + WithField("zone", z.Name). + Print("empty") + default: m.Zones = append(m.Zones, z) } } @@ -53,6 +57,10 @@ func (m *Zones) newZone(name string) (*Zone, error) { Name: name, } + z.debug(). + WithField("zone", z.Name). + Print("found") + if err := z.scan(); err != nil { return nil, err } @@ -149,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 } From d1198328f64aea9162499ab614230685f3047dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nagy=20K=C3=A1roly=20G=C3=A1briel?= Date: Sun, 10 Sep 2023 13:12:50 +0300 Subject: [PATCH 2/2] jpictl: introduce log verbosity flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nagy Károly Gábriel --- cmd/jpictl/dump.go | 5 +++-- cmd/jpictl/env.go | 5 +++-- cmd/jpictl/gateway.go | 5 +++-- cmd/jpictl/log.go | 6 ------ cmd/jpictl/main.go | 26 ++++++++++++++++++++++---- cmd/jpictl/write.go | 5 +++-- 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/cmd/jpictl/dump.go b/cmd/jpictl/dump.go index fb892ba..8a694f1 100644 --- a/cmd/jpictl/dump.go +++ b/cmd/jpictl/dump.go @@ -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 diff --git a/cmd/jpictl/env.go b/cmd/jpictl/env.go index 4b21773..224d92d 100644 --- a/cmd/jpictl/env.go +++ b/cmd/jpictl/env.go @@ -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 { diff --git a/cmd/jpictl/gateway.go b/cmd/jpictl/gateway.go index 1004db3..41b869b 100644 --- a/cmd/jpictl/gateway.go +++ b/cmd/jpictl/gateway.go @@ -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 { diff --git a/cmd/jpictl/log.go b/cmd/jpictl/log.go index bb5f65c..709f5b7 100644 --- a/cmd/jpictl/log.go +++ b/cmd/jpictl/log.go @@ -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() diff --git a/cmd/jpictl/main.go b/cmd/jpictl/main.go index 9e9f6a8..14cf10c 100644 --- a/cmd/jpictl/main.go +++ b/cmd/jpictl/main.go @@ -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)) +} diff --git a/cmd/jpictl/write.go b/cmd/jpictl/write.go index acb79c2..be9117f 100644 --- a/cmd/jpictl/write.go +++ b/cmd/jpictl/write.go @@ -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 {