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
+1 -1
View File
@@ -52,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
m.ForEachZone(func(z *cluster.Zone) bool { m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool { z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(ctx, z.Name, p.ID, p.IsActive(), p.PublicAddresses...) err = mgr.AddHost(ctx, z.Name, int(p.ID), p.IsActive(), p.PublicAddresses...)
return err != nil return err != nil
}) })
+1 -2
View File
@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -128,7 +127,7 @@ func gatewayListAll(zi cluster.ZoneIterator) error {
return false return false
} }
for _, i := range ids { for _, i := range ids {
sIDs = append(sIDs, strconv.Itoa(i)) sIDs = append(sIDs, i.String())
} }
b.WriteString(strings.Join(sIDs, ", ")) b.WriteString(strings.Join(sIDs, ", "))
b.WriteString("\n") b.WriteString("\n")
+2 -2
View File
@@ -45,7 +45,7 @@ func (m *Cluster) initZones(opts *ScanOptions) error {
func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error { func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error {
var hasMissing bool var hasMissing bool
var lastMachineID int var lastMachineID NodeID
z.zones = m z.zones = m
z.logger = m z.logger = m
@@ -58,7 +58,7 @@ func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error {
case p.ID == 0: case p.ID == 0:
hasMissing = true hasMissing = true
case p.ID > lastMachineID: case p.ID > lastMachineID:
lastMachineID = z.ID lastMachineID = p.ID
} }
return false return false
+1 -1
View File
@@ -114,7 +114,7 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
func (m *Cluster) scanZoneIDs(_ *ScanOptions) error { func (m *Cluster) scanZoneIDs(_ *ScanOptions) error {
var hasMissing bool var hasMissing bool
var lastZoneID int var lastZoneID ZoneID
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
switch { switch {
+23 -32
View File
@@ -35,8 +35,8 @@ func (m *Cluster) Env(export bool) (*Env, error) {
} }
// Zones returns the list of Zone IDs // Zones returns the list of Zone IDs
func (m *Env) Zones() []int { func (m *Env) Zones() []ZoneID {
var zones []int var zones []ZoneID
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
zones = append(zones, z.ID) zones = append(zones, z.ID)
@@ -70,8 +70,8 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
m.writeEnvVar(&buf, m.cephFSID, "FSID") m.writeEnvVar(&buf, m.cephFSID, "FSID")
} }
m.writeEnvVarStrings(&buf, m.Regions(), "REGIONS") m.writeEnvVar(&buf, genEnvStrings(m.Regions()), "REGIONS")
m.writeEnvVarInts(&buf, m.Zones(), "ZONES") m.writeEnvVar(&buf, genEnvInts(m.Zones()), "ZONES")
m.ForEachZone(func(z *Zone) bool { m.ForEachZone(func(z *Zone) bool {
m.writeEnvZone(&buf, z) m.writeEnvZone(&buf, z)
@@ -92,7 +92,7 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
// ZONE{zoneID}_GW // ZONE{zoneID}_GW
gateways, _ := z.GatewayIDs() gateways, _ := z.GatewayIDs()
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW") m.writeEnvVar(w, genEnvInts(gateways), "ZONE%v_%s", zoneID, "GW")
// ZONE{zoneID}_REGION // ZONE{zoneID}_REGION
m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION") m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION")
@@ -107,32 +107,6 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID") m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID")
} }
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) {
var buf bytes.Buffer
for _, v := range value {
if buf.Len() > 0 {
_, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%v", v)
}
m.writeEnvVar(w, buf.String(), name, args...)
}
func (m *Env) writeEnvVarStrings(w io.Writer, value []string, name string, args ...any) {
var buf bytes.Buffer
for _, v := range value {
if buf.Len() > 0 {
_, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%s", v)
}
m.writeEnvVar(w, buf.String(), name, args...)
}
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) { func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
var prefix string var prefix string
@@ -155,6 +129,23 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
} }
} }
func genEnvInts[T ~int | ~uint](values []T) string {
var buf bytes.Buffer
for _, v := range values {
if buf.Len() > 0 {
_, _ = buf.WriteRune(' ')
}
_, _ = buf.WriteString(fmt.Sprintf("%v", v))
}
return buf.String()
}
func genEnvStrings(values []string) string {
return strings.Join(values, " ")
}
func genEnvZoneNodes(z *Zone) string { func genEnvZoneNodes(z *Zone) string {
if n := z.Len(); n > 0 { if n := z.Len(); n > 0 {
s := make([]string, 0, n) s := make([]string, 0, n)
@@ -164,7 +155,7 @@ func genEnvZoneNodes(z *Zone) string {
return false return false
}) })
return strings.Join(s, " ") return genEnvStrings(s)
} }
return "" return ""
} }
+2 -2
View File
@@ -12,7 +12,7 @@ type Machine struct {
zone *Zone zone *Zone
logger `json:"-" yaml:"-"` logger `json:"-" yaml:"-"`
ID int ID NodeID
Name string `json:"-" yaml:"-"` Name string `json:"-" yaml:"-"`
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"` Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
@@ -74,7 +74,7 @@ func (m *Machine) SetGateway(enabled bool) error {
} }
// Zone indicates the [Zone] this machine belongs to // Zone indicates the [Zone] this machine belongs to
func (m *Machine) Zone() int { func (m *Machine) Zone() ZoneID {
return m.zone.ID return m.zone.ID
} }
+1 -1
View File
@@ -223,7 +223,7 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er
} }
} }
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error { func (m *Machine) applyZoneNodeID(zoneID ZoneID, nodeID NodeID) error {
switch { switch {
case zoneID == 0: case zoneID == 0:
return fmt.Errorf("invalid %s", "zoneID") return fmt.Errorf("invalid %s", "zoneID")
+1 -1
View File
@@ -65,7 +65,7 @@ func (m *Machine) setID() error {
return err return err
} }
m.ID = int(id) m.ID = NodeID(id)
return nil return nil
} }
+11 -11
View File
@@ -81,8 +81,8 @@ func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
type RingAddressEncoder struct { type RingAddressEncoder struct {
ID int ID int
Port uint16 Port uint16
Encode func(zoneID, nodeID int) (netip.Addr, bool) Encode func(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool)
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool) Decode func(addr netip.Addr) (zoneID ZoneID, nodeID NodeID, ok bool)
} }
var ( var (
@@ -110,7 +110,7 @@ var (
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number. // ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
// //
// 0 is reserved, and only allowed when composing CIDRs. // 0 is reserved, and only allowed when composing CIDRs.
func ValidZoneID(zoneID int) bool { func ValidZoneID(zoneID ZoneID) bool {
switch { switch {
case zoneID < 0 || zoneID > MaxZoneID: case zoneID < 0 || zoneID > MaxZoneID:
return false return false
@@ -122,7 +122,7 @@ func ValidZoneID(zoneID int) bool {
// ValidNodeID checks if the given nodeID is a valid 8 bit number. // ValidNodeID checks if the given nodeID is a valid 8 bit number.
// nodeID is unique within a Zone. // nodeID is unique within a Zone.
// 0 is reserved, and only allowed when composing CIDRs. // 0 is reserved, and only allowed when composing CIDRs.
func ValidNodeID(nodeID int) bool { func ValidNodeID(nodeID NodeID) bool {
switch { switch {
case nodeID < 0 || nodeID > MaxNodeID: case nodeID < 0 || nodeID > MaxNodeID:
return false return false
@@ -133,19 +133,19 @@ func ValidNodeID(nodeID int) bool {
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr] // ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}` // 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() { if addr.IsValid() {
a4 := addr.As4() a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 { 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 return 0, 0, false
} }
// RingZeroAddress returns a wg0 IP address // RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) { func RingZeroAddress(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool) {
switch { switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false 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] // ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}` // 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() { if addr.IsValid() {
a4 := addr.As4() a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 { if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4) zoneID = ZoneID(a4[1] >> 4)
nodeID = int(a4[3]) nodeID = NodeID(a4[3])
return zoneID, nodeID, true 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 // RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) { func RingOneAddress(zoneID ZoneID, nodeID NodeID) (netip.Addr, bool) {
switch { switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false return netip.Addr{}, false
+25
View File
@@ -0,0 +1,25 @@
package cluster
import "strconv"
type (
// ZoneID is the identifier of a [Zone]
ZoneID int
// NodeID is the identifier of a [Machine]
NodeID int
)
func (z ZoneID) String() string {
return idString(int(z))
}
func (n NodeID) String() string {
return idString(int(n))
}
func idString(v int) string {
if v > 0 {
return strconv.Itoa(v)
}
return ""
}
+4 -4
View File
@@ -19,7 +19,7 @@ type Zone struct {
zones *Cluster zones *Cluster
logger `json:"-" yaml:"-"` logger `json:"-" yaml:"-"`
ID int ID ZoneID
Name string Name string
Regions []string `json:",omitempty" yaml:",omitempty"` Regions []string `json:",omitempty" yaml:",omitempty"`
@@ -31,7 +31,7 @@ func (z *Zone) String() string {
} }
// SetGateway configures a machine to be the zone's ring0 gateway // SetGateway configures a machine to be the zone's ring0 gateway
func (z *Zone) SetGateway(gatewayID int, enabled bool) error { func (z *Zone) SetGateway(gatewayID NodeID, enabled bool) error {
var err error var err error
var found bool var found bool
@@ -56,8 +56,8 @@ func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
} }
// GatewayIDs returns the list of IDs of machines that act as ring0 gateways // GatewayIDs returns the list of IDs of machines that act as ring0 gateways
func (z *Zone) GatewayIDs() ([]int, int) { func (z *Zone) GatewayIDs() ([]NodeID, int) {
var out []int var out []NodeID
z.ForEachMachine(func(p *Machine) bool { z.ForEachMachine(func(p *Machine) bool {
if p.IsGateway() { if p.IsGateway() {
out = append(out, p.ID) out = append(out, p.ID)