Browse Source

Merge pull request 'cluster: introduce Region.Cluser and m/{region}/k8s_token to store a cluster's token' (#42) from pr-amery-k8s_token into main

Reviewed-on: #42
pull/43/head
Nagy Károly Gábriel 8 months ago
parent
commit
3a2a20a611
  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"
"path"
"sort"
"strings"
"darvaza.org/core"
)
@ -12,6 +13,10 @@ const (
// ZoneRegionsFileName indicates the file containing
// region names as references
ZoneRegionsFileName = "regions"
// RegionClusterTokenFileName contains the kubernetes
// token of the cluster this region represents
RegionClusterTokenFileName = "k8s_token"
)
func (m *Cluster) scan(opts *ScanOptions) error {
@ -186,6 +191,8 @@ func (z *Zone) scan() error {
switch {
case name == ZoneRegionsFileName:
err = z.loadRegions()
case name == RegionClusterTokenFileName:
err = z.loadClusterToken()
case e.IsDir():
err = z.scanSubdirectory(name)
default:
@ -218,6 +225,32 @@ func (z *Zone) loadRegions() error {
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 {
m := &Machine{
zone: z,

82
pkg/cluster/regions.go

@ -16,6 +16,7 @@ type Region struct {
zones []*Zone
Name string
Cluster *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) {
for i := range m.Regions {
r := &m.Regions[i]
@ -178,20 +199,32 @@ func (z *Zone) SyncRegions() error {
err := z.syncZoneRegions()
if err == nil {
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() {
err = p.RemoveFile("region")
} else {
err = p.WriteStringFile("none\n", "region")
}
return err != nil
})
if err == nil {
err = p.RemoveFile(RegionClusterTokenFileName)
}
return err
}
func (z *Zone) syncZoneRegions() error {
name := filepath.Join(z.Name, "regions")
name := filepath.Join(z.Name, ZoneRegionsFileName)
if len(z.Regions) > 0 {
var buf bytes.Buffer
@ -210,9 +243,25 @@ func (z *Zone) syncZoneRegions() error {
// SyncRegions writes to the file system the regions covered
// by this meta-region
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
for _, s := range r.Regions {
@ -220,12 +269,29 @@ func (r *Region) SyncRegions() error {
_, _ = buf.WriteRune('\n')
}
if err := r.m.MkdirAll(r.Name); err != nil {
err = r.m.WriteStringFile(buf.String(), name)
}
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