From 7f849415ca52df0c7acfba652468869ac68c3573 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 2 Jun 2024 22:04:54 +0000 Subject: [PATCH] cluster: use rings.ZoneID and rings.NodeID types Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 2 +- cmd/jpictl/gateway.go | 3 +- pkg/cluster/cluster_import.go | 6 ++-- pkg/cluster/cluster_scan.go | 4 ++- pkg/cluster/env.go | 57 +++++++++++++++-------------------- pkg/cluster/machine.go | 6 ++-- pkg/cluster/machine_rings.go | 3 +- pkg/cluster/machine_scan.go | 3 +- pkg/cluster/rings.go | 43 ++++++++++---------------- pkg/cluster/zones.go | 10 +++--- 10 files changed, 64 insertions(+), 73 deletions(-) diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index ad13421..9b2fd32 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -52,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error { m.ForEachZone(func(z *cluster.Zone) 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 }) diff --git a/cmd/jpictl/gateway.go b/cmd/jpictl/gateway.go index a2489e3..d093972 100644 --- a/cmd/jpictl/gateway.go +++ b/cmd/jpictl/gateway.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "os" - "strconv" "strings" "github.com/spf13/cobra" @@ -128,7 +127,7 @@ func gatewayListAll(zi cluster.ZoneIterator) error { return false } for _, i := range ids { - sIDs = append(sIDs, strconv.Itoa(i)) + sIDs = append(sIDs, i.String()) } b.WriteString(strings.Join(sIDs, ", ")) b.WriteString("\n") diff --git a/pkg/cluster/cluster_import.go b/pkg/cluster/cluster_import.go index 0201324..bf04ba5 100644 --- a/pkg/cluster/cluster_import.go +++ b/pkg/cluster/cluster_import.go @@ -6,6 +6,8 @@ import ( "os" "gopkg.in/yaml.v3" + + "git.jpi.io/amery/jpictl/pkg/rings" ) func (m *Cluster) init(opts *ScanOptions) error { @@ -45,7 +47,7 @@ func (m *Cluster) initZones(opts *ScanOptions) error { func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error { var hasMissing bool - var lastMachineID int + var lastMachineID rings.NodeID z.zones = m z.logger = m @@ -58,7 +60,7 @@ func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error { case p.ID == 0: hasMissing = true case p.ID > lastMachineID: - lastMachineID = z.ID + lastMachineID = p.ID } return false diff --git a/pkg/cluster/cluster_scan.go b/pkg/cluster/cluster_scan.go index 79eda5d..cdc3107 100644 --- a/pkg/cluster/cluster_scan.go +++ b/pkg/cluster/cluster_scan.go @@ -7,6 +7,8 @@ import ( "strings" "darvaza.org/core" + + "git.jpi.io/amery/jpictl/pkg/rings" ) const ( @@ -114,7 +116,7 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error { func (m *Cluster) scanZoneIDs(_ *ScanOptions) error { var hasMissing bool - var lastZoneID int + var lastZoneID rings.ZoneID m.ForEachZone(func(z *Zone) bool { switch { diff --git a/pkg/cluster/env.go b/pkg/cluster/env.go index f3ee1c6..0600dab 100644 --- a/pkg/cluster/env.go +++ b/pkg/cluster/env.go @@ -6,6 +6,8 @@ import ( "io" "sort" "strings" + + "git.jpi.io/amery/jpictl/pkg/rings" ) // Env is a shell environment factory for this cluster @@ -35,8 +37,8 @@ func (m *Cluster) Env(export bool) (*Env, error) { } // Zones returns the list of Zone IDs -func (m *Env) Zones() []int { - var zones []int +func (m *Env) Zones() []rings.ZoneID { + var zones []rings.ZoneID m.ForEachZone(func(z *Zone) bool { zones = append(zones, z.ID) @@ -70,8 +72,8 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) { m.writeEnvVar(&buf, m.cephFSID, "FSID") } - m.writeEnvVarStrings(&buf, m.Regions(), "REGIONS") - m.writeEnvVarInts(&buf, m.Zones(), "ZONES") + m.writeEnvVar(&buf, genEnvStrings(m.Regions()), "REGIONS") + m.writeEnvVar(&buf, genEnvInts(m.Zones()), "ZONES") m.ForEachZone(func(z *Zone) bool { m.writeEnvZone(&buf, z) @@ -92,7 +94,7 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) { // ZONE{zoneID}_GW 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 m.writeEnvVar(w, genEnvZoneRegion(z), "ZONE%v_%s", zoneID, "REGION") @@ -107,32 +109,6 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) { 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) { var prefix string @@ -155,6 +131,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 { if n := z.Len(); n > 0 { s := make([]string, 0, n) @@ -164,7 +157,7 @@ func genEnvZoneNodes(z *Zone) string { return false }) - return strings.Join(s, " ") + return genEnvStrings(s) } return "" } diff --git a/pkg/cluster/machine.go b/pkg/cluster/machine.go index 07dccbd..0d7259a 100644 --- a/pkg/cluster/machine.go +++ b/pkg/cluster/machine.go @@ -3,6 +3,8 @@ package cluster import ( "net/netip" "strings" + + "git.jpi.io/amery/jpictl/pkg/rings" ) // revive:disable:line-length-limit @@ -12,7 +14,7 @@ type Machine struct { zone *Zone logger `json:"-" yaml:"-"` - ID int + ID rings.NodeID Name string `json:"-" yaml:"-"` Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"` @@ -74,7 +76,7 @@ func (m *Machine) SetGateway(enabled bool) error { } // Zone indicates the [Zone] this machine belongs to -func (m *Machine) Zone() int { +func (m *Machine) Zone() rings.ZoneID { return m.zone.ID } diff --git a/pkg/cluster/machine_rings.go b/pkg/cluster/machine_rings.go index cceae9b..1c0c2f0 100644 --- a/pkg/cluster/machine_rings.go +++ b/pkg/cluster/machine_rings.go @@ -8,6 +8,7 @@ import ( "darvaza.org/core" + "git.jpi.io/amery/jpictl/pkg/rings" "git.jpi.io/amery/jpictl/pkg/wireguard" ) @@ -223,7 +224,7 @@ func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) er } } -func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error { +func (m *Machine) applyZoneNodeID(zoneID rings.ZoneID, nodeID rings.NodeID) error { switch { case zoneID == 0: return fmt.Errorf("invalid %s", "zoneID") diff --git a/pkg/cluster/machine_scan.go b/pkg/cluster/machine_scan.go index 259d945..f47cabb 100644 --- a/pkg/cluster/machine_scan.go +++ b/pkg/cluster/machine_scan.go @@ -9,6 +9,7 @@ import ( "time" "darvaza.org/core" + "git.jpi.io/amery/jpictl/pkg/rings" ) // LookupNetIP uses the DNS Resolver to get the public addresses associated @@ -65,7 +66,7 @@ func (m *Machine) setID() error { return err } - m.ID = int(id) + m.ID = rings.NodeID(id) return nil } diff --git a/pkg/cluster/rings.go b/pkg/cluster/rings.go index 28301a9..37a123d 100644 --- a/pkg/cluster/rings.go +++ b/pkg/cluster/rings.go @@ -5,14 +5,11 @@ import ( "io/fs" "net/netip" + "git.jpi.io/amery/jpictl/pkg/rings" "git.jpi.io/amery/jpictl/pkg/wireguard" ) const ( - // MaxZoneID indicates the highest ID allowed for a Zone - MaxZoneID = 0xf - // MaxNodeID indicates the highest Machine ID allowed within a Zone - MaxNodeID = 0xff - 1 // RingsCount indicates how many wireguard rings we have RingsCount = 2 // RingZeroPort is the port wireguard uses for ring0 @@ -81,8 +78,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 rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) + Decode func(addr netip.Addr) (zoneID rings.ZoneID, nodeID rings.NodeID, ok bool) } var ( @@ -110,42 +107,34 @@ 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 { - switch { - case zoneID < 0 || zoneID > MaxZoneID: - return false - default: - return true - } +func ValidZoneID(zoneID rings.ZoneID) bool { + return zoneID == 0 || zoneID.Valid() } // 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 { - switch { - case nodeID < 0 || nodeID > MaxNodeID: - return false - default: - return true - } +func ValidNodeID(nodeID rings.NodeID) bool { + return nodeID == 0 || nodeID.Valid() } // 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 rings.ZoneID, nodeID rings.NodeID, ok bool) { if addr.IsValid() { a4 := addr.As4() if a4[0] == 10 && a4[1] == 0 { - return int(a4[2]), int(a4[3]), true + zoneID = rings.ZoneID(a4[2]) + nodeID = rings.NodeID(a4[3]) + return zoneID, nodeID, true } } return 0, 0, false } // RingZeroAddress returns a wg0 IP address -func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) { +func RingZeroAddress(zoneID rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) { switch { case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): return netip.Addr{}, false @@ -157,13 +146,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 rings.ZoneID, nodeID rings.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 = rings.ZoneID(a4[1] >> 4) + nodeID = rings.NodeID(a4[3]) return zoneID, nodeID, true } } @@ -171,7 +160,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 rings.ZoneID, nodeID rings.NodeID) (netip.Addr, bool) { switch { case !ValidZoneID(zoneID) || !ValidNodeID(nodeID): return netip.Addr{}, false diff --git a/pkg/cluster/zones.go b/pkg/cluster/zones.go index c6f1582..aec04fa 100644 --- a/pkg/cluster/zones.go +++ b/pkg/cluster/zones.go @@ -2,6 +2,8 @@ package cluster import ( "io/fs" + + "git.jpi.io/amery/jpictl/pkg/rings" ) var ( @@ -19,7 +21,7 @@ type Zone struct { zones *Cluster logger `json:"-" yaml:"-"` - ID int + ID rings.ZoneID Name string Regions []string `json:",omitempty" yaml:",omitempty"` @@ -31,7 +33,7 @@ func (z *Zone) String() string { } // 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 rings.NodeID, enabled bool) error { var err error var found bool @@ -56,8 +58,8 @@ func (z *Zone) SetGateway(gatewayID int, enabled bool) error { } // GatewayIDs returns the list of IDs of machines that act as ring0 gateways -func (z *Zone) GatewayIDs() ([]int, int) { - var out []int +func (z *Zone) GatewayIDs() ([]rings.NodeID, int) { + var out []rings.NodeID z.ForEachMachine(func(p *Machine) bool { if p.IsGateway() { out = append(out, p.ID)