Browse Source

cluster: load regions when scanning a directory

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/39/head
Alejandro Mery 11 months ago
parent
commit
00cf3959a2
  1. 85
      pkg/cluster/cluster_scan.go
  2. 32
      pkg/cluster/regions.go

85
pkg/cluster/cluster_scan.go

@ -2,17 +2,25 @@ package cluster
import ( import (
"io/fs" "io/fs"
"path"
"sort" "sort"
"darvaza.org/core" "darvaza.org/core"
) )
const (
// ZoneRegionsFileName indicates the file containing
// region names as references
ZoneRegionsFileName = "regions"
)
func (m *Cluster) scan(opts *ScanOptions) error { 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.scanZoneIDs, m.scanZoneIDs,
m.scanSort, m.scanSort,
m.initRegions,
m.scanGateways, m.scanGateways,
m.scanCephMonitors, m.scanCephMonitors,
} { } {
@ -24,7 +32,7 @@ func (m *Cluster) scan(opts *ScanOptions) error {
return nil return nil
} }
func (m *Cluster) scanDirectory(_ *ScanOptions) error { func (m *Cluster) scanDirectory(opts *ScanOptions) error {
// each directory is a zone // each directory is a zone
entries, err := fs.ReadDir(m.dir, ".") entries, err := fs.ReadDir(m.dir, ".")
if err != nil { if err != nil {
@ -33,16 +41,14 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
for _, e := range entries { for _, e := range entries {
if e.IsDir() { if e.IsDir() {
z, err := m.newZone(e.Name()) ok, err := m.scanSubdirectory(opts, e.Name())
switch { switch {
case err != nil: case err != nil:
return core.Wrap(err, e.Name()) return core.Wrap(err, e.Name())
case z.Machines.Len() == 0: case !ok:
z.warn(nil). m.warn(nil).
WithField("zone", z.Name). WithField("zone", e.Name()).
Print("empty") Print("empty")
default:
m.Zones = append(m.Zones, z)
} }
} }
} }
@ -50,6 +56,27 @@ func (m *Cluster) scanDirectory(_ *ScanOptions) error {
return nil return nil
} }
func (m *Cluster) scanSubdirectory(_ *ScanOptions, name string) (bool, error) {
z, err := m.newZone(name)
switch {
case err != nil:
// somewhere went wrong scanning the subdirectory
return false, err
case z.Machines.Len() > 0:
// zones have machines and the regions they belong
m.Zones = append(m.Zones, z)
return true, nil
case len(z.Regions) > 0:
// regions have no machines but can include
// other regions
m.appendRegionRegions(name, z.Regions...)
return true, nil
default:
// empty
return false, nil
}
}
func (m *Cluster) newZone(name string) (*Zone, error) { func (m *Cluster) newZone(name string) (*Zone, error) {
z := &Zone{ z := &Zone{
zones: m, zones: m,
@ -154,11 +181,48 @@ func (z *Zone) scan() error {
} }
for _, e := range entries { for _, e := range entries {
if e.IsDir() { name := e.Name()
switch {
case name == ZoneRegionsFileName:
err = z.loadRegions()
case e.IsDir():
err = z.scanSubdirectory(name)
default:
z.warn(nil).
WithField("zone", z.Name).
WithField("filename", name).
Print("unknown")
}
if err != nil {
return err
}
}
return nil
}
func (z *Zone) loadRegions() error {
filename := path.Join(z.Name, ZoneRegionsFileName)
regions, err := z.zones.ReadLines(filename)
if err == nil {
// parsed
err = z.appendRegions(regions...)
if err != nil {
err = core.Wrap(err, filename)
}
}
return err
}
func (z *Zone) scanSubdirectory(name string) error {
m := &Machine{ m := &Machine{
zone: z, zone: z,
logger: z, logger: z,
Name: e.Name(), Name: name,
} }
m.debug(). m.debug().
@ -176,9 +240,6 @@ func (z *Zone) scan() error {
} }
z.Machines = append(z.Machines, m) z.Machines = append(z.Machines, m)
}
}
return nil return nil
} }

32
pkg/cluster/regions.go

@ -106,6 +106,38 @@ func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
}) })
} }
func (m *Cluster) appendRegionRegions(name string, subs ...string) {
for i := range m.Regions {
r := &m.Regions[i]
if name == r.Name {
// found
r.Regions = append(r.Regions, subs...)
return
}
}
// new
m.Regions = append(m.Regions, Region{
Name: name,
Regions: subs,
})
}
func (z *Zone) appendRegions(regions ...string) error {
for _, s := range regions {
// TODO: validate
z.debug().
WithField("zone", z.Name).
WithField("region", s).
Print("attached")
z.Regions = append(z.Regions, s)
}
return nil
}
func (m *Cluster) finishRegion(r *Region) { func (m *Cluster) finishRegion(r *Region) {
if r.m != nil { if r.m != nil {
// ready // ready

Loading…
Cancel
Save