cluster: introduce NodeID and ZoneID types

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2024-05-28 19:40:18 +00:00
parent e1186975a6
commit 6c875ae832
11 changed files with 72 additions and 57 deletions
+11 -11
View File
@@ -81,8 +81,8 @@ func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
type RingAddressEncoder struct {
ID int
Port uint16
Encode func(zoneID, nodeID int) (netip.Addr, bool)
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
Encode func(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool)
Decode func(addr netip.Addr) (zoneID ZoneID, nodeID NodeID, ok bool)
}
var (
@@ -110,7 +110,7 @@ var (
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
//
// 0 is reserved, and only allowed when composing CIDRs.
func ValidZoneID(zoneID int) bool {
func ValidZoneID(zoneID ZoneID) bool {
switch {
case zoneID < 0 || zoneID > MaxZoneID:
return false
@@ -122,7 +122,7 @@ func ValidZoneID(zoneID int) bool {
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
// nodeID is unique within a Zone.
// 0 is reserved, and only allowed when composing CIDRs.
func ValidNodeID(nodeID int) bool {
func ValidNodeID(nodeID NodeID) bool {
switch {
case nodeID < 0 || nodeID > MaxNodeID:
return false
@@ -133,19 +133,19 @@ func ValidNodeID(nodeID int) bool {
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
func ParseRingZeroAddress(addr netip.Addr) (zoneID ZoneID, nodeID NodeID, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
return ZoneID(a4[2]), NodeID(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
func RingZeroAddress(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
@@ -157,13 +157,13 @@ func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
func ParseRingOneAddress(addr netip.Addr) (zoneID ZoneID, nodeID NodeID, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
zoneID = ZoneID(a4[1] >> 4)
nodeID = NodeID(a4[3])
return zoneID, nodeID, true
}
}
@@ -171,7 +171,7 @@ func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
func RingOneAddress(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false