From d0b0698c103bc9a8a77d09cde897f812d11f7e48 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 10 Sep 2023 21:38:07 +0000 Subject: [PATCH] cluster: make domain, basedir and cluster name public fields Signed-off-by: Alejandro Mery --- cmd/jpictl/config.go | 6 ++-- pkg/cluster/cluster.go | 7 +++-- pkg/cluster/cluster_scan_options.go | 46 ++++++++++++++--------------- pkg/cluster/machine.go | 17 ++++++----- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/cmd/jpictl/config.go b/cmd/jpictl/config.go index dba4f2f..a7649fc 100644 --- a/cmd/jpictl/config.go +++ b/cmd/jpictl/config.go @@ -9,13 +9,13 @@ type Config struct { } var cfg = &Config{ - Base: "./m", - Domain: "m.jpi.cloud", + Base: "m", + Domain: "jpi.cloud", } // LoadZones loads all zones and machines in the config directory func (cfg *Config) LoadZones(resolve bool) (*cluster.Cluster, error) { - return cluster.New(cfg.Base, cfg.Domain, + return cluster.NewFromDirectory(cfg.Base, cfg.Domain, cluster.ResolvePublicAddresses(resolve), cluster.WithLogger(log), ) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8475fb8..654333a 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -21,10 +21,13 @@ type Cluster struct { dir fs.FS log slog.Logger resolver resolver.Resolver - domain string + + BaseDir string `json:"dir,omitempty" yaml:"dir,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Domain string `json:"domain,omitempty" yaml:"domain,omitempty"` CephFSID uuid.UUID `json:"ceph_fsid,omitempty" yaml:"ceph_fsid,omitempty"` - Zones []*Zone + Zones []*Zone `json:"zones,omitempty" yaml:"zones,omitempty"` } // revive:enable:line-length-limit diff --git a/pkg/cluster/cluster_scan_options.go b/pkg/cluster/cluster_scan_options.go index 8284e9f..53d2a28 100644 --- a/pkg/cluster/cluster_scan_options.go +++ b/pkg/cluster/cluster_scan_options.go @@ -73,7 +73,7 @@ func WithLogger(log slog.Logger) ScanOption { } } -func (m *Cluster) setDefaults(opt *ScanOptions) error { +func (m *Cluster) setScanDefaults(opt *ScanOptions) error { if m.resolver == nil { h := DefaultLookuper() @@ -91,43 +91,41 @@ func (m *Cluster) setDefaults(opt *ScanOptions) error { return nil } -// NewFS builds a [Cluster] tree using the given directory -func NewFS(dir fs.FS, domain string, opts ...ScanOption) (*Cluster, error) { +// NewFromDirectory builds a [Cluster] tree using the given directory +func NewFromDirectory(dir, domain string, opts ...ScanOption) (*Cluster, error) { var scanOptions ScanOptions - z := &Cluster{ - dir: dir, - domain: domain, - } - - for _, opt := range opts { - if err := opt(z, &scanOptions); err != nil { - return nil, err - } + dir = filepath.Clean(dir) + fullPath, err := filepath.Abs(dir) + if err != nil { + return nil, err } - if err := z.setDefaults(&scanOptions); err != nil { + sub, err := os.NewFS().Sub(fullPath[1:]) + if err != nil { return nil, err } - if err := z.scan(&scanOptions); err != nil { - return nil, err + m := &Cluster{ + dir: sub, + BaseDir: dir, + Name: filepath.Base(fullPath), + Domain: domain, } - return z, nil -} + for _, opt := range opts { + if err := opt(m, &scanOptions); err != nil { + return nil, err + } + } -// New builds a [Cluster] tree using the given directory -func New(dir, domain string, opts ...ScanOption) (*Cluster, error) { - dir, err := filepath.Abs(dir) - if err != nil { + if err := m.setScanDefaults(&scanOptions); err != nil { return nil, err } - base, err := os.NewFS().Sub(dir[1:]) - if err != nil { + if err := m.scan(&scanOptions); err != nil { return nil, err } - return NewFS(base, domain, opts...) + return m, nil } diff --git a/pkg/cluster/machine.go b/pkg/cluster/machine.go index d8c38f1..46ac7e9 100644 --- a/pkg/cluster/machine.go +++ b/pkg/cluster/machine.go @@ -28,16 +28,19 @@ func (m *Machine) String() string { // FullName returns the Name of the machine including domain name func (m *Machine) FullName() string { - if domain := m.zone.zones.domain; domain != "" { - var s = []string{ - m.Name, - domain, + var name []string + + for _, s := range []string{ + m.Name, + m.zone.zones.Name, + m.zone.zones.Domain, + } { + if s != "" { + name = append(name, s) } - - return strings.Join(s, ".") } - return m.Name + return strings.Join(name, ".") } // IsGateway tells if the Machine is a ring0 gateway