Browse Source

Merge pull request 'zones: warn but not fail when scanning finds unknown monitors' (#13)

Reviewed-on: #13
pull/17/head v0.5.5
Alejandro Mery 1 year ago
parent
commit
ab8ffdd507
  1. 104
      pkg/zones/ceph_scan.go

104
pkg/zones/ceph_scan.go

@ -1,89 +1,12 @@
package zones package zones
import ( import (
"net/netip"
"os" "os"
"strings"
"darvaza.org/core" "darvaza.org/slog"
"git.jpi.io/amery/jpictl/pkg/ceph" "git.jpi.io/amery/jpictl/pkg/ceph"
) )
// CephMissingMonitorError is an error that contains ceph
// monitors present in ceph.conf but not found on the cluster
type CephMissingMonitorError struct {
Names []string
Addrs []netip.Addr
}
func (err *CephMissingMonitorError) appendName(name string) {
err.Names = append(err.Names, name)
}
func (err *CephMissingMonitorError) appendAddr(addr netip.Addr) {
err.Addrs = append(err.Addrs, addr)
}
// OK tells if this instance actual shouldn't be treated as an error
func (err CephMissingMonitorError) OK() bool {
switch {
case len(err.Names) > 0:
return false
case len(err.Addrs) > 0:
return false
default:
return true
}
}
func (err CephMissingMonitorError) Error() string {
if !err.OK() {
var buf strings.Builder
_, _ = buf.WriteString("missing:")
err.writeNames(&buf)
err.writeAddrs(&buf)
return buf.String()
}
// no error
return ""
}
func (err *CephMissingMonitorError) writeNames(w *strings.Builder) {
if len(err.Names) > 0 {
_, _ = w.WriteString(" mon_initial_members:")
for i, name := range err.Names {
if i != 0 {
_, _ = w.WriteRune(',')
}
_, _ = w.WriteString(name)
}
}
}
func (err *CephMissingMonitorError) writeAddrs(w *strings.Builder) {
if len(err.Addrs) > 0 {
_, _ = w.WriteString(" mon_host:")
for i, addr := range err.Addrs {
if i != 0 {
_, _ = w.WriteRune(',')
}
_, _ = w.WriteString(addr.String())
}
}
}
// AsError returns nil if the instance is actually OK
func (err *CephMissingMonitorError) AsError() error {
if err == nil || err.OK() {
return nil
}
return err
}
type cephScanTODO struct { type cephScanTODO struct {
names map[string]bool names map[string]bool
addrs map[string]bool addrs map[string]bool
@ -111,26 +34,24 @@ func (todo *cephScanTODO) checkMachine(p *Machine) bool {
return false return false
} }
func (todo *cephScanTODO) Missing() error { func (todo *cephScanTODO) LogMissing(log slog.Logger) {
var check CephMissingMonitorError
for name, found := range todo.names { for name, found := range todo.names {
if !found { if !found {
check.appendName(name) log.Warn().
WithField("subsystem", "ceph").
WithField("monitor", name).
Print("unknown monitor")
} }
} }
for addr, found := range todo.addrs { for addr, found := range todo.addrs {
if !found { if !found {
var a netip.Addr log.Warn().
// it wouldn't be on the map if it wasn't valid WithField("subsystem", "ceph").
_ = a.UnmarshalText([]byte(addr)) WithField("monitor", addr).
Print("unknown monitor")
check.appendAddr(a)
} }
} }
return check.AsError()
} }
func newCephScanTODO(cfg *ceph.Config) *cephScanTODO { func newCephScanTODO(cfg *ceph.Config) *cephScanTODO {
@ -169,9 +90,8 @@ func (m *Zones) scanCephMonitors(_ *ScanOptions) error {
p.CephMonitor = todo.checkMachine(p) p.CephMonitor = todo.checkMachine(p)
return false return false
}) })
if err := todo.Missing(); err != nil {
return core.Wrap(err, "ceph") todo.LogMissing(m.log)
}
} }
// make sure every zone has one // make sure every zone has one

Loading…
Cancel
Save