From 1580c09746988aab59213f2ba8f07306491faf5d Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 23:45:19 +0000 Subject: [PATCH 1/3] cluster: add Machine.ReadLines() shortcut Signed-off-by: Alejandro Mery --- pkg/cluster/machine_file.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/cluster/machine_file.go b/pkg/cluster/machine_file.go index f4bb148..f182f47 100644 --- a/pkg/cluster/machine_file.go +++ b/pkg/cluster/machine_file.go @@ -53,6 +53,14 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) { 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) +} + // 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...) -- 2.17.1 From 892d8497400198564a6b6c708e24e9198a771bab Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 02:01:17 +0000 Subject: [PATCH 2/3] cluster: introduce Machine.Inactive flag if a Machine is Inactive, it won't be included on the DNS aliases for the zone or it's regions. v2: - Machine.Active() renamed to Machine.IsActive() Signed-off-by: Alejandro Mery --- cmd/jpictl/dns.go | 2 +- pkg/cluster/machine.go | 6 ++++++ pkg/cluster/regions.go | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/jpictl/dns.go b/cmd/jpictl/dns.go index f3a2cbe..ad13421 100644 --- a/cmd/jpictl/dns.go +++ b/cmd/jpictl/dns.go @@ -52,7 +52,7 @@ func populateDNSManager(mgr *dns.Manager, m *cluster.Cluster) error { m.ForEachZone(func(z *cluster.Zone) bool { z.ForEachMachine(func(p *cluster.Machine) bool { - err = mgr.AddHost(ctx, z.Name, p.ID, true, p.PublicAddresses...) + err = mgr.AddHost(ctx, z.Name, p.ID, p.IsActive(), p.PublicAddresses...) return err != nil }) diff --git a/pkg/cluster/machine.go b/pkg/cluster/machine.go index 46ac7e9..07dccbd 100644 --- a/pkg/cluster/machine.go +++ b/pkg/cluster/machine.go @@ -15,6 +15,7 @@ type Machine struct { 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"` @@ -43,6 +44,11 @@ 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(0) diff --git a/pkg/cluster/regions.go b/pkg/cluster/regions.go index 3384ca5..277ac15 100644 --- a/pkg/cluster/regions.go +++ b/pkg/cluster/regions.go @@ -32,7 +32,9 @@ func (r *Region) ForEachMachine(fn func(*Machine) bool) { var term bool z.ForEachMachine(func(p *Machine) bool { - term = fn(p) + if p.IsActive() { + term = fn(p) + } return term }) -- 2.17.1 From 99998dc7e889d6bf2807c2bea200d9412f02e920 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 03:12:33 +0000 Subject: [PATCH 3/3] cluster: mark Machine as Inactive if the "region" file contains "none" Signed-off-by: Alejandro Mery --- pkg/cluster/machine_scan.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/cluster/machine_scan.go b/pkg/cluster/machine_scan.go index ef3f221..259d945 100644 --- a/pkg/cluster/machine_scan.go +++ b/pkg/cluster/machine_scan.go @@ -3,6 +3,7 @@ package cluster import ( "context" "net/netip" + "os" "strconv" "strings" "time" @@ -81,7 +82,30 @@ func (m *Machine) scan(_ *ScanOptions) error { } } - return nil + 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 -- 2.17.1