zones: add structured logs to zone scanning #14

Merged
amery merged 2 commits from pr-amery-logs into main 1 year ago
  1. 5
      cmd/jpictl/dump.go
  2. 5
      cmd/jpictl/env.go
  3. 5
      cmd/jpictl/gateway.go
  4. 6
      cmd/jpictl/log.go
  5. 26
      cmd/jpictl/main.go
  6. 5
      cmd/jpictl/write.go
  7. 21
      pkg/zones/machine_rings.go
  8. 5
      pkg/zones/machine_scan.go
  9. 20
      pkg/zones/scan.go

5
cmd/jpictl/dump.go

@ -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

5
cmd/jpictl/env.go

@ -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 {

5
cmd/jpictl/gateway.go

@ -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
cmd/jpictl/log.go

@ -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()

26
cmd/jpictl/main.go

@ -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))
}

5
cmd/jpictl/write.go

@ -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 {

21
pkg/zones/machine_rings.go

@ -122,17 +122,15 @@ 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 {
@ -140,9 +138,14 @@ func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
switch { switch {
case errors.Is(err, ErrUnknownNode): case errors.Is(err, ErrUnknownNode):
// ignore unknown peers // 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: case err != nil:
err = core.Wrapf(err, "%s: wg%v:%s", m.Name, ring, addr) return core.Wrap(err, "peer")
return err
} }
} }
@ -163,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
} }

5
pkg/zones/machine_scan.go

@ -71,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
} }
} }

20
pkg/zones/scan.go

@ -37,7 +37,11 @@ func (m *Zones) scanDirectory(_ *ScanOptions) error {
switch { switch {
case err != nil: case err != nil:
return core.Wrap(err, e.Name()) 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) m.Zones = append(m.Zones, z)
} }
} }
@ -53,6 +57,10 @@ func (m *Zones) newZone(name string) (*Zone, error) {
Name: name, Name: name,
} }
z.debug().
WithField("zone", z.Name).
Print("found")
if err := z.scan(); err != nil { if err := z.scan(); err != nil {
return nil, err return nil, err
} }
@ -149,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
} }

Loading…
Cancel
Save