Browse Source

cluster: move Machines to a dedicated file

Signed-off-by: Alejandro Mery <amery@jpi.io>
Alejandro Mery 1 year ago
parent
commit
f1a3606016
  1. 69
      pkg/cluster/machines.go
  2. 65
      pkg/cluster/zones.go

69
pkg/cluster/machines.go

@ -0,0 +1,69 @@
package cluster
import "sort"
var (
_ MachineIterator = Machines(nil)
_ sort.Interface = Machines(nil)
)
// A MachineIterator is a set of Machines we can iterate on
type MachineIterator interface {
ForEachMachine(func(*Machine) 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]
}
// 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)
}

65
pkg/cluster/zones.go

@ -2,7 +2,6 @@ package cluster
import (
"io/fs"
"sort"
"darvaza.org/resolver"
"darvaza.org/slog"
@ -10,80 +9,16 @@ import (
)
var (
_ MachineIterator = Machines(nil)
_ sort.Interface = Machines(nil)
_ MachineIterator = (*Zone)(nil)
_ MachineIterator = (*Zones)(nil)
_ ZoneIterator = (*Zones)(nil)
)
// A MachineIterator is a set of Machines we can iterate on
type MachineIterator interface {
ForEachMachine(func(*Machine) bool)
}
// A ZoneIterator is a set of Zones we can iterate on
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]
}
// 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

Loading…
Cancel
Save