Browse Source

cluster: make domain, basedir and cluster name public fields

Signed-off-by: Alejandro Mery <amery@jpi.io>
Alejandro Mery 1 year ago
parent
commit
c9314b3b7c
  1. 6
      cmd/jpictl/config.go
  2. 7
      pkg/cluster/cluster.go
  3. 46
      pkg/cluster/cluster_scan_options.go
  4. 17
      pkg/cluster/machine.go

6
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),
)

7
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

46
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
}

17
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

Loading…
Cancel
Save