cluster: introduce Region.Cluser and m/{region}/k8s_token to store a cluster's token #42

Merged
karasz merged 3 commits from pr-amery-k8s_token into main 8 months ago
  1. 33
      pkg/cluster/cluster_scan.go
  2. 82
      pkg/cluster/regions.go

33
pkg/cluster/cluster_scan.go

@ -4,6 +4,7 @@ import (
"io/fs" "io/fs"
"path" "path"
"sort" "sort"
"strings"
"darvaza.org/core" "darvaza.org/core"
) )
@ -12,6 +13,10 @@ const (
// ZoneRegionsFileName indicates the file containing // ZoneRegionsFileName indicates the file containing
// region names as references // region names as references
ZoneRegionsFileName = "regions" ZoneRegionsFileName = "regions"
// RegionClusterTokenFileName contains the kubernetes
// token of the cluster this region represents
RegionClusterTokenFileName = "k8s_token"
) )
func (m *Cluster) scan(opts *ScanOptions) error { func (m *Cluster) scan(opts *ScanOptions) error {
@ -186,6 +191,8 @@ func (z *Zone) scan() error {
switch { switch {
case name == ZoneRegionsFileName: case name == ZoneRegionsFileName:
err = z.loadRegions() err = z.loadRegions()
case name == RegionClusterTokenFileName:
err = z.loadClusterToken()
case e.IsDir(): case e.IsDir():
err = z.scanSubdirectory(name) err = z.scanSubdirectory(name)
default: default:
@ -218,6 +225,32 @@ func (z *Zone) loadRegions() error {
return err return err
} }
func (z *Zone) loadClusterToken() error {
var token string
filename := path.Join(z.Name, RegionClusterTokenFileName)
lines, err := z.zones.ReadLines(filename)
if err != nil {
return err
}
// first non-empty line
for _, s := range lines {
s = strings.TrimSpace(s)
if s != "" {
token = s
break
}
}
err = z.zones.setRegionClusterToken(z.Name, token)
if err != nil {
err = core.Wrap(err, filename)
}
return err
}
func (z *Zone) scanSubdirectory(name string) error { func (z *Zone) scanSubdirectory(name string) error {
m := &Machine{ m := &Machine{
zone: z, zone: z,

82
pkg/cluster/regions.go

@ -16,6 +16,7 @@ type Region struct {
zones []*Zone zones []*Zone
Name string Name string
Cluster *string `json:",omitempty" yaml:",omitempty"`
Regions []string `json:",omitempty" yaml:",omitempty"` Regions []string `json:",omitempty" yaml:",omitempty"`
} }
@ -106,6 +107,26 @@ func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
}) })
} }
func (m *Cluster) setRegionClusterToken(name string, token string) error {
for i := range m.Regions {
r := &m.Regions[i]
if r.Name == name {
// found
r.Cluster = &token
return nil
}
}
// new
m.Regions = append(m.Regions, Region{
m: m,
Name: name,
Cluster: &token,
})
return nil
}
func (m *Cluster) appendRegionRegions(name string, subs ...string) { func (m *Cluster) appendRegionRegions(name string, subs ...string) {
for i := range m.Regions { for i := range m.Regions {
r := &m.Regions[i] r := &m.Regions[i]
@ -178,20 +199,32 @@ func (z *Zone) SyncRegions() error {
err := z.syncZoneRegions() err := z.syncZoneRegions()
if err == nil { if err == nil {
z.ForEachMachine(func(p *Machine) bool { z.ForEachMachine(func(p *Machine) bool {
err = z.syncMachineRegions(p)
return err != nil
})
}
return err
}
func (*Zone) syncMachineRegions(p *Machine) error {
var err error
if p.IsActive() { if p.IsActive() {
err = p.RemoveFile("region") err = p.RemoveFile("region")
} else { } else {
err = p.WriteStringFile("none\n", "region") err = p.WriteStringFile("none\n", "region")
} }
return err != nil
}) if err == nil {
err = p.RemoveFile(RegionClusterTokenFileName)
} }
return err return err
} }
func (z *Zone) syncZoneRegions() error { func (z *Zone) syncZoneRegions() error {
name := filepath.Join(z.Name, "regions") name := filepath.Join(z.Name, ZoneRegionsFileName)
if len(z.Regions) > 0 { if len(z.Regions) > 0 {
var buf bytes.Buffer var buf bytes.Buffer
@ -210,9 +243,25 @@ func (z *Zone) syncZoneRegions() error {
// SyncRegions writes to the file system the regions covered // SyncRegions writes to the file system the regions covered
// by this meta-region // by this meta-region
func (r *Region) SyncRegions() error { func (r *Region) SyncRegions() error {
name := filepath.Join(r.Name, "regions") if err := r.syncRegionsFile(); err != nil {
return err
}
return r.syncClusterFile()
}
func (r *Region) mkdir() error {
return r.m.MkdirAll(r.Name)
}
func (r *Region) syncRegionsFile() error {
var err error
if len(r.Regions) > 0 { name := filepath.Join(r.Name, ZoneRegionsFileName)
if len(r.Regions) == 0 {
err = r.m.RemoveFile(name)
} else if err = r.mkdir(); err == nil {
var buf bytes.Buffer var buf bytes.Buffer
for _, s := range r.Regions { for _, s := range r.Regions {
@ -220,12 +269,29 @@ func (r *Region) SyncRegions() error {
_, _ = buf.WriteRune('\n') _, _ = buf.WriteRune('\n')
} }
if err := r.m.MkdirAll(r.Name); err != nil { err = r.m.WriteStringFile(buf.String(), name)
}
return err return err
}
func (r *Region) syncClusterFile() error {
var err error
name := filepath.Join(r.Name, RegionClusterTokenFileName)
if r.Cluster == nil {
err = r.m.RemoveFile(name)
} else if err = r.mkdir(); err == nil {
var buf bytes.Buffer
_, _ = buf.WriteString(*r.Cluster)
if buf.Len() > 0 {
_, _ = buf.WriteRune('\n')
} }
return r.m.WriteStringFile(buf.String(), name) err = r.m.WriteStringFile(buf.String(), name)
} }
return r.m.RemoveFile(name) return err
} }

Loading…
Cancel
Save