From aca0a5e83463fe27a3d13d4f1adef79f5fb1e4b9 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 24 Aug 2023 17:55:43 +0000 Subject: [PATCH] zones: calculate Machine.ID on init Signed-off-by: Alejandro Mery --- pkg/zones/env.go | 6 ++---- pkg/zones/machine.go | 24 +----------------------- pkg/zones/machine_rings.go | 4 ++-- pkg/zones/machine_scan.go | 18 ++++++++++++++++++ pkg/zones/scan.go | 4 ++-- 5 files changed, 25 insertions(+), 31 deletions(-) diff --git a/pkg/zones/env.go b/pkg/zones/env.go index 59eaadc..f2ec1a2 100644 --- a/pkg/zones/env.go +++ b/pkg/zones/env.go @@ -82,14 +82,12 @@ func getRingZeroGatewayID(z *Zone) int { var firstNodeID, gatewayID int z.ForEachMachine(func(p *Machine) bool { - nodeID := p.ID() - if firstNodeID == 0 { - firstNodeID = nodeID + firstNodeID = p.ID } if _, found := p.getRingInfo(0); found { - gatewayID = nodeID + gatewayID = p.ID } return gatewayID != 0 diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index 09dd2fa..5a9713f 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -5,7 +5,6 @@ import ( "io/fs" "net/netip" "path/filepath" - "strconv" "strings" "sync" ) @@ -15,7 +14,7 @@ type Machine struct { mu sync.Mutex zone *Zone - id int + ID int Name string `toml:"name"` PublicAddresses []netip.Addr `toml:"public,omitempty"` @@ -26,27 +25,6 @@ func (m *Machine) String() string { return m.Name } -// ID return the index within the [Zone] associated to this [Machine] -func (m *Machine) ID() int { - m.mu.Lock() - defer m.mu.Unlock() - - if m.id == 0 { - zoneName := m.zone.Name - - s := m.Name[len(zoneName)+1:] - - id, err := strconv.ParseInt(s, 10, 8) - if err != nil { - panic(err) - } - - m.id = int(id) - } - - return m.id -} - // FullName returns the Name of the machine including domain name func (m *Machine) FullName() string { if domain := m.zone.zones.domain; domain != "" { diff --git a/pkg/zones/machine_rings.go b/pkg/zones/machine_rings.go index 43a55cf..f5d2915 100644 --- a/pkg/zones/machine_rings.go +++ b/pkg/zones/machine_rings.go @@ -195,8 +195,8 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error { return fmt.Errorf("invalid %s", "zoneID") case nodeID == 0: return fmt.Errorf("invalid %s", "nodeID") - case m.ID() != nodeID: - return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID(), nodeID) + case m.ID != nodeID: + return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID, nodeID) case m.zone.ID != 0 && m.zone.ID != zoneID: return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID) case m.zone.ID == 0: diff --git a/pkg/zones/machine_scan.go b/pkg/zones/machine_scan.go index c676644..ee46203 100644 --- a/pkg/zones/machine_scan.go +++ b/pkg/zones/machine_scan.go @@ -3,6 +3,7 @@ package zones import ( "context" "net/netip" + "strconv" "time" ) @@ -26,6 +27,10 @@ func (m *Machine) updatePublicAddresses() error { } func (m *Machine) init() error { + if err := m.setID(); err != nil { + return err + } + for i := 0; i < RingsCount; i++ { if err := m.tryReadWireguardKeys(i); err != nil { return err @@ -34,6 +39,19 @@ func (m *Machine) init() error { return nil } +func (m *Machine) setID() error { + zoneName := m.zone.Name + suffix := m.Name[len(zoneName)+1:] + + id, err := strconv.ParseInt(suffix, 10, 8) + if err != nil { + return err + } + + m.ID = int(id) + return nil +} + func (m *Machine) scan() error { for i := 0; i < RingsCount; i++ { if err := m.tryApplyWireguardConfig(i); err != nil { diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index 418fe18..28b77d0 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -93,8 +93,8 @@ func (m *Zones) scanSort() error { m.ForEachZone(func(z *Zone) bool { sort.SliceStable(z.Machines, func(i, j int) bool { - id1 := z.Machines[i].ID() - id2 := z.Machines[j].ID() + id1 := z.Machines[i].ID + id2 := z.Machines[j].ID return id1 < id2 })