You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
2.4 KiB
133 lines
2.4 KiB
12 months ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
|
"io/fs"
|
||
1 year ago
|
|
||
1 year ago
|
"darvaza.org/resolver"
|
||
12 months ago
|
"darvaza.org/slog"
|
||
12 months ago
|
"github.com/gofrs/uuid/v5"
|
||
1 year ago
|
)
|
||
|
|
||
1 year ago
|
var (
|
||
|
_ MachineIterator = (*Zone)(nil)
|
||
|
_ MachineIterator = (*Zones)(nil)
|
||
1 year ago
|
_ ZoneIterator = (*Zones)(nil)
|
||
1 year ago
|
)
|
||
|
|
||
1 year ago
|
// A ZoneIterator is a set of Zones we can iterate on
|
||
|
type ZoneIterator interface {
|
||
|
ForEachZone(func(*Zone) bool)
|
||
|
}
|
||
|
|
||
1 year ago
|
// Zone represents one zone in a cluster
|
||
|
type Zone struct {
|
||
12 months ago
|
zones *Zones
|
||
12 months ago
|
logger `json:"-" yaml:"-"`
|
||
1 year ago
|
|
||
12 months ago
|
ID int
|
||
|
Name string
|
||
1 year ago
|
|
||
12 months ago
|
Machines
|
||
1 year ago
|
}
|
||
|
|
||
|
func (z *Zone) String() string {
|
||
|
return z.Name
|
||
|
}
|
||
|
|
||
1 year ago
|
// SetGateway configures a machine to be the zone's ring0 gateway
|
||
|
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
|
||
|
var err error
|
||
|
var found bool
|
||
|
|
||
|
z.ForEachMachine(func(p *Machine) bool {
|
||
|
if p.ID == gatewayID {
|
||
|
found = true
|
||
|
err = p.SetGateway(enabled)
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
})
|
||
|
|
||
|
switch {
|
||
|
case err != nil:
|
||
|
return err
|
||
|
case !found:
|
||
|
return fs.ErrNotExist
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// 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)
|
||
|
}
|
||
|
|
||
12 months ago
|
// revive:disable:line-length-limit
|
||
|
|
||
1 year ago
|
// Zones represents all zones in a cluster
|
||
|
type Zones struct {
|
||
1 year ago
|
dir fs.FS
|
||
12 months ago
|
log slog.Logger
|
||
1 year ago
|
resolver resolver.Resolver
|
||
|
domain string
|
||
1 year ago
|
|
||
12 months ago
|
CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
|
||
|
Zones []*Zone
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// revive:enable:line-length-limit
|
||
|
|
||
1 year ago
|
// ForEachMachine calls a function for each Machine in the cluster
|
||
1 year ago
|
// until instructed to terminate the loop
|
||
1 year ago
|
func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
|
||
1 year ago
|
m.ForEachZone(func(z *Zone) bool {
|
||
|
var term bool
|
||
|
|
||
|
z.ForEachMachine(func(p *Machine) bool {
|
||
|
term = fn(p)
|
||
|
return term
|
||
|
})
|
||
|
|
||
|
return term
|
||
|
})
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// ForEachZone calls a function for each Zone in the cluster
|
||
1 year ago
|
// until instructed to terminate the loop
|
||
1 year ago
|
func (m *Zones) ForEachZone(fn func(*Zone) bool) {
|
||
1 year ago
|
for _, p := range m.Zones {
|
||
1 year ago
|
if fn(p) {
|
||
|
// terminate
|
||
|
return
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// GetMachineByName looks for a machine with the specified
|
||
|
// name on any zone
|
||
|
func (m *Zones) GetMachineByName(name string) (*Machine, bool) {
|
||
|
var out *Machine
|
||
|
|
||
|
if name != "" {
|
||
|
m.ForEachMachine(func(p *Machine) bool {
|
||
|
if p.Name == name {
|
||
|
out = p
|
||
|
}
|
||
|
|
||
|
return out != nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return out, out != nil
|
||
|
}
|