Browse Source

zones: calculate Machine.ID on init

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/1/head
Alejandro Mery 1 year ago
parent
commit
aca0a5e834
  1. 6
      pkg/zones/env.go
  2. 24
      pkg/zones/machine.go
  3. 4
      pkg/zones/machine_rings.go
  4. 18
      pkg/zones/machine_scan.go
  5. 4
      pkg/zones/scan.go

6
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

24
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 != "" {

4
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:

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

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

Loading…
Cancel
Save