Browse Source

Merge pull request 'zones: add structured logs to zone scanning' (#14)

Reviewed-on: #14
pull/17/head
Alejandro Mery 1 year ago
parent
commit
422e119f88
  1. 1
      cmd/jpictl/dump.go
  2. 1
      cmd/jpictl/env.go
  3. 1
      cmd/jpictl/gateway.go
  4. 6
      cmd/jpictl/log.go
  5. 22
      cmd/jpictl/main.go
  6. 1
      cmd/jpictl/write.go
  7. 21
      pkg/zones/machine_rings.go
  8. 5
      pkg/zones/machine_scan.go
  9. 20
      pkg/zones/scan.go

1
cmd/jpictl/dump.go

@ -54,6 +54,7 @@ const encoding = YAMLEncoding
var dumpCmd = &cobra.Command{
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

1
cmd/jpictl/env.go

@ -10,6 +10,7 @@ import (
var envCmd = &cobra.Command{
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 {

1
cmd/jpictl/gateway.go

@ -21,6 +21,7 @@ var gatewayCmd = &cobra.Command{
var gatewaySetCmd = &cobra.Command{
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
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()

22
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{
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))
}

1
cmd/jpictl/write.go

@ -8,6 +8,7 @@ import (
var writeCmd = &cobra.Command{
Use: "write",
Short: "rewrites all config files",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
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()
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
}

5
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
}
}

20
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
}

Loading…
Cancel
Save