Compare commits
12 Commits
4a378c2334
..
v0.8.3
| Author | SHA1 | Date | |
|---|---|---|---|
| 879ee69f07 | |||
| 932a41a3ac | |||
| bcb20ab1e6 | |||
| 96c59dfe8a | |||
| 169a1e9602 | |||
| 982e16581c | |||
| cede95e119 | |||
| 14e1c447c9 | |||
| 99dece1e43 | |||
| 6d89e0ea3c | |||
| ff8f2c6ea1 | |||
| 83921c1e13 |
@@ -15,7 +15,8 @@ TMPDIR ?= .tmp
|
|||||||
REVIVE ?= $(GOBIN)/revive
|
REVIVE ?= $(GOBIN)/revive
|
||||||
REVIVE_CONF ?= $(TOOLSDIR)/revive.toml
|
REVIVE_CONF ?= $(TOOLSDIR)/revive.toml
|
||||||
REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly
|
REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly
|
||||||
REVIVE_INSTALL_URL ?= github.com/mgechev/revive@master
|
REVIVE_VERSION ?= v1.3.7
|
||||||
|
REVIVE_INSTALL_URL ?= github.com/mgechev/revive@$(REVIVE_VERSION)
|
||||||
|
|
||||||
GO_INSTALL_URLS = \
|
GO_INSTALL_URLS = \
|
||||||
$(REVIVE_INSTALL_URL) \
|
$(REVIVE_INSTALL_URL) \
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ import (
|
|||||||
func (m *Cluster) init(opts *ScanOptions) error {
|
func (m *Cluster) init(opts *ScanOptions) error {
|
||||||
for _, fn := range []func(*ScanOptions) error{
|
for _, fn := range []func(*ScanOptions) error{
|
||||||
m.initZones,
|
m.initZones,
|
||||||
|
m.initRegions,
|
||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
m.scanGateways,
|
m.scanGateways,
|
||||||
m.initCephMonitors,
|
m.initCephMonitors,
|
||||||
m.initRegions,
|
|
||||||
} {
|
} {
|
||||||
if err := fn(opts); err != nil {
|
if err := fn(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ func (m *Cluster) scan(opts *ScanOptions) error {
|
|||||||
for _, fn := range []func(*ScanOptions) error{
|
for _, fn := range []func(*ScanOptions) error{
|
||||||
m.scanDirectory,
|
m.scanDirectory,
|
||||||
m.scanMachines,
|
m.scanMachines,
|
||||||
|
m.initRegions,
|
||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
m.initRegions,
|
|
||||||
m.scanGateways,
|
m.scanGateways,
|
||||||
m.scanCephMonitors,
|
m.scanCephMonitors,
|
||||||
} {
|
} {
|
||||||
|
|||||||
+4
-12
@@ -163,18 +163,10 @@ func genEnvZoneNodes(z *Zone) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func genEnvZoneRegion(z *Zone) string {
|
func genEnvZoneRegion(z *Zone) string {
|
||||||
var region string
|
if z != nil && z.region != nil {
|
||||||
|
return z.region.Name
|
||||||
z.ForEachRegion(func(r *Region) bool {
|
}
|
||||||
if r.IsPrimary() {
|
return ""
|
||||||
region = r.Name
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
return region
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genEnvZoneCephMonNames(m Machines) string {
|
func genEnvZoneCephMonNames(m Machines) string {
|
||||||
|
|||||||
+39
-7
@@ -31,7 +31,7 @@ type Region struct {
|
|||||||
Regions []string `json:",omitempty" yaml:",omitempty"`
|
Regions []string `json:",omitempty" yaml:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPrimary() indicates the region is primary and corresponds
|
// IsPrimary indicates the region is primary and corresponds
|
||||||
// to a kubernetes cluster.
|
// to a kubernetes cluster.
|
||||||
func (r *Region) IsPrimary() bool {
|
func (r *Region) IsPrimary() bool {
|
||||||
return r != nil && r.Cluster != nil
|
return r != nil && r.Cluster != nil
|
||||||
@@ -100,7 +100,10 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
|
|||||||
m.finishRegion(r)
|
m.finishRegion(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.finishRegions()
|
m.sortRegions()
|
||||||
|
m.scanRegionID()
|
||||||
|
m.computeZonesRegion()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
|
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
|
||||||
@@ -218,12 +221,12 @@ func (m *Cluster) finishRegion(r *Region) {
|
|||||||
r.Regions = sub
|
r.Regions = sub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) finishRegions() error {
|
// revive:disable:cognitive-complexity
|
||||||
|
func (m *Cluster) scanRegionID() {
|
||||||
|
// revive:enable:cognitive-complexity
|
||||||
var max rings.RegionID
|
var max rings.RegionID
|
||||||
var missing bool
|
var missing bool
|
||||||
|
|
||||||
m.sortRegions()
|
|
||||||
|
|
||||||
// check IDs
|
// check IDs
|
||||||
ids := make(map[rings.RegionID]bool)
|
ids := make(map[rings.RegionID]bool)
|
||||||
fn := func(r *Region) bool {
|
fn := func(r *Region) bool {
|
||||||
@@ -231,7 +234,8 @@ func (m *Cluster) finishRegions() error {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case !r.IsPrimary():
|
case !r.IsPrimary():
|
||||||
// ignore secondary
|
// secondary, no ID.
|
||||||
|
r.ID = 0
|
||||||
case !r.ID.Valid():
|
case !r.ID.Valid():
|
||||||
// primary without ID
|
// primary without ID
|
||||||
missing = true
|
missing = true
|
||||||
@@ -272,7 +276,35 @@ func (m *Cluster) finishRegions() error {
|
|||||||
|
|
||||||
m.ForEachRegion(fn)
|
m.ForEachRegion(fn)
|
||||||
}
|
}
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) computeZonesRegion() {
|
||||||
|
fn := func(r *Region, z *Zone) {
|
||||||
|
if z.region != nil {
|
||||||
|
m.error(nil).
|
||||||
|
WithField("zone", z.Name).
|
||||||
|
WithField("region", []string{
|
||||||
|
z.region.Name,
|
||||||
|
r.Name,
|
||||||
|
}).
|
||||||
|
Print("zone in two regions")
|
||||||
|
} else {
|
||||||
|
z.region = r
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.ForEachRegion(func(r *Region) bool {
|
||||||
|
var term bool
|
||||||
|
|
||||||
|
if r.IsPrimary() {
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
fn(r, z)
|
||||||
|
return term
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return term
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) getRegion(name string) (*Region, bool) {
|
func (m *Cluster) getRegion(name string) (*Region, bool) {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type ZoneIterator interface {
|
|||||||
// affinity.
|
// affinity.
|
||||||
type Zone struct {
|
type Zone struct {
|
||||||
zones *Cluster
|
zones *Cluster
|
||||||
|
region *Region
|
||||||
logger `json:"-" yaml:"-"`
|
logger `json:"-" yaml:"-"`
|
||||||
|
|
||||||
ID rings.ZoneID
|
ID rings.ZoneID
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package rings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DecodeAddress extracts ring address fields from a given 10.0.0.0/8
|
||||||
|
// address.
|
||||||
|
//
|
||||||
|
// revive:disable:function-result-limit
|
||||||
|
func DecodeAddress[T ~uint | NodeID](addr netip.Addr) (RingID, RegionID, ZoneID, T) {
|
||||||
|
// revive:enable:function-result-limit
|
||||||
|
if addr.IsValid() {
|
||||||
|
if addr.Is4In6() {
|
||||||
|
addr = addr.Unmap()
|
||||||
|
}
|
||||||
|
|
||||||
|
if addr.Is4() {
|
||||||
|
a4 := addr.As4()
|
||||||
|
return unsafeDecodeAddress[T](a4[0], a4[1], a4[2], a4[3])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return UnspecifiedRingID, 0, 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// revive:disable:function-result-limit
|
||||||
|
func unsafeDecodeAddress[T ~uint | NodeID](a, b, c, d byte) (RingID, RegionID, ZoneID, T) {
|
||||||
|
// revive:enable:function-result-limit
|
||||||
|
switch {
|
||||||
|
case a != 10:
|
||||||
|
return UnspecifiedRingID, 0, 0, 0
|
||||||
|
case b == 0x00:
|
||||||
|
// 10.00.RZ.dd
|
||||||
|
k := RingZeroID
|
||||||
|
r := RegionID(c >> 4)
|
||||||
|
z := ZoneID(c & 0xf)
|
||||||
|
n := T(d)
|
||||||
|
|
||||||
|
return k, r, z, n
|
||||||
|
case b&0xf0 != 0:
|
||||||
|
// 10.Rb.cc.dd
|
||||||
|
k := RingThreeID
|
||||||
|
r := RegionID(b >> 4)
|
||||||
|
|
||||||
|
n2 := T(b & 0x0f)
|
||||||
|
n1 := T(c)
|
||||||
|
n0 := T(d)
|
||||||
|
n := n0 + n1<<8 + n2<<16
|
||||||
|
|
||||||
|
return k, r, 0, n
|
||||||
|
case c&0xf0 != 0:
|
||||||
|
// 10.0R.Zc.dd
|
||||||
|
k := RingOneID
|
||||||
|
r := RegionID(b)
|
||||||
|
z := ZoneID(c >> 4)
|
||||||
|
|
||||||
|
n1 := T(c & 0x0f)
|
||||||
|
n0 := T(d)
|
||||||
|
n := n0 + n1<<8
|
||||||
|
|
||||||
|
return k, r, z, n
|
||||||
|
default:
|
||||||
|
// 10.0R.0c.dd
|
||||||
|
k := RingTwoID
|
||||||
|
r := RegionID(b)
|
||||||
|
|
||||||
|
n1 := T(c & 0x0f)
|
||||||
|
n0 := T(d)
|
||||||
|
n := n0 + n1<<8
|
||||||
|
|
||||||
|
return k, r, 0, n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRingZeroAddress attempts to extract region, zone and node identifiers
|
||||||
|
// from a given ring 0 address.
|
||||||
|
//
|
||||||
|
// revive:disable:function-result-limit
|
||||||
|
func DecodeRingZeroAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool) {
|
||||||
|
// revive:enable:function-result-limit
|
||||||
|
k, r, z, n := DecodeAddress[NodeID](addr)
|
||||||
|
if k == RingZeroID {
|
||||||
|
return r, z, n, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRingOneAddress attempts to extract region, zone and node identifiers
|
||||||
|
// from a given ring 1 address.
|
||||||
|
//
|
||||||
|
// revive:disable:function-result-limit
|
||||||
|
func DecodeRingOneAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool) {
|
||||||
|
// revive:enable:function-result-limit
|
||||||
|
k, r, z, n := DecodeAddress[NodeID](addr)
|
||||||
|
if k == RingOneID {
|
||||||
|
return r, z, n, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRingTwoAddress attempts to extract region and unique identifier for
|
||||||
|
// a kubernetes service from a given ring 2 address.
|
||||||
|
func DecodeRingTwoAddress(addr netip.Addr) (RegionID, uint, bool) {
|
||||||
|
k, r, _, n := DecodeAddress[uint](addr)
|
||||||
|
if k == RingTwoID {
|
||||||
|
return r, n, true
|
||||||
|
}
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRingThreeAddress attempts to extract region and unique identifier for
|
||||||
|
// a kubernetes pod from a given ring 3 address.
|
||||||
|
func DecodeRingThreeAddress(addr netip.Addr) (RegionID, uint, bool) {
|
||||||
|
k, r, _, n := DecodeAddress[uint](addr)
|
||||||
|
if k == RingThreeID {
|
||||||
|
return r, n, true
|
||||||
|
}
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package rings
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDecodeRingZeroAddress(t *testing.T) {
|
||||||
|
RZNDecodeTest(t, "DecodeRingZeroAddress", DecodeRingZeroAddress, []RZNDecodeTestCase{
|
||||||
|
{1, 1, 50, MustParseAddr("10.0.17.50"), true},
|
||||||
|
{1, 2, 50, MustParseAddr("10.0.18.50"), true},
|
||||||
|
{2, 3, 1, MustParseAddr("10.0.35.1"), true},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TesDecodetRingOneAddress(t *testing.T) {
|
||||||
|
RZNDecodeTest(t, "DecodeRingOneAddress", DecodeRingOneAddress, []RZNDecodeTestCase{
|
||||||
|
{1, 1, 50, MustParseAddr("10.1.16.50"), true},
|
||||||
|
{1, 2, 50, MustParseAddr("10.1.32.50"), true},
|
||||||
|
{2, 3, 300, MustParseAddr("10.2.49.44"), true},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type RZNDecodeTestCase struct {
|
||||||
|
region RegionID
|
||||||
|
zone ZoneID
|
||||||
|
node NodeID
|
||||||
|
addr netip.Addr
|
||||||
|
ok bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func RZNDecodeTest(t *testing.T,
|
||||||
|
fnName string, fn func(netip.Addr) (RegionID, ZoneID, NodeID, bool),
|
||||||
|
cases []RZNDecodeTestCase) {
|
||||||
|
//
|
||||||
|
for i, tc := range cases {
|
||||||
|
s := fmt.Sprintf("%s(%q)", fnName, tc.addr)
|
||||||
|
|
||||||
|
r, z, n, ok := fn(tc.addr)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case ok != tc.ok, r != tc.region, z != tc.zone, n != tc.node:
|
||||||
|
t.Errorf("ERROR: [%v/%v]: %s → %v %v %v %v (expected %v %v %v %v)",
|
||||||
|
i, len(cases), s,
|
||||||
|
r, z, n, ok,
|
||||||
|
tc.region, tc.zone, tc.node, tc.ok)
|
||||||
|
default:
|
||||||
|
t.Logf("[%v/%v]: %s → %v %v %v %v", i, len(cases), s,
|
||||||
|
r, z, n, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
// UnspecifiedRingID is the zero value of RingID and not considered
|
||||||
|
// valid.
|
||||||
|
UnspecifiedRingID RingID = iota
|
||||||
|
RingZeroID // RingZeroID is the RingID for RingZero (backbone)
|
||||||
|
RingOneID // RingOneID is the RingID for RingOne (local zone)
|
||||||
|
RingTwoID // RingTwoID is the RingID for RingTwo (region services)
|
||||||
|
RingThreeID // RingThreeID is the RingID for RingThree (region cluster pods)
|
||||||
|
|
||||||
|
// RingMax indicates the highest [Ring] identifier
|
||||||
|
RingMax = RingThreeID
|
||||||
|
|
||||||
// RegionMax indicates the highest number that can be used for a [RegionID].
|
// RegionMax indicates the highest number that can be used for a [RegionID].
|
||||||
RegionMax = (1 << 4) - 1
|
RegionMax = (1 << 4) - 1
|
||||||
// ZoneMax indicates the highest number that can be used for a [ZoneID].
|
// ZoneMax indicates the highest number that can be used for a [ZoneID].
|
||||||
@@ -33,6 +44,21 @@ const (
|
|||||||
RingThreeBits = 12
|
RingThreeBits = 12
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RingID identifies a Ring
|
||||||
|
type RingID int
|
||||||
|
|
||||||
|
// Valid tells a [RingID] is within the valid range.
|
||||||
|
func (n RingID) Valid() bool { return n > 0 && n <= RingMax }
|
||||||
|
|
||||||
|
func (n RingID) String() string {
|
||||||
|
return idString(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A Ring identifies what ring an address belongs to
|
||||||
|
type Ring interface {
|
||||||
|
ID() RingID
|
||||||
|
}
|
||||||
|
|
||||||
// RegionID is the identifier of a region, valid between 1 and [RegionMax].
|
// RegionID is the identifier of a region, valid between 1 and [RegionMax].
|
||||||
type RegionID int
|
type RegionID int
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user