Compare commits
8 Commits
v0.6.3
...
e0d8592dc1
| Author | SHA1 | Date | |
|---|---|---|---|
| e0d8592dc1 | |||
| c397ca29ac | |||
| 066788b9be | |||
| 4402555f04 | |||
| 6e7f24f491 | |||
| 54b302c6d5 | |||
| f62a47003d | |||
| c702d649e0 |
Vendored
+6
@@ -1,9 +1,15 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"asciigoat",
|
||||||
"ceph",
|
"ceph",
|
||||||
|
"cyclomatic",
|
||||||
"darvaza",
|
"darvaza",
|
||||||
"gofrs",
|
"gofrs",
|
||||||
"jpictl",
|
"jpictl",
|
||||||
|
"libdns",
|
||||||
|
"Lookuper",
|
||||||
|
"publicsuffix",
|
||||||
|
"Wrapf",
|
||||||
"zerolog"
|
"zerolog"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ require (
|
|||||||
darvaza.org/slog/handlers/discard v0.4.5
|
darvaza.org/slog/handlers/discard v0.4.5
|
||||||
github.com/gofrs/uuid/v5 v5.0.0
|
github.com/gofrs/uuid/v5 v5.0.0
|
||||||
github.com/hack-pad/hackpadfs v0.2.1
|
github.com/hack-pad/hackpadfs v0.2.1
|
||||||
|
github.com/libdns/libdns v0.2.1
|
||||||
github.com/mgechev/revive v1.3.3
|
github.com/mgechev/revive v1.3.3
|
||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMI
|
|||||||
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
github.com/hack-pad/hackpadfs v0.2.1/go.mod h1:khQBuCEwGXWakkmq8ZiFUvUZz84ZkJ2KNwKvChs4OrU=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
|
||||||
|
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
|
||||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
|
|||||||
return todo
|
return todo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Cluster) scanCephMonitors(_ *ScanOptions) error {
|
func (m *Cluster) scanCephMonitors(opts *ScanOptions) error {
|
||||||
cfg, err := m.GetCephConfig()
|
cfg, err := m.GetCephConfig()
|
||||||
switch {
|
switch {
|
||||||
case os.IsNotExist(err):
|
case os.IsNotExist(err):
|
||||||
@@ -94,6 +94,10 @@ func (m *Cluster) scanCephMonitors(_ *ScanOptions) error {
|
|||||||
todo.LogMissing(m.log)
|
todo.LogMissing(m.log)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return m.initCephMonitors(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Cluster) initCephMonitors(_ *ScanOptions) error {
|
||||||
// make sure every zone has one
|
// make sure every zone has one
|
||||||
m.ForEachZone(func(z *Zone) bool {
|
m.ForEachZone(func(z *Zone) bool {
|
||||||
_ = z.GetCephMonitors()
|
_ = z.GetCephMonitors()
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ func (m *Cluster) init(opts *ScanOptions) error {
|
|||||||
m.scanZoneIDs,
|
m.scanZoneIDs,
|
||||||
m.scanSort,
|
m.scanSort,
|
||||||
m.scanGateways,
|
m.scanGateways,
|
||||||
|
m.initCephMonitors,
|
||||||
|
m.initRegions,
|
||||||
} {
|
} {
|
||||||
if err := fn(opts); err != nil {
|
if err := fn(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
package cluster
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ MachineIterator = (*Region)(nil)
|
||||||
|
_ ZoneIterator = (*Region)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Region represents a group of zones geographically related
|
||||||
|
type Region struct {
|
||||||
|
m *Cluster
|
||||||
|
zones []*Zone
|
||||||
|
|
||||||
|
Name string
|
||||||
|
Regions []string `json:",omitempty" yaml:",omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachRegion calls a function for each Region of the cluster
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (m *Cluster) ForEachRegion(fn func(r *Region) bool) {
|
||||||
|
for i := range m.Regions {
|
||||||
|
r := &m.Regions[i]
|
||||||
|
if fn(r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachMachine calls a function for each Machine in the region
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (r *Region) ForEachMachine(fn func(*Machine) bool) {
|
||||||
|
r.ForEachZone(func(z *Zone) bool {
|
||||||
|
var term bool
|
||||||
|
|
||||||
|
z.ForEachMachine(func(p *Machine) bool {
|
||||||
|
term = fn(p)
|
||||||
|
return term
|
||||||
|
})
|
||||||
|
|
||||||
|
return term
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachZone calls a function for each Zone in the region
|
||||||
|
// until instructed to terminate the loop
|
||||||
|
func (r *Region) ForEachZone(fn func(*Zone) bool) {
|
||||||
|
for _, p := range r.zones {
|
||||||
|
if fn(p) {
|
||||||
|
// terminate
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
// Package dns manages DNS entries for the cluster
|
||||||
|
package dns
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/netip"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"darvaza.org/core"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SortAddrSlice sorts a slice of [netip.Addr]
|
||||||
|
func SortAddrSlice(s []netip.Addr) []netip.Addr {
|
||||||
|
sort.Slice(s, func(i, j int) bool {
|
||||||
|
return s[i].Less(s[j])
|
||||||
|
})
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// SortAddrRecords sorts a slice of [AddrRecord]
|
||||||
|
// by Name and Address
|
||||||
|
func SortAddrRecords(s []AddrRecord) []AddrRecord {
|
||||||
|
sort.Slice(s, func(i, j int) bool {
|
||||||
|
return s[i].Name < s[j].Name
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, p := range s {
|
||||||
|
SortAddrSlice(p.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddrRecord represents an A or AAAA record
|
||||||
|
type AddrRecord struct {
|
||||||
|
Name string
|
||||||
|
Addr []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort sorts the addresses of the record
|
||||||
|
func (rr *AddrRecord) Sort() {
|
||||||
|
SortAddrSlice(rr.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export converts the record into libdns.Record
|
||||||
|
func (rr *AddrRecord) Export() []libdns.Record {
|
||||||
|
out := make([]libdns.Record, len(rr.Addr))
|
||||||
|
for i, addr := range rr.Addr {
|
||||||
|
out[i] = libdns.Record{
|
||||||
|
Name: rr.Name,
|
||||||
|
TTL: time.Second * 1,
|
||||||
|
Type: core.IIf(addr.Is6(), "AAAA", "A"),
|
||||||
|
Value: addr.String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteTo writes the record in BIND notation
|
||||||
|
func (rr *AddrRecord) WriteTo(w io.Writer) (int64, error) {
|
||||||
|
var total int
|
||||||
|
for _, addr := range rr.Addr {
|
||||||
|
n, err := fmt.Fprint(w,
|
||||||
|
rr.Name, "\t",
|
||||||
|
1, "\t",
|
||||||
|
core.IIf(addr.Is6(), "AAAA", "A"), "\t",
|
||||||
|
addr.String(), "\n")
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return 0, err
|
||||||
|
case n > 0:
|
||||||
|
total += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return int64(total), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String converts the record into BIND entries
|
||||||
|
func (rr *AddrRecord) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
_, _ = rr.WriteTo(&buf)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user