Compare commits

..

9 Commits

Author SHA1 Message Date
amery 8d1b9c4f04 WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 12:49:14 +00:00
amery 60d3a5f650 dns: introduce AddrRecord{} to abstract A/AAAA entries
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 12:49:14 +00:00
amery 67e27b7f01 vscode: add Lookuper, publicsuffix and libdns to the dictionary
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 12:49:14 +00:00
amery 03dfc73f63 build-sys: use local asciigoat.org/ini
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-12 12:49:02 +00:00
amery 41e2b75964 Merge branch 'pr-amery-cluster' into next-amery 2023-09-12 12:48:55 +00:00
amery fb331b6586 Merge branch 'pr-amery-wireguard-ini' into next-amery 2023-09-12 12:46:17 +00:00
amery db62adfb9c wireguard: switch from gcfg to asciigoat.org/ini/basic
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-05 19:39:09 +00:00
amery 4599eca7d9 wireguard: implement EndpointAddress.UnmarshalText
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-05 19:38:03 +00:00
amery 312dbe2269 wireguard: implement UnmarshalText for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-05 19:38:03 +00:00
114 changed files with 722 additions and 4187 deletions
-51
View File
@@ -1,51 +0,0 @@
export CLOUDFLARE_DNS_API_TOKEN=wFpklBgp0Z1A4yDs5zNhlTKne3W2Si8GLwkl10Oz
if [ -n "$JPICTL" ]; then
:
elif JPICTL=$(which jpictl); then
echo 2 >&2
elif [ -d ./cmd/jpictl ]; then
JPICTL="go run ./cmd/jpictl/"
if [ -d "$(go env GOBIN)" ]; then
echo 3a >&2
elif [ -d "$(go env GOPATH)" ]; then
export GOBIN="$(go env GOPATH)/bin"
echo 3b >&2
elif WS=$(x --root); then
export GOBIN="$WS/bin"
unset -v WS
echo 3c >&2
else
echo 3d >&2
fi
elif [ -d "${WS:-}" -a -x "${WS:+$WS/bin/jpictl}" ]; then
JPICTL="$WS/bin/jpictl"
export PATH="$WS/bin:$PATH"
echo "4a" >&2
else
for WS in \
"$(x --root)" \
"$HOME/projects/apptly" \
; do
if [ -x "$WS/bin/jpictl" ]; then
JPICTL="$WS/bin/jpictl"
export PATH="$WS/bin:$PATH"
echo "4b: '$WS'" >&2
break
fi
done
unset -v WS
[ -n "$JPICTL" ] || echo "4c" >&2
fi
if [ -n "$JPICTL" ]; then
echo "JPICTL='$JPICTL'" >&2
export JPICTL
jpictl() {
$JPICTL "$@"
}
else
echo "JPICTL="
fi
-1
View File
@@ -1,2 +1 @@
.tmp
.version
-38
View File
@@ -1,38 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "write",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/jpictl",
"args": [ "write" ],
"cwd": "${workspaceFolder}"
},
{
"name": "dump",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/jpictl",
"args": [ "dump", "-vvvv" ],
"cwd": "${workspaceFolder}"
},
{
"name": "env",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/jpictl",
"args": [ "env", "-vvvv" ],
"cwd": "${workspaceFolder}"
}
]
}
]
}
+15 -14
View File
@@ -6,20 +6,19 @@ GOFMT ?= gofmt
GOFMT_FLAGS = -w -l -s
GOGENERATE_FLAGS = -v
GOPATH ?= $(shell $(GO) env GOPATH)
GOBIN ?= $(GOPATH)/bin
TOOLSDIR := $(CURDIR)/pkg/tools
TMPDIR ?= $(CURDIR)/.tmp
OUTDIR ?= $(TMPDIR)
GOLANGCI_LINT_VERSION ?= v1.59.1
REVIVE_VERSION ?= v1.3.7
GOLANGCI_LINT_URL ?= github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
GOLANGCI_LINT ?= $(GO) run $(GOLANGCI_LINT_URL)
TMPDIR ?= .tmp
REVIVE ?= $(GOBIN)/revive
REVIVE_CONF ?= $(TOOLSDIR)/revive.toml
REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly
REVIVE_URL ?= github.com/mgechev/revive@$(REVIVE_VERSION)
REVIVE ?= $(GO) run $(REVIVE_URL)
REVIVE_INSTALL_URL ?= github.com/mgechev/revive
GO_INSTALL_URLS = \
$(REVIVE_INSTALL_URL) \
V = 0
Q = $(if $(filter 1,$V),,@)
@@ -30,12 +29,11 @@ GO_BUILD_CMD = $(GO_BUILD) -o "$(OUTDIR)"
all: get generate tidy build
clean: ; $(info $(M) cleaning)
rm -rf $(TMPDIR)
install: ; $(info $(M) cleaning)
install:
$Q $(GO) install -v ./cmd/...
clean: ; $(info $(M) cleaning)
rm -rf $(TMPDIR)
$(TMPDIR)/index: $(TOOLSDIR)/gen_index.sh Makefile FORCE ; $(info $(M) generating index)
$Q mkdir -p $(@D)
@@ -56,3 +54,6 @@ tidy: fmt
generate: ; $(info $(M) running go:generate)
$Q git grep -l '^//go:generate' | sort -uV | xargs -r -n1 $(GO) generate $(GOGENERATE_FLAGS)
$(REVIVE):
$Q $(GO) install -v $(REVIVE_INSTALL_URL)
-144
View File
@@ -1,144 +0,0 @@
dir: m
name: m
domain: jpi.cloud
ceph_fsid: 28180b9b-6d5d-4be0-bb5b-e4b04bf03804
regions:
- name: de
- name: eu
id: 1
cluster: ""
regions:
- de
- nl
- name: nl
- name: uk
id: 2
cluster: ""
- name: us
id: 3
cluster: ""
- name: europe
regions:
- eu
- uk
- name: global
regions:
- europe
- us
zones:
- id: 1
name: ssd-lon
regions:
- uk
machines:
- id: 3
public:
- 63.250.53.5
- 2602:ff16:7:0:1:66:0:1
rings:
- ring: 0
enabled: true
keys:
privatekey: WJY1yrd14c0gY4qqq1O8BPck46TVYebXrT+OoMbEqG8=
publickey: aDXNwapVjufcuQKfBN+waJObe1TaeeaSNl1suA0u+00=
- ring: 1
enabled: true
keys:
privatekey: 2CDO+x8HVKfw1/iqC2WkwIEQyWZsC9XPW1otqxarR0o=
publickey: untmMfW2W4OiMgG/6nAysoivXtDh7cZ9/rxhgJ4OlC4=
- id: 7
ceph_monitor: true
public:
- 107.155.122.3
- 2602:ff16:7:0:1:6d:0:1
rings:
- ring: 1
enabled: true
keys:
privatekey: iN4Rc3J36APlkIP/ksSpHIwAuxC/ehYLwGQ5va1AhG8=
publickey: XGVXDmxQajHpsgpzgrp+r/ZWxZUaodTpvGBJuLlZ0n4=
- id: 2
name: ssd-ams
regions:
- nl
machines:
- id: 3
public:
- 89.233.107.128
- 2602:ff16:9:0:1:1a7:0:1
rings:
- ring: 0
enabled: true
keys:
privatekey: oHVQY976pFH1lAhCrz9c40vR/e5PIXw7ZXv6pU6A5Ho=
publickey: y/Gkid2Mjxo1WO9Zi7mh+DETNYKT/AAc3K3bvr9xp3c=
- ring: 1
enabled: true
keys:
privatekey: 6I/ZMxaWUTbNc+SIGkNKxgovONqv6EAvg7NeeTRrR1c=
publickey: 9i0XI358OOYYoxoxV5lX8siqE8uiEwCsuWLo6LoFu0Y=
- id: 4
ceph_monitor: true
public:
- 89.233.107.251
- 2602:ff16:9:0:1:130:0:1
rings:
- ring: 1
enabled: true
keys:
privatekey: SPBd5Ka29ZecKYy7BW85rW7FO9dwKwoUHlSibybPG1s=
publickey: QpJupYlnJc7wc3+gAlEmbFMonsqpVnYRy7f/gnPqqBw=
- id: 5
inactive: true
public:
- 89.233.107.107
- 2602:ff16:9:0:1:74:0:1
rings:
- ring: 1
enabled: true
keys:
privatekey: mJaFvu9TBwE2i0rjhfhvpB3xZmp++3BVwa7QAvWXImY=
publickey: HDLVEwLv0uugyZ9C1C9ZWuiaWQ1qIqqe/DAUxYI0whM=
- id: 3
name: htz-fsn
regions:
- de
machines:
- id: 1
ceph_monitor: true
public:
- 157.90.209.125
- 2a01:4f8:252:168f::2
rings:
- ring: 0
enabled: true
keys:
privatekey: AMz/pCgOMVh1/2rT9uCFMox+LQgH5FrE4xUeqSs0x0M=
publickey: K9LYog8vtRFN4nW6dJWMQ9trmRrYuLpbu/Ze5kIiBGw=
- ring: 1
enabled: true
keys:
privatekey: 2JpA1G0Dp+sEuDhKnzToz0h76o2iLP+S7Qb9itlgXVA=
publickey: qWmYfgeRAQ563gWLdeYs65XjGJTSE+W8WTbpLZ0e1CQ=
- id: 4
name: ssd-nyc
regions:
- us
machines:
- id: 3
inactive: true
ceph_monitor: true
public:
- 208.87.134.41
- 2602:ff16:3:0:1:4fa:0:1
rings:
- ring: 0
enabled: true
keys:
privatekey: YNDxj+QSO3p5pXw9h7lBx0cLM0kJDv3v7BYLfc8TR3I=
publickey: 1UFgleUufTszPM1voIcwVhd0DpYZ1HwV05U6p3IS2DE=
- ring: 1
enabled: true
keys:
privatekey: APXGKqOxx1gOr587FSN3O3gRGpkGgSjt6zGlTu49UlU=
publickey: av8ni/9ZUyozabzqTjIy1sOnSJuQ9p63Tu9ECVmHB2I=
+17 -40
View File
@@ -12,14 +12,6 @@ const (
// DefaultConfigFile is read if -f/--config-file isn't specified.
// If it doesn't exist, m/ will be scanned
DefaultConfigFile = "cloud.yaml"
// DefaultClusterDir is the directory we will scan and write
// unless something else is indicated
DefaultClusterDir = "m"
// DefaultDomain indicates the domain to use unless
// something else is specified
DefaultDomain = "jpi.cloud"
)
// Config describes the repository
@@ -30,34 +22,27 @@ type Config struct {
ConfigFile string
}
var forceScan bool
var cfg = &Config{
Base: DefaultClusterDir,
Domain: DefaultDomain,
Base: "m",
Domain: "jpi.cloud",
}
// LoadZones loads all zones and machines in the config directory
// or file
func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
var zones *cluster.Cluster
var err error
// try config file first
zones, err := cluster.NewFromConfig(cfg.ConfigFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
if !forceScan {
// try config file first
zones, err = cluster.NewFromConfig(cfg.ConfigFile,
cluster.ResolvePublicAddresses(resolve),
cluster.WithLogger(log),
)
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
// file was bad
return nil, core.Wrap(err, "NewFromConfig(%q)", cfg.ConfigFile)
}
switch {
case err == nil:
// file was good
return zones, nil
case !os.IsNotExist(err) || cfg.ConfigFile != DefaultConfigFile:
// file was bad
return nil, core.Wrapf(err, "NewFromConfig(%q)", cfg.ConfigFile)
}
// default file doesn't exist. scan instead.
@@ -68,15 +53,7 @@ func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) {
}
func init() {
flags := rootCmd.PersistentFlags()
flags.StringVarP(&cfg.Base, "scan-dir", "d",
DefaultClusterDir, "directory to scan for cluster data")
flags.StringVarP(&cfg.Domain, "domain", "D",
DefaultDomain, "domain to use for scanned data")
flags.StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)")
flags.BoolVarP(&forceScan, "force-scan", "S",
false, "ignore config file and scan the directory instead")
rootCmd.PersistentFlags().
StringVarP(&cfg.ConfigFile, "config-file", "f",
DefaultConfigFile, "config file (JSON or YAML)")
}
+23 -141
View File
@@ -2,24 +2,15 @@ package main
import (
"context"
"net/netip"
"os"
"time"
"darvaza.org/core"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/cluster"
"git.jpi.io/amery/jpictl/pkg/dns"
)
const (
// DNSSyncTimeout specifies how long are we willing to wait for a DNS
// synchronization
DNSSyncTimeout = 10 * time.Second
)
func newDNSManager(m *cluster.Cluster, provider dns.Provider) (*dns.Manager, error) {
func newDNSManager(m *cluster.Cluster) (*dns.Manager, error) {
domain := m.Domain
if m.Name != "" {
domain = m.Name + "." + domain
@@ -30,73 +21,29 @@ func newDNSManager(m *cluster.Cluster, provider dns.Provider) (*dns.Manager, err
return nil, err
}
if provider != nil {
// set provider only if specified
err = dns.WithProvider(provider)(mgr)
m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(context.TODO(), z.Name, p.ID, true, p.PublicAddresses...)
return err != nil
})
return err != nil
})
if err != nil {
return nil, err
}
for _, r := range m.Regions {
err := mgr.AddRegion(context.TODO(), r.Name, r.Zones()...)
if err != nil {
return nil, err
}
}
if err := populateDNSManager(mgr, m); err != nil {
return nil, err
}
return mgr, nil
}
func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error {
var err error
ctx := context.TODO()
m.ForEachZone(func(z *cluster.Zone) bool {
z.ForEachMachine(func(p *cluster.Machine) bool {
err = mgr.AddHost(ctx, z.Name, int(p.ID), p.IsActive(), p.PublicAddresses...)
return err != nil
})
return err != nil
})
if err != nil {
return err
}
m.ForEachRegion(func(r *cluster.Region) bool {
r.ForEachZone(func(z *cluster.Zone) bool {
err = mgr.AddRegion(ctx, r.Name, z.Name)
return err != nil
})
return err != nil
})
return err
}
// revive:disable:flag-parameter
func newDNSManagerCommand(_ *cobra.Command,
resolve bool, withCredentials bool) (*dns.Manager, error) {
// revive:enable:flag-parameter
var cred dns.Provider
if withCredentials {
var err error
cred, err = dns.DefaultDNSProvider()
if err != nil {
return nil, err
}
}
m, err := cfg.LoadZones(resolve)
if err != nil {
return nil, err
}
return newDNSManager(m, cred)
}
// Command
var dnsCmd = &cobra.Command{
Use: "dns",
@@ -104,10 +51,14 @@ var dnsCmd = &cobra.Command{
var dnsWriteCmd = &cobra.Command{
Use: "write",
Short: "dns write generates public DNS records",
PreRun: setVerbosity,
RunE: func(cmd *cobra.Command, _ []string) error {
mgr, err := newDNSManagerCommand(cmd, true, false)
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(true)
if err != nil {
return err
}
mgr, err := newDNSManager(m)
if err != nil {
return err
}
@@ -117,77 +68,8 @@ var dnsWriteCmd = &cobra.Command{
},
}
var dnsSyncCmd = &cobra.Command{
Use: "sync",
Short: "dns sync updates public DNS records",
PreRun: setVerbosity,
RunE: func(cmd *cobra.Command, _ []string) error {
mgr, err := newDNSManagerCommand(cmd, true, true)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
defer cancel()
return mgr.Sync(ctx)
},
}
var dnsShowCmd = &cobra.Command{
Use: "show [<name>...]",
Short: "dns show lists entries on DNS for our domain",
PreRun: setVerbosity,
RunE: func(cmd *cobra.Command, args []string) error {
mgr, err := newDNSManagerCommand(cmd, true, true)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
defer cancel()
return mgr.Show(ctx, args...)
},
}
var dnsAddCmd = &cobra.Command{
Use: "add <name> <address..>",
Short: "dns add registers a new machine on the public DNS",
Args: cobra.MinimumNArgs(2),
PreRun: setVerbosity,
RunE: func(cmd *cobra.Command, args []string) error {
var addrs []netip.Addr
for _, s := range args[1:] {
addr, err := core.ParseAddr(s)
switch {
case err != nil:
return core.Wrap(err, s)
case !addr.IsValid(), addr.IsUnspecified(), addr.IsPrivate(), addr.IsMulticast():
return core.Wrap(core.ErrInvalid, s)
default:
addrs = append(addrs, addr)
}
}
mgr, err := newDNSManagerCommand(cmd, true, true)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), DNSSyncTimeout)
defer cancel()
return mgr.Add(ctx, args[0], addrs...)
},
}
func init() {
rootCmd.AddCommand(dnsCmd)
dnsCmd.AddCommand(dnsWriteCmd)
dnsCmd.AddCommand(dnsSyncCmd)
dnsCmd.AddCommand(dnsShowCmd)
dnsCmd.AddCommand(dnsAddCmd)
}
+5 -4
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
@@ -43,7 +44,7 @@ func gatewaySet(zi cluster.ZoneIterator, gw string) error {
zi.ForEachZone(func(z *cluster.Zone) bool {
for _, m := range z.Machines {
if m.Name == gw {
_ = z.SetGateway(m.ID, true)
z.SetGateway(m.ID, true)
return true
}
}
@@ -79,8 +80,8 @@ func gatewayUnset(zi cluster.ZoneIterator, ngw string) error {
zi.ForEachZone(func(z *cluster.Zone) bool {
for _, m := range z.Machines {
if m.Name == ngw && m.IsGateway() {
_ = z.SetGateway(m.ID, false)
_ = m.RemoveWireguardConfig(0)
z.SetGateway(m.ID, false)
m.RemoveWireguardConfig(0)
return true
}
}
@@ -127,7 +128,7 @@ func gatewayListAll(zi cluster.ZoneIterator) error {
return false
}
for _, i := range ids {
sIDs = append(sIDs, i.String())
sIDs = append(sIDs, strconv.Itoa(i))
}
b.WriteString(strings.Join(sIDs, ", "))
b.WriteString("\n")
-200
View File
@@ -1,200 +0,0 @@
package main
import (
"bytes"
"io"
"net/netip"
"os"
"darvaza.org/core"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/cluster"
"git.jpi.io/amery/jpictl/pkg/rings"
"git.jpi.io/amery/jpictl/pkg/tools"
)
type inventory struct {
r []*cluster.Region
z [][]*cluster.Zone
}
func (g *inventory) renderRingZero(out *tools.LazyBuffer) error {
ring0 := netip.PrefixFrom(rings.UnsafeRingZeroAddress(0, 0, 0), rings.RingZeroBits)
from, to, _ := rings.PrefixToRange(ring0)
_ = out.Printf("; wg%v\n", 0)
_ = out.Printf("%s\t%s-%s\n", ring0, from, to)
if err := g.renderRingZeroRegions(out); err != nil {
return err
}
return g.renderRingZeroZones(out)
}
func (g *inventory) renderRingZeroRegions(out *tools.LazyBuffer) error {
for _, r := range g.r {
if err := g.renderRingZeroRegion(out, r); err != nil {
return err
}
}
return nil
}
func (*inventory) renderRingZeroRegion(out *tools.LazyBuffer, r *cluster.Region) error {
addr := rings.UnsafeRingZeroAddress(r.ID, 0, 0)
ring0r := netip.PrefixFrom(addr, rings.RingZeroBits+4)
from, to, _ := rings.PrefixToRange(ring0r)
_ = out.Printf("%s\t%s-%s\t# %s\n", ring0r, from, to, r.Name)
return nil
}
func (g *inventory) renderRingZeroZones(out *tools.LazyBuffer) error {
for i, r := range g.r {
for _, z := range g.z[i] {
if err := g.renderRingZeroZone(out, r, z); err != nil {
return err
}
}
}
return nil
}
func (*inventory) renderRingZeroZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error {
addr := rings.UnsafeRingZeroAddress(r.ID, z.ID, 0)
ring0rz := netip.PrefixFrom(addr, rings.RingZeroBits+4+4)
from, to, _ := rings.PrefixToRange(ring0rz)
_ = out.Printf("; wg%v: %s (%s)\n", 0, z.Name, r.Name)
_ = out.Printf("%s\t%s-%s\t%s\n", ring0rz, from, to, z.Name)
z.ForEachMachine(func(m *cluster.Machine) bool {
if m.IsGateway() {
addr, _ := m.RingZeroAddress()
cidr := netip.PrefixFrom(addr, 32)
_ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 0)
}
return false
})
return nil
}
func (g *inventory) renderRingOne(out *tools.LazyBuffer) error {
for i, r := range g.r {
for _, z := range g.z[i] {
if err := g.renderRingOneZone(out, r, z); err != nil {
return err
}
}
}
return nil
}
func (*inventory) renderRingOneZone(out *tools.LazyBuffer, r *cluster.Region, z *cluster.Zone) error {
ring1, err := rings.RingOnePrefix(r.ID, z.ID)
if err != nil {
return err
}
from, to, _ := rings.PrefixToRange(ring1)
_ = out.Printf("; wg%v: %s (%s)\n", 1, z.Name, r.Name)
_ = out.Printf("%s\t%s-%s\t%s\n", ring1, from, to, z.Name)
z.ForEachMachine(func(m *cluster.Machine) bool {
addr := m.RingOneAddress()
cidr := netip.PrefixFrom(addr, 32)
_ = out.Printf("%s\t\t%s-%v\n", cidr, m.Name, 1)
return false
})
return nil
}
func (g *inventory) Marshal() ([]byte, error) {
var buf tools.LazyBuffer
if err := g.renderRingZero(&buf); err != nil {
return nil, err
}
if err := g.renderRingOne(&buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (g *inventory) WriteTo(out io.Writer) (int64, error) {
b, err := g.Marshal()
if err != nil {
return 0, err
}
buf := bytes.NewBuffer(b)
return buf.WriteTo(out)
}
func genInventory(m *cluster.Cluster) (*inventory, error) {
g := new(inventory)
g.populateRegions(m)
g.populateZones()
return g, nil
}
func (g *inventory) populateRegions(m *cluster.Cluster) {
m.ForEachRegion(func(r *cluster.Region) bool {
if r.IsPrimary() {
g.r = append(g.r, r)
}
return false
})
core.SliceSortFn(g.r, func(a, b *cluster.Region) bool {
return a.ID < b.ID
})
}
func (g *inventory) populateZones() {
g.z = make([][]*cluster.Zone, len(g.r))
for i, r := range g.r {
r.ForEachZone(func(z *cluster.Zone) bool {
g.z[i] = append(g.z[i], z)
return false
})
core.SliceSortFn(g.z[i], func(a, b *cluster.Zone) bool {
return a.ID < b.ID
})
}
}
// Command
var listCmd = &cobra.Command{
Use: "list",
Short: "list shows the IP/CIDR inventory",
PreRun: setVerbosity,
RunE: func(_ *cobra.Command, _ []string) error {
m, err := cfg.LoadZones(false)
if err != nil {
return err
}
out, err := genInventory(m)
if err != nil {
return err
}
_, err = out.WriteTo(os.Stdout)
return err
},
}
func init() {
rootCmd.AddCommand(listCmd)
}
-21
View File
@@ -3,13 +3,9 @@ package main
import (
"fmt"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
var log = zerolog.New(nil, slog.Error)
// fatal is a convenience wrapper for slog.Logger.Fatal().Print()
func fatal(err error, msg string, args ...any) {
l := log.Fatal()
@@ -23,20 +19,3 @@ func fatal(err error, msg string, args ...any) {
panic("unreachable")
}
var verbosity int
// setVerbosity replaces the global logger using the
// verbosity level specified via -v flags
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
+20 -6
View File
@@ -2,8 +2,8 @@
package main
import (
_ "embed"
"darvaza.org/sidecar/pkg/logger/zerolog"
"darvaza.org/slog"
"github.com/spf13/cobra"
)
@@ -13,10 +13,11 @@ const (
)
var (
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
Version: version,
log = zerolog.New(nil, slog.Error)
verbosity int
rootCmd = &cobra.Command{
Use: CmdName,
Short: "control tool for jpi.cloud",
}
)
@@ -25,3 +26,16 @@ func main() {
fatal(err, "")
}
}
func init() {
rootCmd.PersistentFlags().CountVarP(&verbosity, "verbosity", "v",
"increase the verbosity level to Warn, Info or Debug")
}
func setVerbosity(_ *cobra.Command, _ []string) {
desired := int8(slog.Error) + int8(verbosity)
if desired > 6 {
desired = 6
}
log = zerolog.New(nil, slog.LogLevel(desired))
}
-31
View File
@@ -1,31 +0,0 @@
package main
import (
_ "embed"
"fmt"
"os"
"github.com/spf13/cobra"
)
//go:generate sh -c "git describe | tr -d '\r\n' > .version"
//go:embed .version
var version string
var versionCmd = &cobra.Command{
Use: "version",
Short: "Returns jpictl's version",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", version)
},
}
func init() {
if version == "" {
version = "undetermined"
}
rootCmd.AddCommand(versionCmd)
}
+35 -29
View File
@@ -1,43 +1,49 @@
module git.jpi.io/amery/jpictl
go 1.21
go 1.19
replace asciigoat.org/ini => ../../../asciigoat.org/ini
require (
asciigoat.org/core v0.3.9 // indirect
asciigoat.org/ini v0.2.5
darvaza.org/cache/x/simplelru v0.1.8 // indirect
darvaza.org/core v0.14.2
darvaza.org/resolver v0.9.2
darvaza.org/sidecar v0.4.0
darvaza.org/slog v0.5.7
darvaza.org/slog/handlers/discard v0.4.11
darvaza.org/slog/handlers/filter v0.4.9 // indirect
darvaza.org/slog/handlers/zerolog v0.4.9 // indirect
)
require (
github.com/gofrs/uuid/v5 v5.2.0
darvaza.org/core v0.9.8
darvaza.org/resolver v0.5.4
darvaza.org/sidecar v0.0.2
darvaza.org/slog v0.5.3
darvaza.org/slog/handlers/discard v0.4.5
github.com/gofrs/uuid/v5 v5.0.0
github.com/hack-pad/hackpadfs v0.2.1
github.com/libdns/cloudflare v0.1.1
github.com/libdns/libdns v0.2.2
github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.25.0
golang.org/x/net v0.27.0
github.com/libdns/cloudflare v0.1.0
github.com/libdns/libdns v0.2.1
github.com/mgechev/revive v1.3.3
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0
golang.org/x/net v0.14.0
gopkg.in/yaml.v3 v3.0.1
)
require (
asciigoat.org/core v0.3.9 // indirect
darvaza.org/slog/handlers/filter v0.4.5 // indirect
darvaza.org/slog/handlers/zerolog v0.4.5 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/chavacava/garif v0.1.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.59 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 // indirect
github.com/miekg/dns v1.1.55 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rs/zerolog v1.30.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.12.0 // indirect
)
+83 -58
View File
@@ -1,81 +1,106 @@
asciigoat.org/core v0.3.9 h1:hgDDz4ecm3ZvehX++m8A/IzAt+B5oDPiRtxatzfUHPQ=
asciigoat.org/core v0.3.9/go.mod h1:CAaHwyw8MpAq4a1MYtN2dxJrsK+hmIdW50OndaQZYPI=
asciigoat.org/ini v0.2.5 h1:4gRIp9rU+XQt8+HMqZO5R7GavMv9Yl2+N+je6djDIAE=
asciigoat.org/ini v0.2.5/go.mod h1:gmXzJ9XFqf1NLk5nQkj04USQ4tMtdRJHNQX6vp3DzjU=
darvaza.org/cache/x/simplelru v0.1.8 h1:rvFucut4wKYbsYc994yR3P0M08NqlsvZxr5G4QK82tw=
darvaza.org/cache/x/simplelru v0.1.8/go.mod h1:Mv1isOJTcXYK+aK0AvUe+/3KpRTXDsYga6rdTS/upNs=
darvaza.org/core v0.14.2 h1:6p0iznuGfVGbBp+CnkZTw1b76j6Q/j4ffDztZXrrlK8=
darvaza.org/core v0.14.2/go.mod h1:C+B0GRNLB+/asGfxjQ9XZERdk7xaFxzt5xTIBPiNm2M=
darvaza.org/resolver v0.9.2 h1:sUX6LZ1eN5TzJW7L4m7HM+BvwBeWl8dYYDGVSe+AIhk=
darvaza.org/resolver v0.9.2/go.mod h1:XWqPhrxoOKNzRuSozOwmE1M6QVqQL28jEdxylnIO8Nw=
darvaza.org/sidecar v0.4.0 h1:wHghxzLsiT82WDBBUf34aTqtOvRBg4UbxVIJgKNXRVA=
darvaza.org/sidecar v0.4.0/go.mod h1:fUzjcFM4rN3bSEl4BKvok3MLpZWEhEa9+0/egmtpfMY=
darvaza.org/slog v0.5.7 h1:JWC0OqvzR435AidIRDp4T9kdWTURWkUjzP4R78Koq1Q=
darvaza.org/slog v0.5.7/go.mod h1:12L03t+KYhsZ9IbfF+8if5w9Y91af2par+bSzeBVqIQ=
darvaza.org/slog/handlers/discard v0.4.11 h1:wr34OnDoRaMV1eGgW7yUaupQxjkTnuHrJmYRPj64RHM=
darvaza.org/slog/handlers/discard v0.4.11/go.mod h1:ynxyLmZzZ5mP4ACLhQs4MEuDyhkIzjz6DfBHUjhnIK4=
darvaza.org/slog/handlers/filter v0.4.9 h1:xD8OBwlJytpiwTSDDZqUuNSOsJuaManXQiOj9WEStr8=
darvaza.org/slog/handlers/filter v0.4.9/go.mod h1:t+sjcf1c46kAdf1TRiQmop91xlkteZrC4WDXoVwHgP8=
darvaza.org/slog/handlers/zerolog v0.4.9 h1:08FjRnwRGtJsLLBnbgxVorb/bkgm5QEM/LXD2cxeCbM=
darvaza.org/slog/handlers/zerolog v0.4.9/go.mod h1:PZYfx6eOxQfD+cXJQp52iwKgcD30QVYHoXxOCojAOdw=
darvaza.org/core v0.9.8 h1:luLxgfUc2pzuusYPo/Z/dC/qr9XZPKpSQw8/kS7zNUM=
darvaza.org/core v0.9.8/go.mod h1:Dbme64naxeshQfxcVJX9ZT7AiGyIY8kldfuELVtf8mw=
darvaza.org/resolver v0.5.4 h1:dlSBNV14yYsp7Kg7ipwYOMNsLbrpeXa8Z0HBTa0Ryxs=
darvaza.org/resolver v0.5.4/go.mod h1:vHMkQUmHjaetFqG2ZLZJiQHsXEMGoTOFGm+NXwfndhE=
darvaza.org/sidecar v0.0.2 h1:4H8FUxc43kkLjxdShN1CoxLTcoHQsZjDVwm7kt6eIK0=
darvaza.org/sidecar v0.0.2/go.mod h1:yFC3Qt3j+uS7n9CMpLxwrA68z+FNJhENoenBc9zBJJo=
darvaza.org/slog v0.5.3 h1:sQzmZXgqRh9oFMKBwEYrEpucLvKJVZxaxa2bHIA6GJ0=
darvaza.org/slog v0.5.3/go.mod h1:59d+yi+C7gn4pDDuwbbOKawERpdXthFFk1Yc+Sv6XB0=
darvaza.org/slog/handlers/discard v0.4.5 h1:RRykOItNolHyiUav57lG/GFBL33rcljoa0nWTpY+T0g=
darvaza.org/slog/handlers/discard v0.4.5/go.mod h1:HYHfISQjMqcPbPoPZ92ib/u7s9JcXvF6OaygpPFwdF8=
darvaza.org/slog/handlers/filter v0.4.5 h1:CX1bMzldd67e3y3s3Sh4jK8Lyo0WMvTGBB2lD315jhc=
darvaza.org/slog/handlers/filter v0.4.5/go.mod h1:OuH9rHYg9CIErTJCZliMnFexBfP/HJ9PZ1V1VwSCZ1g=
darvaza.org/slog/handlers/zerolog v0.4.5 h1:W4cgGORx4wImr+RL96CWSQGTdkZzKX6YHXPSYJvdoB4=
darvaza.org/slog/handlers/zerolog v0.4.5/go.mod h1:mCoh/mIl8Nsa6Yu1Um7d7cos6RuEJzgaTXaX5LDRUao=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid/v5 v5.2.0 h1:qw1GMx6/y8vhVsx626ImfKMuS5CvJmhIKKtuyvfajMM=
github.com/gofrs/uuid/v5 v5.2.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/hack-pad/hackpadfs v0.2.1 h1:FelFhIhv26gyjujoA/yeFO+6YGlqzmc9la/6iKMIxMw=
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/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/libdns/cloudflare v0.1.1 h1:FVPfWwP8zZCqj268LZjmkDleXlHPlFU9KC4OJ3yn054=
github.com/libdns/cloudflare v0.1.1/go.mod h1:9VK91idpOjg6v7/WbjkEW49bSCxj00ALesIFDhJ8PBU=
github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s=
github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
github.com/libdns/cloudflare v0.1.0 h1:93WkJaGaiXCe353LHEP36kAWCUw0YjFqwhkBkU2/iic=
github.com/libdns/cloudflare v0.1.0/go.mod h1:a44IP6J1YH6nvcNl1PverfJviADgXUnsozR3a7vBKN8=
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
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.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/miekg/dns v1.1.59 h1:C9EXc/UToRwKLhK5wKU/I4QVsBUc8kE6MkHBkeypWZs=
github.com/miekg/dns v1.1.59/go.mod h1:nZpewl5p6IvctfgrckopVx2OlSEHPRO/U4SYkRklrEk=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
github.com/mgechev/revive v1.3.3 h1:GUWzV3g185agbHN4ZdaQvR6zrLVYTUSA2ktvIinivK0=
github.com/mgechev/revive v1.3.3/go.mod h1:NhpOtVtDbjYNDj697eDUBTobijCDHQKar4HDKc0TuTo=
github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
-14
View File
@@ -1,14 +0,0 @@
{
"folders": [
{
"path": "."
},
{
"path": "../../../bitbucket.org/jpi/m.jpi.cloud"
},
{
"path": "../../../darvaza.org/core"
}
],
"settings": {}
}
-8
View File
@@ -1,8 +0,0 @@
[global]
fsid = 28180b9b-6d5d-4be0-bb5b-e4b04bf03804
mon_initial_members = ssd-lon-7, ssd-ams-4, htz-fsn-1, ssd-nyc-3
mon_host = 10.2.16.7, 10.1.32.4, 10.1.48.1, 10.3.64.3
cluster_network = 10.0.0.0/8
; don't rewrite labels on startup
osd_class_update_on_start = false
View File
-2
View File
@@ -1,2 +0,0 @@
de
nl
-2
View File
@@ -1,2 +0,0 @@
eu
uk
-2
View File
@@ -1,2 +0,0 @@
europe
us
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s ceph k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-23
View File
@@ -1,23 +0,0 @@
[Interface]
# Name: htz-fsn-1-0
Address = 10.0.19.1
PrivateKey = AMz/pCgOMVh1/2rT9uCFMox+LQgH5FrE4xUeqSs0x0M=
ListenPort = 51800
[Peer]
# Name: ssd-lon-3-0
PublicKey = aDXNwapVjufcuQKfBN+waJObe1TaeeaSNl1suA0u+00=
Endpoint = ssd-lon-3.m.jpi.cloud:51800
AllowedIPs = 10.2.16.0/20, 10.0.33.3/32
[Peer]
# Name: ssd-ams-3-0
PublicKey = y/Gkid2Mjxo1WO9Zi7mh+DETNYKT/AAc3K3bvr9xp3c=
Endpoint = ssd-ams-3.m.jpi.cloud:51800
AllowedIPs = 10.1.32.0/20, 10.0.18.3/32
[Peer]
# Name: ssd-nyc-3-0
PublicKey = 1UFgleUufTszPM1voIcwVhd0DpYZ1HwV05U6p3IS2DE=
Endpoint = ssd-nyc-3.m.jpi.cloud:51800
AllowedIPs = 10.3.64.0/20, 10.0.52.3/32
-1
View File
@@ -1 +0,0 @@
AMz/pCgOMVh1/2rT9uCFMox+LQgH5FrE4xUeqSs0x0M=
-1
View File
@@ -1 +0,0 @@
K9LYog8vtRFN4nW6dJWMQ9trmRrYuLpbu/Ze5kIiBGw=
-5
View File
@@ -1,5 +0,0 @@
[Interface]
# Name: htz-fsn-1-1
Address = 10.1.48.1
PrivateKey = 2JpA1G0Dp+sEuDhKnzToz0h76o2iLP+S7Qb9itlgXVA=
ListenPort = 51810
-1
View File
@@ -1 +0,0 @@
2JpA1G0Dp+sEuDhKnzToz0h76o2iLP+S7Qb9itlgXVA=
-1
View File
@@ -1 +0,0 @@
qWmYfgeRAQ563gWLdeYs65XjGJTSE+W8WTbpLZ0e1CQ=
-1
View File
@@ -1 +0,0 @@
de
-1
View File
@@ -1 +0,0 @@
nl
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s ceph k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-23
View File
@@ -1,23 +0,0 @@
[Interface]
# Name: ssd-ams-3-0
Address = 10.0.18.3
PrivateKey = oHVQY976pFH1lAhCrz9c40vR/e5PIXw7ZXv6pU6A5Ho=
ListenPort = 51800
[Peer]
# Name: ssd-lon-3-0
PublicKey = aDXNwapVjufcuQKfBN+waJObe1TaeeaSNl1suA0u+00=
Endpoint = ssd-lon-3.m.jpi.cloud:51800
AllowedIPs = 10.2.16.0/20, 10.0.33.3/32
[Peer]
# Name: htz-fsn-1-0
PublicKey = K9LYog8vtRFN4nW6dJWMQ9trmRrYuLpbu/Ze5kIiBGw=
Endpoint = htz-fsn-1.m.jpi.cloud:51800
AllowedIPs = 10.1.48.0/20, 10.0.19.1/32
[Peer]
# Name: ssd-nyc-3-0
PublicKey = 1UFgleUufTszPM1voIcwVhd0DpYZ1HwV05U6p3IS2DE=
Endpoint = ssd-nyc-3.m.jpi.cloud:51800
AllowedIPs = 10.3.64.0/20, 10.0.52.3/32
-1
View File
@@ -1 +0,0 @@
oHVQY976pFH1lAhCrz9c40vR/e5PIXw7ZXv6pU6A5Ho=
-1
View File
@@ -1 +0,0 @@
y/Gkid2Mjxo1WO9Zi7mh+DETNYKT/AAc3K3bvr9xp3c=
-17
View File
@@ -1,17 +0,0 @@
[Interface]
# Name: ssd-ams-3-1
Address = 10.1.32.3
PrivateKey = 6I/ZMxaWUTbNc+SIGkNKxgovONqv6EAvg7NeeTRrR1c=
ListenPort = 51810
[Peer]
# Name: ssd-ams-4-1
PublicKey = QpJupYlnJc7wc3+gAlEmbFMonsqpVnYRy7f/gnPqqBw=
Endpoint = ssd-ams-4.m.jpi.cloud:51810
AllowedIPs = 10.1.32.4/32
[Peer]
# Name: ssd-ams-5-1
PublicKey = HDLVEwLv0uugyZ9C1C9ZWuiaWQ1qIqqe/DAUxYI0whM=
Endpoint = ssd-ams-5.m.jpi.cloud:51810
AllowedIPs = 10.1.32.5/32
-1
View File
@@ -1 +0,0 @@
6I/ZMxaWUTbNc+SIGkNKxgovONqv6EAvg7NeeTRrR1c=
-1
View File
@@ -1 +0,0 @@
9i0XI358OOYYoxoxV5lX8siqE8uiEwCsuWLo6LoFu0Y=
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s ceph k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-17
View File
@@ -1,17 +0,0 @@
[Interface]
# Name: ssd-ams-4-1
Address = 10.1.32.4
PrivateKey = SPBd5Ka29ZecKYy7BW85rW7FO9dwKwoUHlSibybPG1s=
ListenPort = 51810
[Peer]
# Name: ssd-ams-3-1
PublicKey = 9i0XI358OOYYoxoxV5lX8siqE8uiEwCsuWLo6LoFu0Y=
Endpoint = ssd-ams-3.m.jpi.cloud:51810
AllowedIPs = 10.1.32.3/32, 10.2.16.0/20, 10.1.48.0/20, 10.3.64.0/20, 10.0.33.3/32, 10.0.18.3/32, 10.0.19.1/32, 10.0.52.3/32
[Peer]
# Name: ssd-ams-5-1
PublicKey = HDLVEwLv0uugyZ9C1C9ZWuiaWQ1qIqqe/DAUxYI0whM=
Endpoint = ssd-ams-5.m.jpi.cloud:51810
AllowedIPs = 10.1.32.5/32
-1
View File
@@ -1 +0,0 @@
SPBd5Ka29ZecKYy7BW85rW7FO9dwKwoUHlSibybPG1s=
-1
View File
@@ -1 +0,0 @@
QpJupYlnJc7wc3+gAlEmbFMonsqpVnYRy7f/gnPqqBw=
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s ceph k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-1
View File
@@ -1 +0,0 @@
none
-17
View File
@@ -1,17 +0,0 @@
[Interface]
# Name: ssd-ams-5-1
Address = 10.1.32.5
PrivateKey = mJaFvu9TBwE2i0rjhfhvpB3xZmp++3BVwa7QAvWXImY=
ListenPort = 51810
[Peer]
# Name: ssd-ams-3-1
PublicKey = 9i0XI358OOYYoxoxV5lX8siqE8uiEwCsuWLo6LoFu0Y=
Endpoint = ssd-ams-3.m.jpi.cloud:51810
AllowedIPs = 10.1.32.3/32, 10.2.16.0/20, 10.1.48.0/20, 10.3.64.0/20, 10.0.33.3/32, 10.0.18.3/32, 10.0.19.1/32, 10.0.52.3/32
[Peer]
# Name: ssd-ams-4-1
PublicKey = QpJupYlnJc7wc3+gAlEmbFMonsqpVnYRy7f/gnPqqBw=
Endpoint = ssd-ams-4.m.jpi.cloud:51810
AllowedIPs = 10.1.32.4/32
-1
View File
@@ -1 +0,0 @@
mJaFvu9TBwE2i0rjhfhvpB3xZmp++3BVwa7QAvWXImY=
-1
View File
@@ -1 +0,0 @@
HDLVEwLv0uugyZ9C1C9ZWuiaWQ1qIqqe/DAUxYI0whM=
-1
View File
@@ -1 +0,0 @@
uk
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s ceph k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-23
View File
@@ -1,23 +0,0 @@
[Interface]
# Name: ssd-lon-3-0
Address = 10.0.33.3
PrivateKey = WJY1yrd14c0gY4qqq1O8BPck46TVYebXrT+OoMbEqG8=
ListenPort = 51800
[Peer]
# Name: ssd-ams-3-0
PublicKey = y/Gkid2Mjxo1WO9Zi7mh+DETNYKT/AAc3K3bvr9xp3c=
Endpoint = ssd-ams-3.m.jpi.cloud:51800
AllowedIPs = 10.1.32.0/20, 10.0.18.3/32
[Peer]
# Name: htz-fsn-1-0
PublicKey = K9LYog8vtRFN4nW6dJWMQ9trmRrYuLpbu/Ze5kIiBGw=
Endpoint = htz-fsn-1.m.jpi.cloud:51800
AllowedIPs = 10.1.48.0/20, 10.0.19.1/32
[Peer]
# Name: ssd-nyc-3-0
PublicKey = 1UFgleUufTszPM1voIcwVhd0DpYZ1HwV05U6p3IS2DE=
Endpoint = ssd-nyc-3.m.jpi.cloud:51800
AllowedIPs = 10.3.64.0/20, 10.0.52.3/32
-1
View File
@@ -1 +0,0 @@
WJY1yrd14c0gY4qqq1O8BPck46TVYebXrT+OoMbEqG8=
-1
View File
@@ -1 +0,0 @@
aDXNwapVjufcuQKfBN+waJObe1TaeeaSNl1suA0u+00=
-11
View File
@@ -1,11 +0,0 @@
[Interface]
# Name: ssd-lon-3-1
Address = 10.2.16.3
PrivateKey = 2CDO+x8HVKfw1/iqC2WkwIEQyWZsC9XPW1otqxarR0o=
ListenPort = 51810
[Peer]
# Name: ssd-lon-7-1
PublicKey = XGVXDmxQajHpsgpzgrp+r/ZWxZUaodTpvGBJuLlZ0n4=
Endpoint = ssd-lon-7.m.jpi.cloud:51810
AllowedIPs = 10.2.16.7/32
-1
View File
@@ -1 +0,0 @@
2CDO+x8HVKfw1/iqC2WkwIEQyWZsC9XPW1otqxarR0o=
-1
View File
@@ -1 +0,0 @@
untmMfW2W4OiMgG/6nAysoivXtDh7cZ9/rxhgJ4OlC4=
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s ceph k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-11
View File
@@ -1,11 +0,0 @@
[Interface]
# Name: ssd-lon-7-1
Address = 10.2.16.7
PrivateKey = iN4Rc3J36APlkIP/ksSpHIwAuxC/ehYLwGQ5va1AhG8=
ListenPort = 51810
[Peer]
# Name: ssd-lon-3-1
PublicKey = untmMfW2W4OiMgG/6nAysoivXtDh7cZ9/rxhgJ4OlC4=
Endpoint = ssd-lon-3.m.jpi.cloud:51810
AllowedIPs = 10.2.16.3/32, 10.1.32.0/20, 10.1.48.0/20, 10.3.64.0/20, 10.0.33.3/32, 10.0.18.3/32, 10.0.19.1/32, 10.0.52.3/32
-1
View File
@@ -1 +0,0 @@
iN4Rc3J36APlkIP/ksSpHIwAuxC/ehYLwGQ5va1AhG8=
-1
View File
@@ -1 +0,0 @@
XGVXDmxQajHpsgpzgrp+r/ZWxZUaodTpvGBJuLlZ0n4=
-1
View File
@@ -1 +0,0 @@
us
-22
View File
@@ -1,22 +0,0 @@
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
10.2.16.3 ssd-lon-3
10.2.16.7 ssd-lon-7 ssd-lon-ceph ssd-lon-k3s
10.1.32.3 ssd-ams-3
10.1.32.4 ssd-ams-4 ssd-ams-ceph ssd-ams-k3s
10.1.32.5 ssd-ams-5
10.1.48.1 htz-fsn-1 htz-fsn-ceph htz-fsn-k3s
10.3.64.3 ssd-nyc-3 ssd-nyc-ceph ssd-nyc-k3s ceph k3s
10.0.33.3 ssd-lon-3-0
10.0.18.3 ssd-ams-3-0
10.0.19.1 htz-fsn-1-0
10.0.52.3 ssd-nyc-3-0
-1
View File
@@ -1 +0,0 @@
none
-23
View File
@@ -1,23 +0,0 @@
[Interface]
# Name: ssd-nyc-3-0
Address = 10.0.52.3
PrivateKey = YNDxj+QSO3p5pXw9h7lBx0cLM0kJDv3v7BYLfc8TR3I=
ListenPort = 51800
[Peer]
# Name: ssd-lon-3-0
PublicKey = aDXNwapVjufcuQKfBN+waJObe1TaeeaSNl1suA0u+00=
Endpoint = ssd-lon-3.m.jpi.cloud:51800
AllowedIPs = 10.2.16.0/20, 10.0.33.3/32
[Peer]
# Name: ssd-ams-3-0
PublicKey = y/Gkid2Mjxo1WO9Zi7mh+DETNYKT/AAc3K3bvr9xp3c=
Endpoint = ssd-ams-3.m.jpi.cloud:51800
AllowedIPs = 10.1.32.0/20, 10.0.18.3/32
[Peer]
# Name: htz-fsn-1-0
PublicKey = K9LYog8vtRFN4nW6dJWMQ9trmRrYuLpbu/Ze5kIiBGw=
Endpoint = htz-fsn-1.m.jpi.cloud:51800
AllowedIPs = 10.1.48.0/20, 10.0.19.1/32
-1
View File
@@ -1 +0,0 @@
YNDxj+QSO3p5pXw9h7lBx0cLM0kJDv3v7BYLfc8TR3I=
-1
View File
@@ -1 +0,0 @@
1UFgleUufTszPM1voIcwVhd0DpYZ1HwV05U6p3IS2DE=
-5
View File
@@ -1,5 +0,0 @@
[Interface]
# Name: ssd-nyc-3-1
Address = 10.3.64.3
PrivateKey = APXGKqOxx1gOr587FSN3O3gRGpkGgSjt6zGlTu49UlU=
ListenPort = 51810
-1
View File
@@ -1 +0,0 @@
APXGKqOxx1gOr587FSN3O3gRGpkGgSjt6zGlTu49UlU=
-1
View File
@@ -1 +0,0 @@
av8ni/9ZUyozabzqTjIy1sOnSJuQ9p63Tu9ECVmHB2I=
View File
View File
-3
View File
@@ -39,9 +39,6 @@ func writeGlobalToBuffer(w *bytes.Buffer, c *GlobalConfig) {
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_initial_members", strings.Join(c.Monitors, ", "))
_, _ = fmt.Fprintf(w, "%s = %s\n", "mon_host", joinAddrs(c.MonitorsAddr, ", "))
_, _ = fmt.Fprintf(w, "%s = %s\n", "cluster_network", c.ClusterNetwork.String())
_, _ = fmt.Fprintf(w, "\n; %s\n", "don't rewrite labels on startup")
_, _ = fmt.Fprintf(w, "%s = %s\n", "osd_class_update_on_start", "false")
}
func joinAddrs(addrs []netip.Addr, sep string) string {
+2 -2
View File
@@ -17,7 +17,7 @@ var sectionMap = map[string]func(*Config, *basic.Section) error{
func loadConfSection(out *Config, src *basic.Section) error {
h, ok := sectionMap[src.Key]
if !ok {
return core.Wrap(fs.ErrInvalid, "unknown section %q", src.Key)
return core.Wrapf(fs.ErrInvalid, "unknown section %q", src.Key)
}
return h(out, src)
@@ -48,7 +48,7 @@ func loadGlobalConfField(cfg *GlobalConfig, field basic.Field) error {
switch field.Key {
case "fsid":
if !core.IsZero(cfg.FSID) {
return core.Wrap(fs.ErrInvalid, "duplicate field %q", field.Key)
return core.Wrapf(fs.ErrInvalid, "duplicate field %q", field.Key)
}
err := cfg.FSID.UnmarshalText([]byte(field.Value))
-43
View File
@@ -1,43 +0,0 @@
package cluster
import (
"net/netip"
"git.jpi.io/amery/jpictl/pkg/rings"
)
// RingOnePrefix returns the ring 1 subnet of this [Zone].
func (z *Zone) RingOnePrefix() netip.Prefix {
subnet, err := rings.RingOnePrefix(z.RegionID(), z.ID)
if err != nil {
panic(err)
}
return subnet
}
// RingOnePrefix returns the ring 1 subnet this [Machine] belongs
// to.
func (m *Machine) RingOnePrefix() netip.Prefix {
return m.zone.RingOnePrefix()
}
// RingZeroAddress returns the ring 0 address of the [Machine]
// if it can act as gateway.
func (m *Machine) RingZeroAddress() (netip.Addr, bool) {
addr, err := rings.RingZeroAddress(m.Region(), m.Zone(), m.ID)
if err != nil {
return netip.Addr{}, false
}
return addr, true
}
// RingOneAddress returns the ring 1 address of the [Machine]
func (m *Machine) RingOneAddress() netip.Addr {
addr, err := rings.RingOneAddress(m.Region(), m.Zone(), m.ID)
if err != nil {
panic(err)
}
return addr
}
+1 -1
View File
@@ -66,7 +66,7 @@ func (m *Cluster) GenCephConfig() (*ceph.Config, error) {
m.ForEachZone(func(z *Zone) bool {
for _, p := range z.GetCephMonitors() {
addr := p.RingOneAddress()
addr, _ := RingOneAddress(z.ID, p.ID)
cfg.Global.Monitors = append(cfg.Global.Monitors, p.Name)
cfg.Global.MonitorsAddr = append(cfg.Global.MonitorsAddr, addr)
+6 -3
View File
@@ -4,7 +4,6 @@ import (
"os"
"darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/ceph"
)
@@ -15,7 +14,8 @@ type cephScanTODO struct {
func (todo *cephScanTODO) checkMachine(p *Machine) bool {
// on ceph all addresses are ring1
addr := p.RingOneAddress().String()
ring1, _ := RingOneAddress(p.Zone(), p.ID)
addr := ring1.String()
if _, found := todo.names[p.Name]; found {
// found on the TODO by name
@@ -73,7 +73,10 @@ func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
func (m *Cluster) scanCephMonitors(opts *ScanOptions) error {
cfg, err := m.GetCephConfig()
if err != nil && !os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
err = nil
case err != nil:
return err
}
+1 -70
View File
@@ -1,12 +1,9 @@
package cluster
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
fs "github.com/hack-pad/hackpadfs"
)
@@ -36,26 +33,7 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
return nil, err
}
if f, ok := f.(io.WriteCloser); ok {
return f, nil
}
panic("unreachable")
}
// RemoveFile deletes a file from the cluster's config directory
func (m *Cluster) RemoveFile(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
err := fs.Remove(m.dir, name)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
return f.(io.WriteCloser), nil
}
// ReadFile reads a file from the cluster's config directory
@@ -66,50 +44,3 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(m.dir, name)
}
// ReadLines reads a file from the cluster's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) {
var out []string
data, err := m.ReadFile(name, args...)
if err != nil {
return nil, err
}
sc := bufio.NewScanner(bytes.NewReader(data))
for sc.Scan() {
s := strings.TrimSpace(sc.Text())
switch {
case s == "", strings.HasPrefix(s, "#"):
// ignore
default:
// accepted
out = append(out, s)
}
}
return out, nil
}
// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Cluster) WriteStringFile(value string, name string, args ...any) error {
f, err := m.CreateTruncFile(name, args...)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
}
// MkdirAll creates directories relative to the cluster's config directory
func (m *Cluster) MkdirAll(name string, args ...any) error {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.MkdirAll(m.dir, name, 0755)
}
+3 -5
View File
@@ -6,18 +6,16 @@ import (
"os"
"gopkg.in/yaml.v3"
"git.jpi.io/amery/jpictl/pkg/rings"
)
func (m *Cluster) init(opts *ScanOptions) error {
for _, fn := range []func(*ScanOptions) error{
m.initZones,
m.initRegions,
m.scanZoneIDs,
m.scanSort,
m.scanGateways,
m.initCephMonitors,
m.initRegions,
} {
if err := fn(opts); err != nil {
return err
@@ -47,7 +45,7 @@ func (m *Cluster) initZones(opts *ScanOptions) error {
func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error {
var hasMissing bool
var lastMachineID rings.NodeID
var lastMachineID int
z.zones = m
z.logger = m
@@ -60,7 +58,7 @@ func (m *Cluster) initZone(z *Zone, _ *ScanOptions) error {
case p.ID == 0:
hasMissing = true
case p.ID > lastMachineID:
lastMachineID = p.ID
lastMachineID = z.ID
}
return false
+27 -127
View File
@@ -2,30 +2,15 @@ package cluster
import (
"io/fs"
"path"
"sort"
"strings"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
)
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 {
for _, fn := range []func(*ScanOptions) error{
m.scanDirectory,
m.scanMachines,
m.initRegions,
m.scanZoneIDs,
m.scanSort,
m.scanGateways,
@@ -39,7 +24,7 @@ func (m *Cluster) scan(opts *ScanOptions) error {
return nil
}
func (m *Cluster) scanDirectory(opts *ScanOptions) error {
func (m *Cluster) scanDirectory(_ *ScanOptions) error {
// each directory is a zone
entries, err := fs.ReadDir(m.dir, ".")
if err != nil {
@@ -48,14 +33,16 @@ func (m *Cluster) scanDirectory(opts *ScanOptions) error {
for _, e := range entries {
if e.IsDir() {
ok, err := m.scanSubdirectory(opts, e.Name())
z, err := m.newZone(e.Name())
switch {
case err != nil:
return core.Wrap(err, e.Name())
case !ok:
m.warn(nil).
WithField("zone", e.Name()).
case z.Machines.Len() == 0:
z.warn(nil).
WithField("zone", z.Name).
Print("empty")
default:
m.Zones = append(m.Zones, z)
}
}
}
@@ -63,27 +50,6 @@ func (m *Cluster) scanDirectory(opts *ScanOptions) error {
return nil
}
func (m *Cluster) scanSubdirectory(_ *ScanOptions, name string) (bool, error) {
z, err := m.newZone(name)
switch {
case err != nil:
// somewhere went wrong scanning the subdirectory
return false, err
case z.Machines.Len() > 0:
// zones have machines and the regions they belong
m.Zones = append(m.Zones, z)
return true, nil
case len(z.Regions) > 0:
// regions have no machines but can include
// other regions
m.appendRegionRegions(name, z.Regions...)
return true, nil
default:
// empty
return false, nil
}
}
func (m *Cluster) newZone(name string) (*Zone, error) {
z := &Zone{
zones: m,
@@ -107,16 +73,12 @@ func (m *Cluster) scanMachines(opts *ScanOptions) error {
err = p.scan(opts)
return err != nil
})
m.ForEachMachine(func(p *Machine) bool {
err = p.scanWrapUp(opts)
return err != nil
})
return err
}
func (m *Cluster) scanZoneIDs(_ *ScanOptions) error {
var hasMissing bool
var lastZoneID rings.ZoneID
var lastZoneID int
m.ForEachZone(func(z *Zone) bool {
switch {
@@ -188,96 +150,34 @@ func (z *Zone) scan() error {
}
for _, e := range entries {
name := e.Name()
if e.IsDir() {
m := &Machine{
zone: z,
logger: z,
Name: e.Name(),
}
switch {
case name == ZoneRegionsFileName:
err = z.loadRegions()
case name == RegionClusterTokenFileName:
err = z.loadClusterToken()
case e.IsDir():
err = z.scanSubdirectory(name)
default:
z.warn(nil).
m.debug().
WithField("node", m.Name).
WithField("zone", z.Name).
WithField("filename", name).
Print("unknown")
}
Print("found")
if err != nil {
return err
if err := m.init(); err != nil {
m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err
}
z.Machines = append(z.Machines, m)
}
}
return nil
}
func (z *Zone) loadRegions() error {
filename := path.Join(z.Name, ZoneRegionsFileName)
regions, err := z.zones.ReadLines(filename)
if err == nil {
// parsed
err = z.appendRegions(regions...)
if err != nil {
err = core.Wrap(err, filename)
}
}
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,
logger: z,
Name: name,
}
m.debug().
WithField("node", m.Name).
WithField("zone", z.Name).
Print("found")
if err := m.init(); err != nil {
m.error(err).
WithField("node", m.Name).
WithField("zone", z.Name).
Print()
return err
}
z.Machines = append(z.Machines, m)
return nil
}
// GetGateway returns the first gateway found, if none
// files will be created to enable the first [Machine] to
// be one
+3 -3
View File
@@ -27,7 +27,7 @@ type ScanOptions struct {
// the DNS resolver to get PublicAddresses of nodes.
// Default is true
func ResolvePublicAddresses(resolve bool) ScanOption {
return func(_ *Cluster, opt *ScanOptions) error {
return func(m *Cluster, opt *ScanOptions) error {
opt.DontResolvePublicAddresses = !resolve
return nil
}
@@ -36,7 +36,7 @@ func ResolvePublicAddresses(resolve bool) ScanOption {
// WithLookuper specifies what resolver.Lookuper to use to
// find public addresses
func WithLookuper(h resolver.Lookuper) ScanOption {
return func(m *Cluster, _ *ScanOptions) error {
return func(m *Cluster, opt *ScanOptions) error {
if h == nil {
return fs.ErrInvalid
}
@@ -49,7 +49,7 @@ func WithLookuper(h resolver.Lookuper) ScanOption {
// public addresses. if nil is passed, the [net.Resolver] will be used.
// The default is using Cloudflare's 1.1.1.1.
func WithResolver(h resolver.Resolver) ScanOption {
return func(m *Cluster, _ *ScanOptions) error {
return func(m *Cluster, opt *ScanOptions) error {
if h == nil {
h = resolver.SystemResolver(true)
}
+48 -129
View File
@@ -5,16 +5,11 @@ import (
"fmt"
"io"
"strings"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
)
// Env is a shell environment factory for this cluster
type Env struct {
ZoneIterator
RegionIterator
cephFSID string
export bool
@@ -28,61 +23,26 @@ func (m *Cluster) Env(export bool) (*Env, error) {
}
env := &Env{
ZoneIterator: m,
RegionIterator: m,
cephFSID: fsid.String(),
export: export,
ZoneIterator: m,
cephFSID: fsid.String(),
export: export,
}
return env, nil
}
// Zones returns the list of Zone IDs of a region,
// or from all if none is specified.
func (m *Env) Zones(r *Region) []rings.ZoneID {
var zones []rings.ZoneID
// Zones returns the list of Zone IDs
func (m *Env) Zones() []int {
var zones []int
iter := core.IIf[ZoneIterator](r != nil, r, m)
iter.ForEachZone(func(z *Zone) bool {
m.ForEachZone(func(z *Zone) bool {
zones = append(zones, z.ID)
return false
})
core.SliceSortOrdered(zones)
return zones
}
// RegionsNames returns a sorted list of primary regions names
func (m *Env) RegionsNames() []string {
var regions []string
m.ForEachRegion(func(r *Region) bool {
if r.IsPrimary() {
regions = append(regions, r.Name)
}
return false
})
core.SliceSortOrdered(regions)
return regions
}
// Regions returns a sorted list of primary regions IDs
func (m *Env) Regions() (regions []rings.RegionID) {
m.ForEachRegion(func(r *Region) bool {
if r.IsPrimary() {
regions = append(regions, r.ID)
}
return false
})
core.SliceSortOrdered(regions)
return regions
}
// WriteTo generates environment variables for shell scripts
func (m *Env) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
@@ -91,75 +51,56 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
m.writeEnvVar(&buf, m.cephFSID, "FSID")
}
regions := m.getRegions()
ids := core.SliceMap(regions, func(_ []rings.RegionID, r *Region) (out []rings.RegionID) {
return append(out, r.ID)
})
names := core.SliceMap(regions, func(_ []string, r *Region) (out []string) {
return append(out, r.Name)
})
m.writeEnvVarInts(&buf, m.Zones(), "ZONES")
m.writeEnvVar(&buf, genEnvInts(ids), "REGIONS")
m.writeEnvVar(&buf, genEnvStrings(names), "REGIONS_NAMES")
for _, r := range regions {
m.writeEnvRegion(&buf, r)
}
m.ForEachZone(func(z *Zone) bool {
m.writeEnvZone(&buf, z)
return false
})
return buf.WriteTo(w)
}
func (m *Env) getRegions() (out []*Region) {
m.ForEachRegion(func(r *Region) bool {
if r.IsPrimary() {
out = append(out, r)
}
return false
})
func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
zoneID := z.ID
core.SliceSortFn(out, func(a, b *Region) bool {
return a.ID < b.ID
})
// ZONE{zoneID}
m.writeEnvVar(w, genEnvZoneNodes(z), "ZONE%v", zoneID)
return out
}
func (m *Env) writeEnvRegion(w io.Writer, r *Region) {
regionID := r.ID
// ZONE{zoneID}_NAME
m.writeEnvVar(w, z.Name, "ZONE%v_%s", zoneID, "NAME")
// REGION{regionID}_NAME
m.writeEnvVar(w, r.Name, "REGION%v_%s", regionID, "NAME")
// REGION{regionID}_ZONES
m.writeEnvVar(w, genEnvInts(m.Zones(r)), "REGION%v_%s", regionID, "ZONES")
r.ForEachZone(func(z *Zone) bool {
m.writeEnvZone(w, r, z)
return false
})
}
func (m *Env) writeEnvZone(w io.Writer, r *Region, z *Zone) {
zonePrefix := fmt.Sprintf("REGION%v_ZONE%v", r.ID, z.ID)
monPrefix := zonePrefix + "_MON"
// REGION{regionID}_ZONE{zoneID}
m.writeEnvVar(w, genEnvZoneNodes(z), zonePrefix)
// REGION{regionID}_ZONE{zoneID}_NAME
m.writeEnvVar(w, z.Name, zonePrefix+"_NAME")
// REGION{regionID}_ZONE{zoneID}_GW
// ZONE{zoneID}_GW
gateways, _ := z.GatewayIDs()
m.writeEnvVar(w, genEnvInts(gateways), zonePrefix+"_GW")
m.writeEnvVarInts(w, gateways, "ZONE%v_%s", zoneID, "GW")
// Ceph
monitors := z.GetCephMonitors()
// REGION{regionID}_MON{zone_ID}
m.writeEnvVar(w, genEnvZoneCephMonNames(monitors), monPrefix)
// REGION{regionID}_MON{zone_ID}_IP
m.writeEnvVar(w, genEnvZoneCephMonIPs(monitors), monPrefix+"_IP")
// REGION{regionID}_MON{zone_ID}_ID
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), monPrefix+"_ID")
// MON{zoneID}_NAME
m.writeEnvVar(w, genEnvZoneCephMonNames(monitors), "MON%v_%s", zoneID, "NAME")
// MON{zoneID}_IP
m.writeEnvVar(w, genEnvZoneCephMonIPs(monitors), "MON%v_%s", zoneID, "IP")
// MON{zoneID}_ID
m.writeEnvVar(w, genEnvZoneCephMonIDs(monitors), "MON%v_%s", zoneID, "ID")
}
func (m *Env) writeEnvVarInts(w io.Writer, value []int, name string, args ...any) {
var s string
if n := len(value); n > 0 {
var buf bytes.Buffer
for i, v := range value {
if i != 0 {
_, _ = fmt.Fprint(&buf, " ")
}
_, _ = fmt.Fprintf(&buf, "%v", v)
}
s = buf.String()
}
m.writeEnvVar(w, s, name, args...)
}
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
@@ -176,32 +117,10 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if name != "" {
value = strings.TrimSpace(value)
if value == "" {
_, _ = fmt.Fprintf(w, "%s%s=\n", prefix, name)
} else {
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
}
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
}
}
func genEnvInts[T core.Signed](values []T) string {
var buf bytes.Buffer
for _, v := range values {
if buf.Len() > 0 {
_, _ = buf.WriteRune(' ')
}
_, _ = buf.WriteString(fmt.Sprintf("%v", int64(v)))
}
return buf.String()
}
func genEnvStrings(values []string) string {
return strings.Join(values, " ")
}
func genEnvZoneNodes(z *Zone) string {
if n := z.Len(); n > 0 {
s := make([]string, 0, n)
@@ -211,7 +130,7 @@ func genEnvZoneNodes(z *Zone) string {
return false
})
return genEnvStrings(s)
return strings.Join(s, " ")
}
return ""
}
@@ -232,7 +151,7 @@ func genEnvZoneCephMonNames(m Machines) string {
func genEnvZoneCephMonIPs(m Machines) string {
var buf strings.Builder
m.ForEachMachine(func(p *Machine) bool {
addr := p.RingOneAddress()
addr, _ := RingOneAddress(p.Zone(), p.ID)
if buf.Len() > 0 {
_, _ = buf.WriteRune(' ')
+1 -14
View File
@@ -1,13 +1,6 @@
package cluster
import (
"errors"
"io/fs"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
)
import "errors"
var (
// ErrInvalidName indicates the name isn't valid
@@ -21,9 +14,3 @@ var (
// the intended purpose
ErrInvalidNode = errors.New("invalid node")
)
// ErrInvalidRing returns an error indicating the [rings.RingID]
// can't be used for the intended purpose
func ErrInvalidRing(ringID rings.RingID) error {
return core.QuietWrap(fs.ErrInvalid, "invalid ring %v", ringID)
}
+3 -3
View File
@@ -71,14 +71,14 @@ func (p *Machine) WriteHosts() error {
func (z *Zone) genHosts(out *hostsFile, p *Machine) {
var names []string
ip := p.RingOneAddress()
ip, _ := RingOneAddress(p.zone.ID, p.ID)
names = append(names, p.Name)
if p.CephMonitor {
names = append(names, fmt.Sprintf("%s-%s", p.zone.Name, "ceph"))
names = append(names, fmt.Sprintf("%s-%s", p.zone.Name, "k3s"))
if z.Is(p.Region(), p.Zone()) {
if z.ID == p.zone.ID {
names = append(names, "ceph")
names = append(names, "k3s")
}
@@ -94,7 +94,7 @@ func (z *Zone) genHosts(out *hostsFile, p *Machine) {
if p.IsGateway() {
var s string
ip, _ = p.RingZeroAddress()
ip, _ = RingZeroAddress(p.zone.ID, p.ID)
s = fmt.Sprintf("%s-%v", p.Name, 0)
entry = hostsEntry{
+5 -18
View File
@@ -3,8 +3,6 @@ package cluster
import (
"net/netip"
"strings"
"git.jpi.io/amery/jpictl/pkg/rings"
)
// revive:disable:line-length-limit
@@ -14,10 +12,9 @@ type Machine struct {
zone *Zone
logger `json:"-" yaml:"-"`
ID rings.NodeID
ID int
Name string `json:"-" yaml:"-"`
Inactive bool `json:"inactive,omitempty" yaml:"inactive,omitempty"`
CephMonitor bool `json:"ceph_monitor,omitempty" yaml:"ceph_monitor,omitempty"`
PublicAddresses []netip.Addr `json:"public,omitempty" yaml:"public,omitempty"`
Rings []*RingInfo `json:"rings,omitempty" yaml:"rings,omitempty"`
@@ -46,20 +43,15 @@ func (m *Machine) FullName() string {
return strings.Join(name, ".")
}
// IsActive indicates the machine is to be included in regions' DNS entries
func (m *Machine) IsActive() bool {
return !m.Inactive
}
// IsGateway tells if the Machine is a ring0 gateway
func (m *Machine) IsGateway() bool {
_, ok := m.getRingInfo(rings.RingZeroID)
_, ok := m.getRingInfo(0)
return ok
}
// SetGateway enables/disables a Machine ring0 integration
func (m *Machine) SetGateway(enabled bool) error {
ri, found := m.getRingInfo(rings.RingZeroID)
ri, found := m.getRingInfo(0)
switch {
case !found && !enabled:
return nil
@@ -72,19 +64,14 @@ func (m *Machine) SetGateway(enabled bool) error {
}
ri.Enabled = enabled
return m.SyncWireguardConfig(rings.RingZeroID)
return m.SyncWireguardConfig(0)
}
// Zone indicates the [Zone] this machine belongs to
func (m *Machine) Zone() rings.ZoneID {
func (m *Machine) Zone() int {
return m.zone.ID
}
// Region indicates the [Region] this machine belongs to
func (m *Machine) Region() rings.RegionID {
return m.zone.RegionID()
}
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name)
}
+22 -25
View File
@@ -1,6 +1,7 @@
package cluster
import (
"bytes"
"fmt"
"io"
"os"
@@ -11,9 +12,10 @@ import (
// OpenFile opens a file on the machine's config directory with the specified flags
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return m.zone.zones.OpenFile(fullName, flags)
return fs.OpenFile(base, fullName, flags, 0644)
}
// CreateTruncFile creates or truncates a file on the machine's config directory
@@ -32,47 +34,42 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
return nil, err
}
if f, ok := f.(io.WriteCloser); ok {
return f, nil
}
panic("unreachable")
return f.(io.WriteCloser), nil
}
// RemoveFile deletes a file from the machine's config directory
func (m *Machine) RemoveFile(name string, args ...any) error {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
err := fs.Remove(base, fullName)
return m.zone.zones.RemoveFile(fullName)
switch {
case os.IsNotExist(err):
return nil
default:
return err
}
}
// ReadFile reads a file from the machine's config directory
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...)
return m.zone.zones.ReadFile(fullName)
}
// ReadLines reads a file from the machine's config directory,
// split by lines, trimmed, and accepting `#` to comment lines out.
func (m *Machine) ReadLines(name string, args ...any) ([]string, error) {
fullName := m.getFilename(name, args...)
return m.zone.zones.ReadLines(fullName)
return fs.ReadFile(base, fullName)
}
// WriteStringFile writes the given content to a file on the machine's config directory
func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
fullName := m.getFilename(name, args...)
f, err := m.CreateTruncFile(name, args...)
if err != nil {
return err
}
defer f.Close()
return m.zone.zones.WriteStringFile(value, fullName)
}
// MkdirAll creates directories relative to the machine's config directory
func (m *Machine) MkdirAll(name string, args ...any) error {
fullName := m.getFilename(name, args...)
return m.zone.zones.MkdirAll(fullName)
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
}
func (m *Machine) getFilename(name string, args ...any) string {
+43 -110
View File
@@ -8,26 +8,18 @@ import (
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
// GetWireguardKeys reads a wgN.key/wgN.pub files
func (m *Machine) GetWireguardKeys(ringID rings.RingID) (wireguard.KeyPair, error) {
func (m *Machine) GetWireguardKeys(ring int) (wireguard.KeyPair, error) {
var (
data []byte
err error
out wireguard.KeyPair
)
ring, err := AsWireguardInterfaceID(ringID)
if err != nil {
// invalid ring
return out, err
}
keyFile, pubFile, _ := ring.Files()
data, err = m.ReadFile(keyFile)
data, err = m.ReadFile("wg%v.key", ring)
if err != nil {
// failed to read
return out, err
@@ -36,11 +28,11 @@ func (m *Machine) GetWireguardKeys(ringID rings.RingID) (wireguard.KeyPair, erro
out.PrivateKey, err = wireguard.PrivateKeyFromBase64(string(data))
if err != nil {
// bad key
err = core.Wrap(err, keyFile)
err = core.Wrapf(err, "wg%v.key", ring)
return out, err
}
data, err = m.ReadFile(pubFile)
data, err = m.ReadFile("wg%v.pub", ring)
switch {
case os.IsNotExist(err):
// no wgN.pub is fine
@@ -52,7 +44,7 @@ func (m *Machine) GetWireguardKeys(ringID rings.RingID) (wireguard.KeyPair, erro
out.PublicKey, err = wireguard.PublicKeyFromBase64(string(data))
if err != nil {
// bad key
err = core.Wrap(err, pubFile)
err = core.Wrapf(err, "wg%v.pub", ring)
return out, err
}
}
@@ -61,8 +53,8 @@ func (m *Machine) GetWireguardKeys(ringID rings.RingID) (wireguard.KeyPair, erro
return out, err
}
func (m *Machine) tryReadWireguardKeys(ringID rings.RingID) error {
kp, err := m.GetWireguardKeys(ringID)
func (m *Machine) tryReadWireguardKeys(ring int) error {
kp, err := m.GetWireguardKeys(ring)
switch {
case os.IsNotExist(err):
// ignore
@@ -73,25 +65,20 @@ func (m *Machine) tryReadWireguardKeys(ringID rings.RingID) error {
default:
// import keys
ri := &RingInfo{
Ring: MustWireguardInterfaceID(ringID),
Ring: ring,
Keys: kp,
}
return m.applyRingInfo(ringID, ri)
return m.applyRingInfo(ring, ri)
}
}
// RemoveWireguardKeys deletes wgN.key and wgN.pub from
// the machine's config directory
func (m *Machine) RemoveWireguardKeys(ringID rings.RingID) error {
ring, err := AsWireguardInterfaceID(ringID)
if err != nil {
return err
}
func (m *Machine) RemoveWireguardKeys(ring int) error {
var err error
keyFile, pubFile, _ := ring.Files()
err = m.RemoveFile(pubFile)
err = m.RemoveFile("wg%v.pub", ring)
switch {
case os.IsNotExist(err):
// ignore
@@ -99,7 +86,7 @@ func (m *Machine) RemoveWireguardKeys(ringID rings.RingID) error {
return err
}
err = m.RemoveFile(keyFile)
err = m.RemoveFile("wg%v.key", ring)
if os.IsNotExist(err) {
// ignore
err = nil
@@ -109,13 +96,8 @@ func (m *Machine) RemoveWireguardKeys(ringID rings.RingID) error {
}
// GetWireguardConfig reads a wgN.conf file
func (m *Machine) GetWireguardConfig(ringID rings.RingID) (*wireguard.Config, error) {
ring, err := AsWireguardInterfaceID(ringID)
if err != nil {
return nil, err
}
data, err := m.ReadFile(ring.ConfFile())
func (m *Machine) GetWireguardConfig(ring int) (*wireguard.Config, error) {
data, err := m.ReadFile("wg%v.conf", ring)
if err != nil {
return nil, err
}
@@ -124,7 +106,7 @@ func (m *Machine) GetWireguardConfig(ringID rings.RingID) (*wireguard.Config, er
return wireguard.NewConfigFromReader(r)
}
func (m *Machine) tryApplyWireguardConfig(ring rings.RingID) error {
func (m *Machine) tryApplyWireguardConfig(ring int) error {
wg, err := m.GetWireguardConfig(ring)
switch {
case os.IsNotExist(err):
@@ -136,31 +118,21 @@ func (m *Machine) tryApplyWireguardConfig(ring rings.RingID) error {
}
}
func (m *Machine) applyWireguardConfigNode(ring rings.RingID, wg *wireguard.Config) error {
func (m *Machine) applyWireguardConfig(ring int, wg *wireguard.Config) error {
addr := wg.GetAddress()
if !core.IsZero(addr) {
regionID, zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid address", addr)
}
zoneID, nodeID, ok := Rings[ring].Decode(addr)
if !ok {
return fmt.Errorf("%s: invalid address", addr)
}
if err := m.applyZoneNodeID(regionID, zoneID, nodeID); err != nil {
return core.Wrap(err, "%s: invalid address", addr)
}
if err := m.applyZoneNodeID(zoneID, nodeID); err != nil {
return core.Wrapf(err, "%s: invalid address", addr)
}
if err := m.applyWireguardInterfaceConfig(ring, wg.Interface); err != nil {
return core.Wrap(err, "interface")
}
return nil
}
func (m *Machine) applyWireguardConfig(ring rings.RingID, wg *wireguard.Config) error {
if err := m.applyWireguardConfigNode(ring, wg); err != nil {
return err
}
for _, peer := range wg.Peer {
err := m.applyWireguardPeerConfig(ring, peer)
switch {
@@ -170,7 +142,7 @@ func (m *Machine) applyWireguardConfig(ring rings.RingID, wg *wireguard.Config)
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("peer", peer.Endpoint.Host).
WithField("ring", MustWireguardInterfaceID(ring)).
WithField("ring", ring).
Print("ignoring unknown endpoint")
case err != nil:
return core.Wrap(err, "peer")
@@ -180,9 +152,9 @@ func (m *Machine) applyWireguardConfig(ring rings.RingID, wg *wireguard.Config)
return nil
}
func (m *Machine) getRingInfo(ring rings.RingID) (*RingInfo, bool) {
func (m *Machine) getRingInfo(ring int) (*RingInfo, bool) {
for _, ri := range m.Rings {
if ri.RingID() == ring {
if ri.Ring == ring {
return ri, ri.Enabled
}
}
@@ -190,13 +162,13 @@ func (m *Machine) getRingInfo(ring rings.RingID) (*RingInfo, bool) {
return nil, false
}
func (m *Machine) applyRingInfo(ring rings.RingID, new *RingInfo) error {
func (m *Machine) applyRingInfo(ring int, new *RingInfo) error {
cur, _ := m.getRingInfo(ring)
if cur == nil {
// first, append
m.debug().
WithField("node", m.Name).
WithField("ring", MustWireguardInterfaceID(ring)).
WithField("ring", ring).
Print("found")
m.Rings = append(m.Rings, new)
return nil
@@ -206,11 +178,9 @@ func (m *Machine) applyRingInfo(ring rings.RingID, new *RingInfo) error {
return cur.Merge(new)
}
func (m *Machine) applyWireguardInterfaceConfig(ring rings.RingID,
data wireguard.InterfaceConfig) error {
//
func (m *Machine) applyWireguardInterfaceConfig(ring int, data wireguard.InterfaceConfig) error {
ri := &RingInfo{
Ring: MustWireguardInterfaceID(ring),
Ring: ring,
Enabled: true,
Keys: wireguard.KeyPair{
PrivateKey: data.PrivateKey,
@@ -220,9 +190,7 @@ func (m *Machine) applyWireguardInterfaceConfig(ring rings.RingID,
return m.applyRingInfo(ring, ri)
}
func (m *Machine) applyWireguardPeerConfig(ring rings.RingID,
pc wireguard.PeerConfig) error {
//
func (m *Machine) applyWireguardPeerConfig(ring int, pc wireguard.PeerConfig) error {
peer, found := m.getPeerByName(pc.Endpoint.Name())
switch {
case !found:
@@ -234,7 +202,7 @@ func (m *Machine) applyWireguardPeerConfig(ring rings.RingID,
default:
// apply RingInfo
ri := &RingInfo{
Ring: MustWireguardInterfaceID(ring),
Ring: ring,
Enabled: true,
Keys: wireguard.KeyPair{
PublicKey: pc.PublicKey,
@@ -245,57 +213,27 @@ func (m *Machine) applyWireguardPeerConfig(ring rings.RingID,
}
}
func (m *Machine) applyZoneNodeID(regionID rings.RegionID,
zoneID rings.ZoneID, nodeID rings.NodeID) error {
//
func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
switch {
case !regionID.Valid():
return fmt.Errorf("invalid %s", "regionID")
case !zoneID.Valid():
case zoneID == 0:
return fmt.Errorf("invalid %s", "zoneID")
case !nodeID.Valid():
case nodeID == 0:
return fmt.Errorf("invalid %s", "nodeID")
case m.ID != nodeID:
return fmt.Errorf("invalid %s: %v ≠ %v", "nodeID", m.ID, nodeID)
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.ID, nodeID)
case m.zone.ID != 0 && m.zone.ID != zoneID:
return fmt.Errorf("invalid %s: %v ≠ %v", "zoneID", m.zone.ID, zoneID)
case m.Region() != regionID:
return fmt.Errorf("invalid %s: %v ≠ %v", "regionID", m.Region(), regionID)
default:
if m.zone.ID == 0 {
m.zone.ID = zoneID
}
return nil
case m.zone.ID == 0:
m.zone.ID = zoneID
}
}
func (m *Machine) setRingDefaults(ri *RingInfo) error {
if ri.Keys.PrivateKey.IsZero() {
m.info().
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
Print("generating key pair")
kp, err := wireguard.NewKeyPair()
if err != nil {
return err
}
ri.Keys = kp
}
return nil
}
// RemoveWireguardConfig deletes wgN.conf from the machine's
// config directory.
func (m *Machine) RemoveWireguardConfig(ringID rings.RingID) error {
ring, err := AsWireguardInterfaceID(ringID)
if err != nil {
return err
}
err = m.RemoveFile(ring.ConfFile())
func (m *Machine) RemoveWireguardConfig(ring int) error {
err := m.RemoveFile("wg%v.conf", ring)
if os.IsNotExist(err) {
err = nil
}
@@ -303,12 +241,7 @@ func (m *Machine) RemoveWireguardConfig(ringID rings.RingID) error {
return err
}
func (m *Machine) createRingInfo(ringID rings.RingID, enabled bool) (*RingInfo, error) {
ring, err := AsWireguardInterfaceID(ringID)
if err != nil {
return nil, err
}
func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) {
keys, err := wireguard.NewKeyPair()
if err != nil {
return nil, err
@@ -320,7 +253,7 @@ func (m *Machine) createRingInfo(ringID rings.RingID, enabled bool) (*RingInfo,
Keys: keys,
}
err = m.applyRingInfo(ringID, ri)
err = m.applyRingInfo(ring, ri)
if err != nil {
return nil, err
}
+7 -50
View File
@@ -3,14 +3,11 @@ package cluster
import (
"context"
"net/netip"
"os"
"strconv"
"strings"
"time"
"darvaza.org/core"
"git.jpi.io/amery/jpictl/pkg/rings"
)
// LookupNetIP uses the DNS Resolver to get the public addresses associated
@@ -39,8 +36,8 @@ func (m *Machine) init() error {
return core.Wrap(err, m.Name)
}
for _, ring := range Rings {
if err := m.tryReadWireguardKeys(ring.ID); err != nil {
for i := 0; i < RingsCount; i++ {
if err := m.tryReadWireguardKeys(i); err != nil {
return core.Wrap(err, m.Name)
}
}
@@ -67,57 +64,17 @@ func (m *Machine) setID() error {
return err
}
m.ID = rings.NodeID(id)
m.ID = int(id)
return nil
}
// scan is called once we know about all zones and machine names
func (m *Machine) scan(_ *ScanOptions) error {
for _, ring := range Rings {
if err := m.tryApplyWireguardConfig(ring.ID); err != nil {
func (m *Machine) scan(opts *ScanOptions) error {
for i := 0; i < RingsCount; i++ {
if err := m.tryApplyWireguardConfig(i); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", MustWireguardInterfaceID(ring.ID)).
Print()
return err
}
}
return m.loadInactive()
}
func (m *Machine) loadInactive() error {
data, err := m.ReadLines("region")
switch {
case os.IsNotExist(err):
// no file
return nil
case err != nil:
// read error
return err
default:
// look for "none"
for _, r := range data {
switch r {
case "none":
m.Inactive = true
default:
m.Inactive = false
}
}
return nil
}
}
// scanWrapUp is called once all machines have been scanned
func (m *Machine) scanWrapUp(opts *ScanOptions) error {
for _, ri := range m.Rings {
if err := m.setRingDefaults(ri); err != nil {
m.error(err).
WithField("subsystem", "wireguard").
WithField("node", m.Name).
WithField("ring", ri.Ring).
WithField("ring", i).
Print()
return err
}
+13 -347
View File
@@ -1,79 +1,21 @@
package cluster
import (
"bytes"
"path/filepath"
"git.jpi.io/amery/jpictl/pkg/rings"
)
var (
_ MachineIterator = (*Region)(nil)
_ ZoneIterator = (*Region)(nil)
_ RegionIterator = (*Zone)(nil)
_ RegionIterator = (*Cluster)(nil)
)
// A RegionIterator is a set of Regions we can iterate on
type RegionIterator interface {
ForEachRegion(func(*Region) bool)
}
// Region represents a group of zones geographically related
type Region struct {
m *Cluster
zones []*Zone
Name string
ID rings.RegionID `json:",omitempty" yaml:",omitempty"`
Cluster *string `json:",omitempty" yaml:",omitempty"`
Regions []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
// 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 {
if p.IsActive() {
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
}
// Zones ...
func (r *Region) Zones() []string {
out := make([]string, len(r.zones))
for i := range r.zones {
out[i] = r.zones[i].Name
}
return out
}
func (m *Cluster) initRegions(_ *ScanOptions) error {
@@ -81,7 +23,6 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// first regions defined by zones
m.ForEachZone(func(z *Zone) bool {
SortRegions(z.Regions)
for _, region := range z.Regions {
regions[region] = append(regions[region], z)
}
@@ -91,7 +32,7 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
// bind first level regions and their zones
for name, zones := range regions {
m.setRegionZones(name, zones...)
m.syncRegions(name, zones...)
}
// and combine zones to produce larger regions
@@ -100,16 +41,11 @@ func (m *Cluster) initRegions(_ *ScanOptions) error {
m.finishRegion(r)
}
m.sortRegions()
m.scanRegionID()
m.computeZonesRegion()
return nil
}
func (m *Cluster) setRegionZones(name string, zones ...*Zone) {
for i := range m.Regions {
r := &m.Regions[i]
func (m *Cluster) syncRegions(name string, zones ...*Zone) {
for _, r := range m.Regions {
if r.Name == name {
// found
r.m = m
@@ -126,80 +62,6 @@ 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]
if name == r.Name {
// found
r.Regions = append(r.Regions, subs...)
return
}
}
// new
m.Regions = append(m.Regions, Region{
Name: name,
Regions: subs,
})
}
// ForEachRegion calls a function on all regions this zone belongs to.
func (z *Zone) ForEachRegion(fn func(*Region) bool) {
if fn == nil {
return
}
z.zones.ForEachRegion(func(r *Region) bool {
var match bool
r.ForEachZone(func(z2 *Zone) bool {
match = (z == z2)
return match
})
if match && fn(r) {
return true
}
return false
})
}
func (z *Zone) appendRegions(regions ...string) error {
for _, s := range regions {
// TODO: validate
z.debug().
WithField("zone", z.Name).
WithField("region", s).
Print("attached")
z.Regions = append(z.Regions, s)
}
return nil
}
func (m *Cluster) finishRegion(r *Region) {
if r.m != nil {
// ready
@@ -209,7 +71,7 @@ func (m *Cluster) finishRegion(r *Region) {
r.m = m
sub := []string{}
for _, name := range r.Regions {
r2, ok := m.getFinishRegion(name)
r2, ok := m.getRegion(name)
if !ok {
m.warn(nil).WithField("region", name).Print("unknown region")
continue
@@ -221,211 +83,15 @@ func (m *Cluster) finishRegion(r *Region) {
r.Regions = sub
}
// revive:disable:cognitive-complexity
func (m *Cluster) scanRegionID() {
// revive:enable:cognitive-complexity
var max rings.RegionID
var missing bool
// check IDs
ids := make(map[rings.RegionID]bool)
fn := func(r *Region) bool {
var term bool
switch {
case !r.IsPrimary():
// secondary, no ID.
r.ID = 0
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)
}
}
func (m *Cluster) computeZonesRegion() {
fn := func(r *Region, z *Zone) {
if z.region != nil {
m.error(nil).
WithField("zone", z.Name).
WithField("region", []string{
z.region.Name,
r.Name,
}).
Print("zone in two regions")
} else {
z.region = r
}
}
m.ForEachRegion(func(r *Region) bool {
var term bool
if r.IsPrimary() {
r.ForEachZone(func(z *Zone) bool {
fn(r, z)
return term
})
}
return term
})
}
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
}
func (m *Cluster) getFinishRegion(name string) (*Region, bool) {
if r, ok := m.getRegion(name); ok {
m.finishRegion(r)
return r, true
}
return nil, false
}
// SyncRegions writes to the file system the regions this [Zone]
// belongs to.
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")
}
if err == nil {
err = p.RemoveFile(RegionClusterTokenFileName)
}
return err
}
func (z *Zone) syncZoneRegions() error {
name := filepath.Join(z.Name, ZoneRegionsFileName)
if len(z.Regions) > 0 {
var buf bytes.Buffer
for _, s := range z.Regions {
_, _ = buf.WriteString(s)
_, _ = buf.WriteRune('\n')
}
return z.zones.WriteStringFile(buf.String(), name)
}
return z.zones.RemoveFile(name)
}
// SyncRegions writes to the file system the regions covered
// by this meta-region
func (r *Region) SyncRegions() error {
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
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 {
_, _ = buf.WriteString(s)
_, _ = buf.WriteRune('\n')
}
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')
}
err = r.m.WriteStringFile(buf.String(), name)
}
return err
}
-41
View File
@@ -1,41 +0,0 @@
package cluster
import "sort"
// SortRegions sorts regions. first by length those 3-character
// or shorter, and then by length. It's mostly aimed at
// supporting ISO-3166 order
func SortRegions(regions []string) []string {
sort.Slice(regions, func(i, j int) bool {
r1, r2 := regions[i], regions[j]
return regionLess(r1, r2)
})
return regions
}
func regionLess(r1, r2 string) bool {
switch {
case len(r1) < 4:
switch {
case len(r1) < len(r2):
return true
case len(r1) > len(r2):
return false
default:
return r1 < r2
}
case len(r2) < 4:
return false
default:
return r1 < r2
}
}
func (m *Cluster) sortRegions() {
sort.Slice(m.Regions, func(i, j int) bool {
r1 := m.Regions[i].Name
r2 := m.Regions[j].Name
return regionLess(r1, r2)
})
}
+110 -117
View File
@@ -3,88 +3,32 @@ package cluster
import (
"fmt"
"io/fs"
"log"
"net/netip"
"strconv"
"git.jpi.io/amery/jpictl/pkg/rings"
"git.jpi.io/amery/jpictl/pkg/wireguard"
)
const (
// MaxZoneID indicates the highest ID allowed for a Zone
MaxZoneID = 0xf
// MaxNodeID indicates the highest Machine ID allowed within a Zone
MaxNodeID = 0xff - 1
// RingsCount indicates how many wireguard rings we have
RingsCount = 2
// RingZeroPort is the port wireguard uses for ring0
RingZeroPort = 51800
// RingOnePort is the port wireguard uses for ring1
RingOnePort = 51810
)
// WireguardInterfaceID represents the number in the `wg%v`
// interface name.
type WireguardInterfaceID uint
// AsWireguardInterfaceID returns the [WireguardInterfaceID] for
// a valid [rings.RingID].
func AsWireguardInterfaceID(ring rings.RingID) (WireguardInterfaceID, error) {
switch ring {
case rings.RingZeroID:
return 0, nil
case rings.RingOneID:
return 1, nil
default:
return 0, ErrInvalidRing(ring)
}
}
// MustWireguardInterfaceID returns the [WireguardInterfaceID] for
// a valid [rings.RingID], and panics if it's not.
func MustWireguardInterfaceID(ring rings.RingID) WireguardInterfaceID {
id, err := AsWireguardInterfaceID(ring)
if err != nil {
panic(err)
}
return id
}
// RingID tells the [rings.RingID] of the [WireguardInterfaceID].
func (wi WireguardInterfaceID) RingID() rings.RingID {
return rings.RingID(wi + 1)
}
// PubFile returns "wgN.pub"
func (wi WireguardInterfaceID) PubFile() string {
return fmt.Sprintf("wg%v.pub", wi)
}
// KeyFile returns "wgN.key"
func (wi WireguardInterfaceID) KeyFile() string {
return fmt.Sprintf("wg%v.key", wi)
}
// ConfFile returns "wgN.conf"
func (wi WireguardInterfaceID) ConfFile() string {
return fmt.Sprintf("wg%v.conf", wi)
}
// Files returns all wgN.ext file names.
func (wi WireguardInterfaceID) Files() (keyFile, pubFile, confFile string) {
prefix := "wg" + strconv.Itoa(int(wi))
return prefix + ".key", prefix + ".pub", prefix + ".conf"
}
// RingInfo contains represents the Wireguard endpoint details
// for a Machine on a particular ring
type RingInfo struct {
Ring WireguardInterfaceID
Ring int
Enabled bool
Keys wireguard.KeyPair
}
// RingID returns the [rings.RingID] for this [RingInfo].
func (ri *RingInfo) RingID() rings.RingID {
return rings.RingID(ri.Ring + 1)
}
// Merge attempts to combine two RingInfo structs
func (ri *RingInfo) Merge(alter *RingInfo) error {
switch {
@@ -97,7 +41,7 @@ func (ri *RingInfo) Merge(alter *RingInfo) error {
// can't disable via Merge
return fmt.Errorf("invalid %s: %v → %v", "enabled", ri.Enabled, alter.Enabled)
case !canMergeKeyPairs(ri.Keys, alter.Keys):
// incompatible key pairs
// incompatible keypairs
return fmt.Errorf("invalid %s: %s ≠ %s", "keys", ri.Keys, alter.Keys)
}
@@ -110,7 +54,7 @@ func (ri *RingInfo) unsafeMerge(alter *RingInfo) error {
ri.Enabled = true
}
// fill the gaps on our key pair
// fill the gaps on our keypair
if ri.Keys.PrivateKey.IsZero() {
ri.Keys.PrivateKey = alter.Keys.PrivateKey
}
@@ -135,34 +79,108 @@ func canMergeKeyPairs(p1, p2 wireguard.KeyPair) bool {
// RingAddressEncoder provides encoder/decoder access for a particular
// Wireguard ring
type RingAddressEncoder struct {
ID rings.RingID
ID int
Port uint16
Encode func(rings.RegionID, rings.ZoneID, rings.NodeID) (netip.Addr, error)
Decode func(addr netip.Addr) (rings.RegionID, rings.ZoneID, rings.NodeID, bool)
Encode func(zoneID, nodeID int) (netip.Addr, bool)
Decode func(addr netip.Addr) (zoneID, nodeID int, ok bool)
}
var (
// RingZero is a wg0 address encoder/decoder
RingZero = RingAddressEncoder{
ID: rings.RingZeroID,
ID: 0,
Port: RingZeroPort,
Decode: rings.DecodeRingZeroAddress,
Encode: rings.RingZeroAddress,
Decode: ParseRingZeroAddress,
Encode: RingZeroAddress,
}
// RingOne is a wg1 address encoder/decoder
RingOne = RingAddressEncoder{
ID: rings.RingOneID,
ID: 1,
Port: RingOnePort,
Decode: rings.DecodeRingOneAddress,
Encode: rings.RingOneAddress,
Decode: ParseRingOneAddress,
Encode: RingOneAddress,
}
// Rings provides indexed access to the ring address encoders
Rings = []RingAddressEncoder{
Rings = [RingsCount]RingAddressEncoder{
RingZero,
RingOne,
}
)
// ValidZoneID checks if the given zoneID is a valid 4 bit zone number.
//
// 0 is reserved, and only allowed when composing CIDRs.
func ValidZoneID(zoneID int) bool {
switch {
case zoneID < 0 || zoneID > MaxZoneID:
return false
default:
return true
}
}
// ValidNodeID checks if the given nodeID is a valid 8 bit number.
// nodeID is unique within a Zone.
// 0 is reserved, and only allowed when composing CIDRs.
func ValidNodeID(nodeID int) bool {
switch {
case nodeID < 0 || nodeID > MaxNodeID:
return false
default:
return true
}
}
// ParseRingZeroAddress extracts zone and node ID from a wg0 [netip.Addr]
// wg0 addresses are of the form `10.0.{{zoneID}}.{{nodeID}}`
func ParseRingZeroAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[1] == 0 {
return int(a4[2]), int(a4[3]), true
}
}
return 0, 0, false
}
// RingZeroAddress returns a wg0 IP address
func RingZeroAddress(zoneID, nodeID int) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, 0, uint8(zoneID), uint8(nodeID)}
return netip.AddrFrom4(a4), true
}
}
// ParseRingOneAddress extracts zone and node ID from a wg1 [netip.Addr]
// wg1 addresses are of the form `10.{{zoneID << 4}}.{{nodeID}}`
func ParseRingOneAddress(addr netip.Addr) (zoneID int, nodeID int, ok bool) {
if addr.IsValid() {
a4 := addr.As4()
if a4[0] == 10 && a4[2] == 0 {
zoneID = int(a4[1] >> 4)
nodeID = int(a4[3])
return zoneID, nodeID, true
}
}
return 0, 0, false
}
// RingOneAddress returns a wg1 IP address
func RingOneAddress(zoneID, nodeID int) (netip.Addr, bool) {
switch {
case !ValidZoneID(zoneID) || !ValidNodeID(nodeID):
return netip.Addr{}, false
default:
a4 := [4]uint8{10, uint8(zoneID << 4), 0, uint8(nodeID)}
return netip.AddrFrom4(a4), true
}
}
var (
_ MachineIterator = (*Ring)(nil)
_ ZoneIterator = (*Ring)(nil)
@@ -185,15 +203,14 @@ func (r *Ring) AddPeer(p *Machine) bool {
nodeID := p.ID
zoneID := p.Zone()
regionID := p.Region()
addr, _ := r.Encode(regionID, zoneID, nodeID)
addr, _ := r.Encode(zoneID, nodeID)
rp := &RingPeer{
Node: p,
Address: addr,
PrivateKey: ri.Keys.PrivateKey,
PeerConfig: wireguard.PeerConfig{
Name: fmt.Sprintf("%s-%v", p.Name, ri.Ring),
Name: fmt.Sprintf("%s-%v", p.Name, r.ID),
PublicKey: ri.Keys.PublicKey,
Endpoint: wireguard.EndpointAddress{
Host: p.FullName(),
@@ -203,7 +220,7 @@ func (r *Ring) AddPeer(p *Machine) bool {
}
switch {
case r.ID == rings.RingZeroID:
case r.ID == 0:
r.setRingZeroAllowedIPs(rp)
case p.IsGateway():
r.setRingOneGatewayAllowedIPs(rp)
@@ -216,52 +233,42 @@ func (r *Ring) AddPeer(p *Machine) bool {
}
func (r *Ring) setRingZeroAllowedIPs(rp *RingPeer) {
regionID, zoneID, _, _ := r.Decode(rp.Address)
zoneID, _, _ := r.Decode(rp.Address)
// everyone on ring0 is a gateway to ring1
subnet, _ := rings.RingOnePrefix(regionID, zoneID)
rp.AllowSubnet(subnet)
addr, _ := RingOneAddress(zoneID, 0)
rp.AllowCIDR(addr, 12)
// peer
rp.AllowCIDR(rp.Address, 32)
}
func (r *Ring) setRingOneGatewayAllowedIPs(rp *RingPeer) {
regionID, zoneID, _, _ := r.Decode(rp.Address)
zoneID, _, _ := r.Decode(rp.Address)
// peer
rp.AllowCIDR(rp.Address, 32)
log.Println(rp.Node.Name, "0:", rp.Address, regionID, zoneID)
// ring1 gateways connect to all other ring1 networks
r.ForEachZone(func(z *Zone) bool {
log.Println(rp.Node.Name, "1:", z.Name, z.RegionID(), z.ID)
if !z.Is(regionID, zoneID) {
subnet := z.RingOnePrefix()
rp.AllowSubnet(subnet)
log.Println(rp.Node.Name, "1.1:", rp.PeerConfig.AllowedIPs)
if z.ID != zoneID {
addr, _ := r.Encode(z.ID, 0)
rp.AllowCIDR(addr, 12)
}
return false
})
// ring1 gateways also connect to all ring0 addresses
r.ForEachZone(func(z *Zone) bool {
log.Println(rp.Node.Name, "2:", z.Name, z.RegionID(), z.ID)
z.ForEachMachine(func(p *Machine) bool {
log.Println(rp.Node.Name, "2.1:", p.Name, p.IsGateway())
if p.IsGateway() {
addr, _ := p.RingZeroAddress()
addr, _ := RingZeroAddress(z.ID, p.ID)
rp.AllowCIDR(addr, 32)
log.Println(rp.Node.Name, "2.2:", rp.PeerConfig.AllowedIPs)
}
return false
})
return false
})
log.Println(rp.Node.Name, "3:", rp.PeerConfig.AllowedIPs)
}
func (*Ring) setRingOneNodeAllowedIPs(rp *RingPeer) {
@@ -322,29 +329,15 @@ type RingPeer struct {
// AllowCIDR allows an IP range via this peer
func (rp *RingPeer) AllowCIDR(addr netip.Addr, bits int) {
rp.AllowSubnet(netip.PrefixFrom(addr, bits))
}
// AllowSubnet allows an IP range via this peer
func (rp *RingPeer) AllowSubnet(subnet netip.Prefix) {
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs, subnet)
cidr := netip.PrefixFrom(addr, bits)
rp.PeerConfig.AllowedIPs = append(rp.PeerConfig.AllowedIPs, cidr)
}
// NewRing composes a new Ring for Wireguard setup
func NewRing(z ZoneIterator, m MachineIterator, ringID rings.RingID) (*Ring, error) {
var r *Ring
for _, ring := range Rings {
if ringID == ring.ID {
r = &Ring{
RingAddressEncoder: ring,
ZoneIterator: z,
}
break
}
}
if r == nil {
return nil, ErrInvalidRing(ringID)
func NewRing(z ZoneIterator, m MachineIterator, ring int) (*Ring, error) {
r := &Ring{
RingAddressEncoder: Rings[ring],
ZoneIterator: z,
}
m.ForEachMachine(func(p *Machine) bool {
+3 -40
View File
@@ -3,10 +3,8 @@ package cluster
// SyncAll updates all config files
func (m *Cluster) SyncAll() error {
for _, fn := range []func() error{
m.SyncMkdirAll,
m.SyncAllWireguard,
m.SyncAllCeph,
m.SyncAllRegions,
m.WriteHosts,
} {
if err := fn(); err != nil {
@@ -17,31 +15,17 @@ func (m *Cluster) SyncAll() error {
return nil
}
// SyncMkdirAll creates the directories needed to store files
// required to represent the cluster.
func (m *Cluster) SyncMkdirAll() error {
err := m.MkdirAll(".")
if err == nil {
m.ForEachMachine(func(p *Machine) bool {
err = p.MkdirAll(".")
return err != nil
})
}
return err
}
// SyncAllWireguard updates all wireguard config files
func (m *Cluster) SyncAllWireguard() error {
var err error
for _, ring := range Rings {
err = m.WriteWireguardKeys(ring.ID)
for ring := 0; ring < RingsCount; ring++ {
err = m.WriteWireguardKeys(ring)
if err != nil {
return err
}
err = m.SyncWireguardConfig(ring.ID)
err = m.SyncWireguardConfig(ring)
if err != nil {
return err
}
@@ -59,24 +43,3 @@ func (m *Cluster) SyncAllCeph() error {
return m.WriteCephConfig(cfg)
}
// SyncAllRegions rewrites all region data
func (m *Cluster) SyncAllRegions() error {
var err error
m.ForEachZone(func(z *Zone) bool {
err := z.SyncRegions()
return err != nil
})
if err != nil {
return err
}
m.ForEachRegion(func(r *Region) bool {
err = r.SyncRegions()
return err != nil
})
return err
}
+36 -44
View File
@@ -3,8 +3,6 @@ package cluster
import (
"io/fs"
"os"
"git.jpi.io/amery/jpictl/pkg/rings"
)
var (
@@ -28,22 +26,22 @@ var (
// A WireguardConfigPruner deletes wgN.conf on all machines under
// its scope with the specified ring disabled
type WireguardConfigPruner interface {
PruneWireguardConfig(ring rings.RingID) error
PruneWireguardConfig(ring int) error
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled on all zones
func (m *Cluster) PruneWireguardConfig(ring rings.RingID) error {
func (m *Cluster) PruneWireguardConfig(ring int) error {
return pruneWireguardConfig(m, ring)
}
// PruneWireguardConfig removes wgN.conf files of machines with
// the corresponding ring disabled.
func (z *Zone) PruneWireguardConfig(ring rings.RingID) error {
func (z *Zone) PruneWireguardConfig(ring int) error {
return pruneWireguardConfig(z, ring)
}
func pruneWireguardConfig(m MachineIterator, ring rings.RingID) error {
func pruneWireguardConfig(m MachineIterator, ring int) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
@@ -61,7 +59,7 @@ func pruneWireguardConfig(m MachineIterator, ring rings.RingID) error {
// PruneWireguardConfig deletes the wgN.conf file if its
// presence on the ring is disabled
func (m *Machine) PruneWireguardConfig(ring rings.RingID) error {
func (m *Machine) PruneWireguardConfig(ring int) error {
_, ok := m.getRingInfo(ring)
if !ok {
return m.RemoveWireguardConfig(ring)
@@ -73,16 +71,16 @@ func (m *Machine) PruneWireguardConfig(ring rings.RingID) error {
// A WireguardConfigWriter rewrites all wgN.conf on all machines under
// its scope attached to that ring
type WireguardConfigWriter interface {
WriteWireguardConfig(ring rings.RingID) error
WriteWireguardConfig(ring int) error
}
// WriteWireguardConfig rewrites all wgN.conf on all machines
// attached to that ring
func (m *Cluster) WriteWireguardConfig(ring rings.RingID) error {
func (m *Cluster) WriteWireguardConfig(ring int) error {
switch ring {
case rings.RingZeroID:
case 0:
return writeWireguardConfig(m, m, ring)
case rings.RingOneID:
case 1:
var err error
m.ForEachZone(func(z *Zone) bool {
err = writeWireguardConfig(m, z, ring)
@@ -90,24 +88,24 @@ func (m *Cluster) WriteWireguardConfig(ring rings.RingID) error {
})
return err
default:
return ErrInvalidRing(ring)
return fs.ErrInvalid
}
}
// WriteWireguardConfig rewrites all wgN.conf on all machines
// on the Zone attached to that ring
func (z *Zone) WriteWireguardConfig(ring rings.RingID) error {
func (z *Zone) WriteWireguardConfig(ring int) error {
switch ring {
case rings.RingZeroID:
case 0:
return writeWireguardConfig(z.zones, z.zones, ring)
case rings.RingOneID:
case 1:
return writeWireguardConfig(z.zones, z, ring)
default:
return ErrInvalidRing(ring)
return fs.ErrInvalid
}
}
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID) error {
func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
r, err := NewRing(z, m, ring)
if err != nil {
return err
@@ -123,7 +121,7 @@ func writeWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID)
// WriteWireguardConfig rewrites the wgN.conf file of this Machine
// if enabled
func (m *Machine) WriteWireguardConfig(ring rings.RingID) error {
func (m *Machine) WriteWireguardConfig(ring int) error {
r, err := NewRing(m.zone.zones, m.zone, ring)
if err != nil {
return err
@@ -133,17 +131,12 @@ func (m *Machine) WriteWireguardConfig(ring rings.RingID) error {
}
func (m *Machine) writeWireguardRingConfig(r *Ring) error {
ring, err := AsWireguardInterfaceID(r.ID)
if err != nil {
return err
}
wg, err := r.ExportConfig(m)
if err != nil {
return nil
}
f, err := m.CreateTruncFile(ring.ConfFile())
f, err := m.CreateTruncFile("wg%v.conf", r.ID)
if err != nil {
return err
}
@@ -156,16 +149,16 @@ func (m *Machine) writeWireguardRingConfig(r *Ring) error {
// A WireguardConfigSyncer updates all wgN.conf on all machines under
// its scope reflecting the state of the ring
type WireguardConfigSyncer interface {
SyncWireguardConfig(ring rings.RingID) error
SyncWireguardConfig(ring int) error
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (m *Cluster) SyncWireguardConfig(ring rings.RingID) error {
func (m *Cluster) SyncWireguardConfig(ring int) error {
switch ring {
case rings.RingZeroID:
case 0:
return syncWireguardConfig(m, m, ring)
case rings.RingOneID:
case 1:
var err error
m.ForEachZone(func(z *Zone) bool {
err = syncWireguardConfig(m, z, ring)
@@ -173,24 +166,24 @@ func (m *Cluster) SyncWireguardConfig(ring rings.RingID) error {
})
return err
default:
return ErrInvalidRing(ring)
return fs.ErrInvalid
}
}
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (z *Zone) SyncWireguardConfig(ring rings.RingID) error {
func (z *Zone) SyncWireguardConfig(ring int) error {
switch ring {
case rings.RingZeroID:
case 0:
return syncWireguardConfig(z.zones, z.zones, ring)
case rings.RingOneID:
case 1:
return syncWireguardConfig(z.zones, z, ring)
default:
return ErrInvalidRing(ring)
return fs.ErrInvalid
}
}
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID) error {
func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring int) error {
r, err := NewRing(z, m, ring)
if err != nil {
return err
@@ -210,27 +203,27 @@ func syncWireguardConfig(z ZoneIterator, m MachineIterator, ring rings.RingID) e
// SyncWireguardConfig updates all wgN.conf files for the specified
// ring
func (m *Machine) SyncWireguardConfig(ring rings.RingID) error {
func (m *Machine) SyncWireguardConfig(ring int) error {
return m.zone.SyncWireguardConfig(ring)
}
// A WireguardKeysWriter writes the Wireguard Keys for all machines
// under its scope for the specified ring
type WireguardKeysWriter interface {
WriteWireguardKeys(ring rings.RingID) error
WriteWireguardKeys(ring int) error
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files
func (m *Cluster) WriteWireguardKeys(ring rings.RingID) error {
func (m *Cluster) WriteWireguardKeys(ring int) error {
return writeWireguardKeys(m, ring)
}
// WriteWireguardKeys rewrites all wgN.{key,pub} files on this zone
func (z *Zone) WriteWireguardKeys(ring rings.RingID) error {
func (z *Zone) WriteWireguardKeys(ring int) error {
return writeWireguardKeys(z, ring)
}
func writeWireguardKeys(m MachineIterator, ring rings.RingID) error {
func writeWireguardKeys(m MachineIterator, ring int) error {
var err error
m.ForEachMachine(func(p *Machine) bool {
@@ -247,12 +240,12 @@ func writeWireguardKeys(m MachineIterator, ring rings.RingID) error {
}
// WriteWireguardKeys writes the wgN.key/wgN.pub files
func (m *Machine) WriteWireguardKeys(ringID rings.RingID) error {
func (m *Machine) WriteWireguardKeys(ring int) error {
var err error
var key, pub string
var ri *RingInfo
ri, _ = m.getRingInfo(ringID)
ri, _ = m.getRingInfo(ring)
if ri != nil {
key = ri.Keys.PrivateKey.String()
pub = ri.Keys.PublicKey.String()
@@ -265,13 +258,12 @@ func (m *Machine) WriteWireguardKeys(ringID rings.RingID) error {
pub = ri.Keys.PrivateKey.Public().String()
}
keyFile, pubFile, _ := ri.Ring.Files()
err = m.WriteStringFile(key+"\n", keyFile)
err = m.WriteStringFile(key+"\n", "wg%v.key", ring)
if err != nil {
return err
}
err = m.WriteStringFile(pub+"\n", pubFile)
err = m.WriteStringFile(pub+"\n", "wg%v.pub", ring)
if err != nil {
return err
}
+4 -42
View File
@@ -2,8 +2,6 @@ package cluster
import (
"io/fs"
"git.jpi.io/amery/jpictl/pkg/rings"
)
var (
@@ -19,10 +17,9 @@ type ZoneIterator interface {
// affinity.
type Zone struct {
zones *Cluster
region *Region
logger `json:"-" yaml:"-"`
ID rings.ZoneID
ID int
Name string
Regions []string `json:",omitempty" yaml:",omitempty"`
@@ -34,7 +31,7 @@ func (z *Zone) String() string {
}
// SetGateway configures a machine to be the zone's ring0 gateway
func (z *Zone) SetGateway(gatewayID rings.NodeID, enabled bool) error {
func (z *Zone) SetGateway(gatewayID int, enabled bool) error {
var err error
var found bool
@@ -59,8 +56,8 @@ func (z *Zone) SetGateway(gatewayID rings.NodeID, enabled bool) error {
}
// GatewayIDs returns the list of IDs of machines that act as ring0 gateways
func (z *Zone) GatewayIDs() ([]rings.NodeID, int) {
var out []rings.NodeID
func (z *Zone) GatewayIDs() ([]int, int) {
var out []int
z.ForEachMachine(func(p *Machine) bool {
if p.IsGateway() {
out = append(out, p.ID)
@@ -70,38 +67,3 @@ func (z *Zone) GatewayIDs() ([]rings.NodeID, int) {
return out, len(out)
}
// RegionID returns the primary [Region] of a [Zone].
func (z *Zone) RegionID() rings.RegionID {
if z != nil && z.region != nil {
return z.region.ID
}
return 0
}
// Is checks if the given [rings.RegionID] and [rings.ZoneID] match
// the [Zone].
func (z *Zone) Is(regionID rings.RegionID, zoneID rings.ZoneID) bool {
switch {
case z.ID != zoneID:
return false
case z.RegionID() != regionID:
return false
default:
return true
}
}
// Eq checks if two [Zone]s are the same.
func (z *Zone) Eq(z2 *Zone) bool {
switch {
case z == nil, z2 == nil:
return false
case z.ID != z2.ID:
return false
case z.RegionID() != z2.RegionID():
return false
default:
return true
}
}
-69
View File
@@ -1,69 +0,0 @@
package dns
import (
"context"
"net/netip"
"os"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// Add adds a machine to the DNS records
func (mgr *Manager) Add(ctx context.Context, name string, addrs ...netip.Addr) error {
// TODO: validate name
cur, err := mgr.GetRecords(ctx, name)
if err != nil {
return core.Wrap(err, "GetRecords")
}
// merge []SyncAddr for name
s := mgr.asSyncRecordsMap(cur)[name+mgr.suffix]
for _, addr := range addrs {
s = AppendSyncAddr(s, addr)
}
return mgr.addSyncAddr(ctx, name, s)
}
func (mgr *Manager) addSyncAddr(ctx context.Context, name string, s []SyncAddr) error {
var recs []libdns.Record
for _, a := range s {
recs = append(recs, libdns.Record{
ID: a.ID,
Name: name + mgr.suffix,
Type: core.IIf(a.Addr.Is6(), "AAAA", "A"),
TTL: time.Second,
Value: a.Addr.String(),
})
}
SortRecords(recs)
err := writeRecords(recs, os.Stdout)
if err != nil {
return err
}
_, err = mgr.p.SetRecords(ctx, mgr.domain, recs)
return err
}
// AppendSyncAddr appends a [netip.Addr] to a [SyncAddr] slice
// if the address is new.
func AppendSyncAddr(s []SyncAddr, addr netip.Addr) []SyncAddr {
for _, se := range s {
if se.Addr.Compare(addr) == 0 {
// found
return s
}
}
s = append(s, SyncAddr{
Addr: addr,
TTL: time.Second,
})
return s
}
+21 -25
View File
@@ -1,38 +1,34 @@
// Package dns manages DNS entries for the cluster
package dns
import (
"fmt"
"net/netip"
)
import "net/netip"
// Zone represents a set of hosts with high affinity
// // A Config defines a Region
//
// type Config struct {
// // Name is the identifier of this Region
// Name string
// // Regions are a list of (sub)regions that belong to this Region
// Regions []string
// // Zones are a list of Zones that directly belong to this Region
// Zones []string
// }
//
// type Region struct {
// Name string
// }
// Zone represents a set of machines with high affinity
type Zone struct {
Name string
Hosts map[int]*Host
Machines map[int]*Machine
}
func (z *Zone) String() string {
if z == nil {
return "undetermined"
}
return z.Name
}
// Machine represents a member of the cluster
type Machine struct {
ID int
// Host represents a member of the cluster
type Host struct {
zone *Zone
ID int
Active bool
Addrs []netip.Addr
}
func (p *Host) String() string {
if p == nil {
return "undetermined"
}
return fmt.Sprintf("%s-%v", p.zone, p.ID)
}
-12
View File
@@ -1,12 +0,0 @@
package dns
import "errors"
var (
// ErrNoDNSProvider indicates a [libdns.Provider] wasn't assigned
// to the [Manager]
ErrNoDNSProvider = errors.New("dns provider not specified")
// ErrNoDomain indicates a domain wasn't specified
ErrNoDomain = errors.New("domain not specified")
)
+41
View File
@@ -0,0 +1,41 @@
package dns
import (
"context"
"io/fs"
"net/netip"
)
// AddHost registers a machine
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
active bool, addrs ...netip.Addr) error {
//
if zone == "" || id <= 0 {
return fs.ErrInvalid
}
z, ok := mgr.zones[zone]
if !ok {
z = &Zone{
Name: zone,
Machines: make(map[int]*Machine),
}
mgr.zones[zone] = z
}
z.Machines[id] = &Machine{
ID: id,
Active: active,
Addrs: SortAddrSlice(addrs),
}
return nil
}
// AddRegion specifies a new region and the zones it contains
func (mgr *Manager) AddRegion(_ context.Context, region string, zones ...string) error {
mgr.l.Debug().WithField("region", region).WithField("zones", zones).Print()
mgr.regions[region] = append(mgr.regions[region], zones...)
return nil
}
+14 -120
View File
@@ -1,21 +1,20 @@
package dns
import (
"context"
"io/fs"
"net/netip"
"errors"
"strings"
"sync"
"darvaza.org/core"
"darvaza.org/slog"
"github.com/libdns/libdns"
"golang.org/x/net/publicsuffix"
"git.jpi.io/amery/jpictl/pkg/cluster"
"golang.org/x/net/publicsuffix"
)
// Manager is a DNS Manager instance
type Manager struct {
mu sync.Mutex
domain string
suffix string
zones map[string]*Zone
@@ -30,7 +29,10 @@ type ManagerOption func(*Manager) error
func newErrorManagerOption(err error, hint string) ManagerOption {
return func(*Manager) error {
return core.Wrap(err, hint)
if hint != "" {
err = core.Wrap(err, hint)
}
return err
}
}
@@ -72,9 +74,12 @@ func (mgr *Manager) setDefaults() error {
}
if mgr.domain == "" || mgr.suffix == "" {
return ErrNoDomain
return errors.New("domain not specified")
}
mgr.zones = make(map[string]*Zone)
mgr.regions = make(map[string][]string)
for _, opt := range opts {
if err := opt(mgr); err != nil {
return err
@@ -104,10 +109,7 @@ func WithDomain(domain string) ManagerOption {
// NewManager creates a DNS manager with the
func NewManager(opts ...ManagerOption) (*Manager, error) {
mgr := &Manager{
zones: make(map[string]*Zone),
regions: make(map[string][]string),
}
mgr := new(Manager)
for _, opt := range opts {
if err := opt(mgr); err != nil {
@@ -120,111 +122,3 @@ func NewManager(opts ...ManagerOption) (*Manager, error) {
}
return mgr, nil
}
// GetRecords pulls all the address records on DNS for our domain,
// optionally only those matching the given names.
func (mgr *Manager) GetRecords(ctx context.Context, names ...string) ([]libdns.Record, error) {
if mgr.p == nil {
return nil, ErrNoDNSProvider
}
recs, err := mgr.p.GetRecords(ctx, mgr.domain)
switch {
case err != nil:
// failed
return nil, err
case len(recs) == 0:
// empty
return []libdns.Record{}, nil
case mgr.suffix == "" && len(names) == 0:
// unfiltered
return recs, nil
default:
// filtered
recs = mgr.filterRecords(recs, names...)
return recs, nil
}
}
func (mgr *Manager) filterRecords(recs []libdns.Record, names ...string) []libdns.Record {
out := make([]libdns.Record, 0, len(recs))
for _, rr := range recs {
name, ok := mgr.matchSuffix(rr)
switch {
case !ok:
// skip, wrong subdomain
continue
case len(names) == 0:
// unfiltered, take it
case !core.SliceContains(names, name):
// skip, not one of the requested names
continue
}
out = append(out, rr)
}
return out
}
func (mgr *Manager) matchSuffix(rr libdns.Record) (string, bool) {
if mgr.suffix == "" {
// no suffix
return rr.Name, true
}
// remove suffix
return strings.CutSuffix(rr.Name, mgr.suffix)
}
// AddHost registers a host
func (mgr *Manager) AddHost(_ context.Context, zone string, id int,
active bool, addrs ...netip.Addr) error {
//
if zone == "" || id <= 0 {
return fs.ErrInvalid
}
z, ok := mgr.zones[zone]
if !ok {
z = &Zone{
Name: zone,
Hosts: make(map[int]*Host),
}
mgr.zones[zone] = z
}
p := &Host{
zone: z,
ID: id,
Active: active,
Addrs: SortAddrSlice(addrs),
}
z.Hosts[id] = p
if log, ok := mgr.l.Debug().WithEnabled(); ok {
log.WithField("subsystem", "dns").
WithField("zone", zone).
WithField("host", p.String()).
WithField("active", active).
Print()
}
return nil
}
// AddRegion specifies a new region and the zones it contains
func (mgr *Manager) AddRegion(_ context.Context, region string, zones ...string) error {
mgr.regions[region] = append(mgr.regions[region], zones...)
if log, ok := mgr.l.Debug().WithEnabled(); ok {
for _, zoneName := range zones {
log.WithField("subsystem", "dns").
WithField("region", region).
WithField("zone", zoneName).Print()
}
}
return nil
}
-2
View File
@@ -18,8 +18,6 @@ const (
type Provider interface {
libdns.RecordGetter
libdns.RecordDeleter
libdns.RecordSetter
libdns.RecordAppender
}
// DefaultDNSProvider returns a cloudflare DNS provider
+16 -142
View File
@@ -1,25 +1,16 @@
package dns
import (
"bytes"
"fmt"
"io"
"net/netip"
"sort"
"strings"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
"git.jpi.io/amery/jpictl/pkg/cluster"
)
func (mgr *Manager) fqdn(name string) string {
return fmt.Sprintf("%s.%s.", name, mgr.domain)
}
// SortAddrSlice sorts a slice of [netip.Addr]
// SortAddrSlice ...
func SortAddrSlice(s []netip.Addr) []netip.Addr {
sort.Slice(s, func(i, j int) bool {
return s[i].Less(s[j])
@@ -27,67 +18,6 @@ func SortAddrSlice(s []netip.Addr) []netip.Addr {
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
}
// SortRecords sorts a slice of [libdns.Record], by Name, Type and Value
func SortRecords(s []libdns.Record) []libdns.Record {
sort.Slice(s, func(i, j int) bool {
return lessRecord(s[i], s[j])
})
return s
}
func lessRecord(a, b libdns.Record) bool {
aName := strings.ToLower(a.Name)
bName := strings.ToLower(b.Name)
switch {
case aName < bName:
return true
case aName > bName:
return false
}
aType := strings.ToUpper(a.Type)
bType := strings.ToUpper(b.Type)
switch {
case aType < bType:
return true
case aType > bType:
return false
case aType == "A", aType == "AAAA":
// IP Addresses
var aa, ba netip.Addr
switch {
case aa.UnmarshalText([]byte(a.Value)) != nil:
// bad address on a
return true
case ba.UnmarshalText([]byte(b.Value)) != nil:
// bad address on b
return false
default:
return aa.Less(ba)
}
default:
// text
return a.Value < b.Value
}
}
// AddrRecord represents an A or AAAA record
type AddrRecord struct {
Name string
@@ -114,53 +44,6 @@ func (rr *AddrRecord) Export() []libdns.Record {
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()
}
func (mgr *Manager) genRegionsSorted() []string {
regions := make([]string, 0, len(mgr.regions))
for name := range mgr.regions {
regions = append(regions, name)
}
return cluster.SortRegions(regions)
}
func (mgr *Manager) genZonesSorted() []string {
zones := make([]string, 0, len(mgr.zones))
for name := range mgr.zones {
zones = append(zones, name)
}
sort.Strings(zones)
return zones
}
func (mgr *Manager) genAllAddrRecords() []AddrRecord {
var out []AddrRecord
@@ -168,8 +51,8 @@ func (mgr *Manager) genAllAddrRecords() []AddrRecord {
// zones
for _, z := range mgr.zones {
// hosts
s := mgr.genZoneHostRecords(z)
// machines
s := mgr.genZoneMachineRecords(z)
out = append(out, s...)
// zone alias
@@ -185,10 +68,11 @@ func (mgr *Manager) genAllAddrRecords() []AddrRecord {
cache[name] = addrs
}
for _, name := range mgr.genRegionsSorted() {
// regions
for name, zones := range mgr.regions {
var addrs []netip.Addr
for _, z := range mgr.regions[name] {
for _, z := range zones {
addrs = append(addrs, cache[z]...)
}
@@ -201,14 +85,18 @@ func (mgr *Manager) genAllAddrRecords() []AddrRecord {
out = append(out, rec)
}
SortAddrRecords(out)
// sort
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
return out
}
func (*Manager) genZoneAddresses(z *Zone) []netip.Addr {
var out []netip.Addr
for _, p := range z.Hosts {
for _, p := range z.Machines {
if p.Active {
out = append(out, p.Addrs...)
}
@@ -218,30 +106,16 @@ func (*Manager) genZoneAddresses(z *Zone) []netip.Addr {
return out
}
func (mgr *Manager) genZoneHostRecords(z *Zone) []AddrRecord {
out := make([]AddrRecord, 0, len(z.Hosts))
func (mgr *Manager) genZoneMachineRecords(z *Zone) []AddrRecord {
out := make([]AddrRecord, 0, len(z.Machines))
for _, p := range z.Hosts {
for _, p := range z.Machines {
rec := AddrRecord{
Name: p.String() + mgr.suffix,
Name: fmt.Sprintf("%s-%v%s", z.Name, p.ID, mgr.suffix),
Addr: p.Addrs,
}
out = append(out, rec)
}
SortAddrRecords(out)
return out
}
func (mgr *Manager) genRegionAddressesCached(name string,
zones map[string][]netip.Addr) []netip.Addr {
//
var addrs []netip.Addr
for _, zoneName := range mgr.regions[name] {
addrs = append(addrs, zones[zoneName]...)
}
SortAddrSlice(addrs)
return addrs
}
-58
View File
@@ -1,58 +0,0 @@
package dns
import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"
"darvaza.org/core"
"github.com/libdns/libdns"
)
// Show shows current DNS entries
func (mgr *Manager) Show(ctx context.Context, names ...string) error {
recs, err := mgr.GetRecords(ctx, names...)
if err != nil {
return core.Wrap(err, "GetRecords")
}
SortRecords(recs)
return writeRecords(recs, os.Stdout)
}
func writeRecords(recs []libdns.Record, w io.Writer) error {
var buf bytes.Buffer
for _, rr := range recs {
_ = fmtRecord(&buf, rr)
_, _ = buf.WriteRune('\n')
}
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
_, err := buf.WriteTo(w)
return err
}
func fmtRecord(w io.Writer, rr libdns.Record) error {
ttl := int(rr.TTL / time.Second)
if ttl < 1 {
ttl = 1
}
_, err := fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s",
rr.Name,
ttl,
rr.Type,
rr.Value)
if err == nil {
if rr.ID != "" {
_, err = fmt.Fprintf(w, "\t; %s", rr.ID)
}
}
return err
}
-347
View File
@@ -1,347 +0,0 @@
package dns
import (
"context"
"net/netip"
"sort"
"strings"
"time"
"darvaza.org/core"
"darvaza.org/slog"
"github.com/libdns/libdns"
)
// SyncAddrRecord is similar to AddrRecord but include libdns.Record details
// fetched from the Provider
type SyncAddrRecord struct {
Name string
Addrs []SyncAddr
}
// SyncAddr extends netip.Addr with ID and TTL fetched from the Provider
type SyncAddr struct {
ID string
Addr netip.Addr
TTL time.Duration
}
// Export assembles a libdns.Record
func (rec *SyncAddr) Export(name string) libdns.Record {
return libdns.Record{
ID: rec.ID,
Name: name,
Type: core.IIf(rec.Addr.Is6(), "AAAA", "A"),
TTL: time.Second,
Value: rec.Addr.String(),
}
}
// SortSyncAddrSlice sorts a slice of [SyncAddr] by its address
func SortSyncAddrSlice(s []SyncAddr) []SyncAddr {
sort.Slice(s, func(i, j int) bool {
a1 := s[i].Addr
a2 := s[j].Addr
return a1.Less(a2)
})
return s
}
// GetSyncRecords pulls all the address records on DNS for our domain
func (mgr *Manager) GetSyncRecords(ctx context.Context) ([]SyncAddrRecord, error) {
recs, err := mgr.GetRecords(ctx)
if err != nil {
return nil, err
}
return mgr.asSyncRecords(recs)
}
// AsSyncAddr converts a A or AAAA [libdns.Record] into a [SyncAddr]
func (mgr *Manager) AsSyncAddr(rr libdns.Record) (SyncAddr, bool, error) {
var out SyncAddr
var addr netip.Addr
// skip non-address types
if rr.Type != "A" && rr.Type != "AAAA" {
return out, false, nil
}
// skip entries not containing our suffix
if mgr.suffix != "" {
if !strings.HasSuffix(rr.Name, mgr.suffix) {
return out, false, nil
}
}
err := addr.UnmarshalText([]byte(rr.Value))
if err != nil {
// invalid address on A or AAAA record
return out, false, err
}
out = SyncAddr{
ID: rr.ID,
TTL: rr.TTL,
Addr: addr,
}
return out, true, nil
}
func (mgr *Manager) asSyncRecordsMap(recs []libdns.Record) map[string][]SyncAddr {
// filter and convert
out := make(map[string][]SyncAddr)
for _, rr := range recs {
addr, ok, err := mgr.AsSyncAddr(rr)
switch {
case err != nil:
// skip invalid addresses
mgr.l.Error().
WithField("subsystem", "dns").
WithField(slog.ErrorFieldName, err).
WithField("name", rr.Name).
WithField("type", rr.Type).
WithField("addr", rr.Value).
Print()
case ok:
// store
out[rr.Name] = append(out[rr.Name], addr)
}
}
return out
}
func (mgr *Manager) asSyncRecords(recs []libdns.Record) ([]SyncAddrRecord, error) {
cache := mgr.asSyncRecordsMap(recs)
// prepare records
out := make([]SyncAddrRecord, len(cache))
names := make([]string, 0, len(cache))
for name := range cache {
names = append(names, name)
}
sort.Strings(names)
for i, name := range names {
addrs := cache[name]
out[i] = SyncAddrRecord{
Name: name,
Addrs: SortSyncAddrSlice(addrs),
}
}
return out, nil
}
// Sync updates all the address records on DNS for our domain
func (mgr *Manager) Sync(ctx context.Context) error {
current, err := mgr.GetSyncRecords(ctx)
if err != nil {
return core.Wrap(err, "GetRecords")
}
goal := mgr.genAllAddrRecords()
for _, p := range makeSyncMap(current, goal) {
err := mgr.doSync(ctx, p.Name, p.Before, p.After)
if err != nil {
return err
}
}
return nil
}
func (mgr *Manager) doSync(ctx context.Context, name string,
before []SyncAddr, after []netip.Addr) error {
//
var err error
for _, a := range after {
before, err = mgr.doSyncUpdateOrInsert(ctx, name, a, before)
if err != nil {
return err
}
}
for _, b := range before {
err = mgr.doSyncRemove(ctx, name, b)
if err != nil {
return err
}
}
return nil
}
func (mgr *Manager) doSyncUpdateOrInsert(ctx context.Context, name string,
addr netip.Addr, addrs []SyncAddr) ([]SyncAddr, error) {
//
var err error
i, ok := findSyncAddrSorted(addr, addrs)
if ok {
rec := addrs[i]
addrs = append(addrs[:i], addrs[i+1:]...)
err = mgr.doSyncUpdate(ctx, name, addr, rec)
} else {
err = mgr.doSyncInsert(ctx, name, addr)
}
return addrs, err
}
func (mgr *Manager) doSyncUpdate(ctx context.Context, name string,
addr netip.Addr, rec SyncAddr) error {
//
var log slog.Logger
var msg string
var err error
if rec.TTL != time.Second {
// amend TTL
// TODO: batch updates
_, err = mgr.p.SetRecords(ctx, mgr.domain, []libdns.Record{
rec.Export(name),
})
if err == nil {
log = mgr.l.Info()
msg = "Updated"
} else {
log = mgr.l.Error().
WithField(slog.ErrorFieldName, err)
msg = "Failed"
}
} else {
log = mgr.l.Info()
msg = "OK"
}
log.
WithField("subsystem", "dns").
WithField("name", name).
WithField("addr", addr).
Print(msg)
return err
}
func (mgr *Manager) doSyncInsert(ctx context.Context, name string,
addr netip.Addr) error {
//
var log slog.Logger
var msg string
rec := libdns.Record{
Name: name,
Type: core.IIf(addr.Is6(), "AAAA", "A"),
TTL: time.Second,
Value: addr.String(),
}
_, err := mgr.p.AppendRecords(ctx, mgr.domain, []libdns.Record{
rec,
})
if err != nil {
log = mgr.l.Error().
WithField(slog.ErrorFieldName, err)
msg = "Failed to Add"
} else {
log = mgr.l.Info()
msg = "Added"
}
log.
WithField("subsystem", "dns").
WithField("name", name).
WithField("addr", addr).
Print(msg)
return err
}
func (mgr *Manager) doSyncRemove(ctx context.Context, name string,
rec SyncAddr) error {
//
var log slog.Logger
var msg string
// TODO: batch deletes
_, err := mgr.p.DeleteRecords(ctx, mgr.domain, []libdns.Record{
rec.Export(name),
})
if err != nil {
log = mgr.l.Error().
WithField(slog.ErrorFieldName, err)
msg = "Failed to Delete"
} else {
log = mgr.l.Warn()
msg = "Deleted"
}
log.
WithField("subsystem", "dns").
WithField("name", name).
WithField("addr", rec.Addr).
Print(msg)
return err
}
func findSyncAddrSorted(target netip.Addr, addrs []SyncAddr) (int, bool) {
for i, a := range addrs {
switch target.Compare(a.Addr) {
case 0:
// match
return i, true
case -1:
// miss
return -1, false
default:
// next
}
}
return -1, false
}
type syncMapEntry struct {
Name string
Before []SyncAddr
After []netip.Addr
}
func makeSyncMap(current []SyncAddrRecord,
goal []AddrRecord) map[string]syncMapEntry {
//
data := make(map[string]syncMapEntry)
for _, cur := range current {
me, ok := data[cur.Name]
if !ok {
me = syncMapEntry{
Name: cur.Name,
}
}
me.Before = append(me.Before, cur.Addrs...)
data[cur.Name] = me
}
for _, rr := range goal {
me, ok := data[rr.Name]
if !ok {
me = syncMapEntry{
Name: rr.Name,
}
}
me.After = append(me.After, rr.Addr...)
data[rr.Name] = me
}
return data
}
+15 -43
View File
@@ -4,60 +4,32 @@ import (
"bytes"
"fmt"
"io"
"net/netip"
"time"
"github.com/libdns/libdns"
)
// WriteTo writes the DNS data for the cluster
func (mgr *Manager) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
cache := make(map[string][]netip.Addr)
// zones
for _, zoneName := range mgr.genZonesSorted() {
z := mgr.zones[zoneName]
mgr.writeZoneHosts(&buf, z)
// zone alias
addrs := mgr.genZoneAddresses(z)
rr := AddrRecord{
Name: mgr.fqdn(zoneName + mgr.suffix),
Addr: addrs,
}
_, _ = rr.WriteTo(&buf)
// and cache for regions
cache[zoneName] = addrs
records := mgr.genAllAddrRecords()
for i := range records {
r := &records[i]
r.Name = fmt.Sprintf("%s.%s.", r.Name, mgr.domain)
}
// regions, sorted
for _, name := range mgr.genRegionsSorted() {
addrs := mgr.genRegionAddressesCached(name, cache)
mgr.writeRegionAddresses(&buf, name, addrs)
for _, rr1 := range records {
for _, rr2 := range rr1.Export() {
writeRecord(&buf, rr2)
}
}
return buf.WriteTo(w)
}
func (mgr *Manager) writeZoneHosts(w io.Writer, z *Zone) {
_, _ = fmt.Fprintf(w, ";\n; %s\n;\n", z.Name)
for _, rr := range mgr.genZoneHostRecords(z) {
rr.Name = mgr.fqdn(rr.Name)
_, _ = rr.WriteTo(w)
}
}
func (mgr *Manager) writeRegionAddresses(w io.Writer, name string, addrs []netip.Addr) {
_, _ = fmt.Fprintf(w, "; %s\n", name)
rr := AddrRecord{
Name: mgr.fqdn(name + mgr.suffix),
Addr: addrs,
}
_, _ = rr.WriteTo(w)
func writeRecord(w io.Writer, rr libdns.Record) {
_, _ = fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s\n",
rr.Name, int(rr.TTL/time.Second),
rr.Type, rr.Value)
}

Some files were not shown because too many files have changed in this diff Show More