From 223edf846b26249bc0697188066530a8d69d8046 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Tue, 22 Aug 2023 22:01:40 +0000 Subject: [PATCH] zones: introduce Zone.ForEachMachine() and refactor Zones.ForEachMachine() using it Signed-off-by: Alejandro Mery --- pkg/zones/zones.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/pkg/zones/zones.go b/pkg/zones/zones.go index 3f4a559..30bee91 100644 --- a/pkg/zones/zones.go +++ b/pkg/zones/zones.go @@ -22,6 +22,16 @@ 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 + } + } +} + // Zones represents all zones in a cluster type Zones struct { dir fs.FS @@ -32,18 +42,22 @@ type Zones struct { } // ForEachMachine calls a function for each Machine in the cluster +// until instructed to terminate the loop func (m *Zones) ForEachMachine(fn func(*Machine) bool) { - for _, z := range m.Zones { - for _, p := range z.Machines { - if fn(p) { - // terminate - return - } - } - } + m.ForEachZone(func(z *Zone) bool { + var term bool + + z.ForEachMachine(func(p *Machine) bool { + term = fn(p) + return term + }) + + return term + }) } // ForEachZone calls a function for each Zone in the cluster +// until instructed to terminate the loop func (m *Zones) ForEachZone(fn func(*Zone) bool) { for _, p := range m.Zones { if fn(p) {