Compare commits

...

2 Commits

Author SHA1 Message Date
amery a3e3cde4c4 zones: fix scanMachines to run after all zones have been read
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 02:54:35 +00:00
amery a4a10d0226 zones: add helpers to compose and parse wg0/wg1 addresses
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 02:50:54 +00:00
2 changed files with 61 additions and 12 deletions
+49
View File
@@ -0,0 +1,49 @@
package zones
import "net/netip"
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) netip.Addr {
c := zoneID
d := nodeID
return netip.AddrFrom4([4]byte{
10, 0, uint8(c), uint8(d),
})
}
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
return zoneID, nodeID, true
}
}
return 0, 0, false
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) netip.Addr {
b := zoneID << 4
d := nodeID
return netip.AddrFrom4([4]byte{
10, uint8(b), 0, uint8(d),
})
}
+12 -12
View File
@@ -26,7 +26,17 @@ func (m *Zones) scan() error {
}
}
return nil
return m.scanMachines()
}
func (m *Zones) scanMachines() error {
var err error
m.ForEachMachine(func(p *Machine) {
if err == nil {
err = p.scan()
}
})
return err
}
func (z *Zone) scan() error {
@@ -47,15 +57,5 @@ func (z *Zone) scan() error {
}
}
return z.scanMachines()
}
func (z *Zone) scanMachines() error {
var err error
z.zones.ForEachMachine(func(m *Machine) {
if err == nil {
err = m.scan()
}
})
return err
return nil
}