Browse Source

Merge pull request 'cluster: introduce Regions to group zones' (#22)

Reviewed-on: #22
pull/23/head v0.6.4
Alejandro Mery 1 year ago
parent
commit
f62a47003d
  1. 3
      pkg/cluster/cluster.go
  2. 1
      pkg/cluster/cluster_import.go
  3. 88
      pkg/cluster/regions.go
  4. 5
      pkg/cluster/zones.go

3
pkg/cluster/cluster.go

@ -27,7 +27,8 @@ type Cluster struct {
Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` Domain string `json:"domain,omitempty" yaml:"domain,omitempty"`
CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"` CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"`
Zones []*Zone `json:"zones,omitempty" yaml:"zones,omitempty"` Regions []Region `json:",omitempty" yaml:",omitempty"`
Zones []*Zone `json:",omitempty" yaml:",omitempty"`
} }
// revive:enable:line-length-limit // revive:enable:line-length-limit

1
pkg/cluster/cluster_import.go

@ -14,6 +14,7 @@ func (m *Cluster) init(opts *ScanOptions) error {
m.scanZoneIDs, m.scanZoneIDs,
m.scanSort, m.scanSort,
m.scanGateways, m.scanGateways,
m.initRegions,
} { } {
if err := fn(opts); err != nil { if err := fn(opts); err != nil {
return err return err

88
pkg/cluster/regions.go

@ -0,0 +1,88 @@
package cluster
// Region represents a group of zones geographically related
type Region struct {
m *Cluster
zones []*Zone
Name string
Regions []string `json:",omitempty" yaml:",omitempty"`
}
func (m *Cluster) initRegions(_ *ScanOptions) error {
regions := make(map[string][]*Zone)
// first regions defined by zones
m.ForEachZone(func(z *Zone) bool {
for _, region := range z.Regions {
regions[region] = append(regions[region], z)
}
return false
})
// bind first level regions and their zones
for name, zones := range regions {
m.syncRegions(name, zones...)
}
// and combine zones to produce larger regions
for i := range m.Regions {
r := &m.Regions[i]
m.finishRegion(r)
}
return nil
}
func (m *Cluster) syncRegions(name string, zones ...*Zone) {
for _, r := range m.Regions {
if r.Name == name {
// found
r.m = m
r.zones = zones
return
}
}
// new
m.Regions = append(m.Regions, Region{
m: m,
zones: zones,
Name: name,
})
}
func (m *Cluster) finishRegion(r *Region) {
if r.m != nil {
// ready
return
}
r.m = m
sub := []string{}
for _, name := range r.Regions {
r2, ok := m.getRegion(name)
if !ok {
m.warn(nil).WithField("region", name).Print("unknown region")
continue
}
sub = append(sub, r2.Name)
r.zones = append(r.zones, r2.zones...)
}
r.Regions = sub
}
func (m *Cluster) getRegion(name string) (*Region, bool) {
for i := range m.Regions {
r := &m.Regions[i]
if name == r.Name {
m.finishRegion(r)
return r, true
}
}
return nil, false
}

5
pkg/cluster/zones.go

@ -19,8 +19,9 @@ type Zone struct {
zones *Cluster zones *Cluster
logger `json:"-" yaml:"-"` logger `json:"-" yaml:"-"`
ID int ID int
Name string Name string
Regions []string `json:",omitempty" yaml:",omitempty"`
Machines Machines
} }

Loading…
Cancel
Save