Compare commits

..

2 Commits

Author SHA1 Message Date
amery 4a378c2334 cluster: assign valid rings.RegionID to each primary region
Signed-off-by: Alejandro Mery <amery@jpi.io>
2024-06-02 23:23:31 +00:00
amery 7b44f40416 cluster: use rings.ZoneID and rings.NodeID types
Signed-off-by: Alejandro Mery <amery@jpi.io>
2024-06-02 23:22:08 +00:00
4 changed files with 74 additions and 7 deletions
+2 -1
View File
@@ -5,8 +5,9 @@ import (
"fmt"
"os"
"git.jpi.io/amery/jpictl/pkg/rings"
"gopkg.in/yaml.v3"
"git.jpi.io/amery/jpictl/pkg/rings"
)
func (m *Cluster) init(opts *ScanOptions) error {
+1
View File
@@ -7,6 +7,7 @@ import (
"strings"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
)
+2 -2
View File
@@ -53,7 +53,7 @@ func (m *Env) Regions() []string {
var regions []string
m.ForEachRegion(func(r *Region) bool {
if r.Cluster != nil {
if r.IsPrimary() {
regions = append(regions, r.Name)
}
@@ -166,7 +166,7 @@ func genEnvZoneRegion(z *Zone) string {
var region string
z.ForEachRegion(func(r *Region) bool {
if r.Cluster != nil {
if r.IsPrimary() {
region = r.Name
return true
}
+69 -4
View File
@@ -3,6 +3,8 @@ package cluster
import (
"bytes"
"path/filepath"
"git.jpi.io/amery/jpictl/pkg/rings"
)
var (
@@ -24,8 +26,15 @@ type Region struct {
zones []*Zone
Name string
Cluster *string `json:",omitempty" yaml:",omitempty"`
Regions []string `json:",omitempty" yaml:",omitempty"`
ID rings.RegionID `json:",omitempty" yaml:",omitempty"`
Cluster *string `json:",omitempty" yaml:",omitempty"`
Regions []string `json:",omitempty" yaml:",omitempty"`
}
// IsPrimary() indicates the region is primary and corresponds
// to a kubernetes cluster.
func (r *Region) IsPrimary() bool {
return r != nil && r.Cluster != nil
}
// ForEachRegion calls a function for each Region of the cluster
@@ -91,8 +100,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
m.finishRegion(r)
}
m.sortRegions()
return nil
return m.finishRegions()
}
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
@@ -210,6 +218,63 @@ func (m *Cluster) finishRegion(r *Region) {
r.Regions = sub
}
func (m *Cluster) finishRegions() error {
var max rings.RegionID
var missing bool
m.sortRegions()
// check IDs
ids := make(map[rings.RegionID]bool)
fn := func(r *Region) bool {
var term bool
switch {
case !r.IsPrimary():
// ignore secondary
case !r.ID.Valid():
// primary without ID
missing = true
case ids[r.ID]:
// duplicate
m.error(nil).WithField("region", r.Name).Print("duplicate ID")
missing = true
r.ID = 0
default:
ids[r.ID] = true
if r.ID > max {
max = r.ID
}
}
return term
}
m.ForEachRegion(fn)
if missing {
// assign missing IDs
fn := func(r *Region) bool {
var term bool
switch {
case !r.IsPrimary():
// ignore secondary
case r.ID.Valid():
// already has an ID
default:
r.ID = max + 1
max = r.ID
}
return term
}
m.ForEachRegion(fn)
}
return nil
}
func (m *Cluster) getRegion(name string) (*Region, bool) {
for i := range m.Regions {
r := &m.Regions[i]