From 2224e7063801055d5a8242c50dc6870afc13f326 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 27 Aug 2023 16:08:00 +0000 Subject: [PATCH 1/3] zones: introduce Machines type. iterable and sortable Signed-off-by: Alejandro Mery --- pkg/zones/scan.go | 7 +------ pkg/zones/zones.go | 52 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/pkg/zones/scan.go b/pkg/zones/scan.go index 07b8127..8faf8a8 100644 --- a/pkg/zones/scan.go +++ b/pkg/zones/scan.go @@ -93,12 +93,7 @@ func (m *Zones) scanSort() error { }) m.ForEachZone(func(z *Zone) bool { - sort.SliceStable(z.Machines, func(i, j int) bool { - id1 := z.Machines[i].ID - id2 := z.Machines[j].ID - return id1 < id2 - }) - + sort.Sort(z) return false }) diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index f6924c7..7601919 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -4,6 +4,7 @@ package zones import ( "io/fs" "path/filepath" + "sort" "github.com/hack-pad/hackpadfs/os" @@ -11,6 +12,9 @@ import ( ) var ( + _ MachineIterator = Machines(nil) + _ sort.Interface = Machines(nil) + _ MachineIterator = (*Zone)(nil) _ MachineIterator = (*Zones)(nil) _ ZoneIterator = (*Zones)(nil) @@ -26,6 +30,42 @@ type ZoneIterator interface { ForEachZone(func(*Zone) bool) } +// Machines is a list of Machine objects +type Machines []*Machine + +// ForEachMachine calls a function for each Machine in the list +// until instructed to terminate the loop +func (m Machines) ForEachMachine(fn func(*Machine) bool) { + for _, p := range m { + if fn(p) { + return + } + } +} + +// Len returns the number of machines in the list +func (m Machines) Len() int { + return len(m) +} + +// Less implements sort.Interface to sort the list +func (m Machines) Less(i, j int) bool { + a, b := m[i], m[j] + za, zb := a.Zone(), b.Zone() + + switch { + case za == zb: + return a.ID < b.ID + default: + return za < zb + } +} + +// Swap implements sort.Interface to sort the list +func (m Machines) Swap(i, j int) { + m[i], m[j] = m[j], m[i] +} + // Zone represents one zone in a cluster type Zone struct { zones *Zones @@ -33,23 +73,13 @@ type Zone struct { ID int `toml:"id"` Name string `toml:"name"` - Machines []*Machine `toml:"machines"` + Machines `toml:"machines"` } func (z *Zone) String() string { return z.Name } -// ForEachMachine calls a function for each Machine in the zone -// until instructed to terminate the loop -func (z *Zone) ForEachMachine(fn func(*Machine) bool) { - for _, p := range z.Machines { - if fn(p) { - return - } - } -} - // SetGateway configures a machine to be the zone's ring0 gateway func (z *Zone) SetGateway(gatewayID int, enabled bool) error { var err error -- 2.17.1 From 1885c76198b684b88cd2ccab8ef40fb28184253c Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 27 Aug 2023 16:25:11 +0000 Subject: [PATCH 2/3] zones: FilterMachines() creates a Machines subset Signed-off-by: Alejandro Mery --- pkg/zones/zones.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index 7601919..a8fb2ea 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -66,6 +66,26 @@ func (m Machines) Swap(i, j int) { m[i], m[j] = m[j], m[i] } +// FilterMachines produces a subset of the machines offered by the given +// iterator fulfilling a condition +func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) { + var out []*Machine + + if cond == nil { + // unconditional + cond = func(*Machine) bool { return true } + } + + m.ForEachMachine(func(p *Machine) bool { + if cond(p) { + out = append(out, p) + } + return false + }) + + return out, len(out) +} + // Zone represents one zone in a cluster type Zone struct { zones *Zones -- 2.17.1 From f63ce6c4e78254b3f9c4d12ed9e497c6cdcf9c02 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 27 Aug 2023 17:09:15 +0000 Subject: [PATCH 3/3] zones: introduce Zone.GatewayIDs() Signed-off-by: Alejandro Mery --- pkg/zones/zones.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index a8fb2ea..ae9eb33 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -125,6 +125,19 @@ 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 + z.ForEachMachine(func(p *Machine) bool { + if p.IsGateway() { + out = append(out, p.ID) + } + return false + }) + + return out, len(out) +} + // Zones represents all zones in a cluster type Zones struct { dir fs.FS -- 2.17.1