Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Oct 24, 2023
1 parent 14a9ab3 commit df4655a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 52 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewCluster(l logger.Logger, clusterName string, opts ...Option) (cluster.Op
}

// Configure Metadata Manager
mm, err := metadata.New("", clusterName)
mm, err := metadata.New("")
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/baremetal/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Cluster) Get(ctx context.Context, options *opt.GetOptions) error {
}

func (c *Cluster) get(_ context.Context, options *opt.GetOptions) (*ClusterMetadata, error) {
csd := c.mm.GetClusterScopeDir()
csd := c.mm.GetClusterScopeDirs()
_, err := os.Stat(csd.BaseDir)
if os.IsNotExist(err) {
return nil, fmt.Errorf("cluster %s is not exist", options.Name)
Expand Down
18 changes: 7 additions & 11 deletions pkg/deployer/baremetal/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewDeployer(l logger.Logger, clusterName string, opts ...Option) (Interface
return nil, err
}

mm, err := metadata.New("", clusterName)
mm, err := metadata.New("")
if err != nil {
return nil, err
}
Expand All @@ -88,21 +88,17 @@ func NewDeployer(l logger.Logger, clusterName string, opts ...Option) (Interface
d.am = am

if !d.createNoDirs {
if err = mm.AllocateClusterScopeDirs(); err != nil {
mm.AllocateClusterScopeDirs(clusterName)
if err = mm.CreateClusterScopeDirs(d.config); err != nil {
return nil, err
}

csd := mm.GetClusterScopeDir()
csd := mm.GetClusterScopeDirs()
d.bm = component.NewBareMetalCluster(d.config.Cluster, component.WorkingDirs{
DataDir: csd.DataDir,
LogsDir: csd.LogsDir,
PidsDir: csd.PidsDir,
}, &d.wg, d.logger)

// Save a copy of cluster config in yaml format.
if err = mm.AllocateClusterConfigPath(d.config); err != nil {
return nil, err
}
}

return d, nil
Expand Down Expand Up @@ -164,7 +160,7 @@ func WithCreateNoDirs() Option {
}

func (d *Deployer) GetGreptimeDBCluster(ctx context.Context, name string, options *GetGreptimeDBClusterOptions) (*GreptimeDBCluster, error) {
csd := d.mm.GetClusterScopeDir()
csd := d.mm.GetClusterScopeDirs()
_, err := os.Stat(csd.BaseDir)
if os.IsNotExist(err) {
return nil, fmt.Errorf("cluster %s is not exist", name)
Expand Down Expand Up @@ -254,7 +250,7 @@ func (d *Deployer) DeleteGreptimeDBCluster(ctx context.Context, name string, opt
// deleteGreptimeDBClusterForeground delete the whole cluster if it runs in foreground.
func (d *Deployer) deleteGreptimeDBClusterForeground(ctx context.Context, option component.DeleteOptions) error {
// No matter what options are, the config file of one cluster must be deleted.
csd := d.mm.GetClusterScopeDir()
csd := d.mm.GetClusterScopeDirs()
if err := os.Remove(csd.ConfigPath); err != nil {
return err
}
Expand Down Expand Up @@ -366,7 +362,7 @@ func (d *Deployer) checkEtcdHealth(etcdBin string) error {
time.Sleep(1 * time.Second)
}

csd := d.mm.GetClusterScopeDir()
csd := d.mm.GetClusterScopeDirs()
return fmt.Errorf("etcd is not ready in 10 second! You can find its logs in %s", path.Join(csd.LogsDir, "etcd"))
}

Expand Down
6 changes: 2 additions & 4 deletions pkg/helm/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ func NewLoader(l logger.Logger, opts ...Option) (*Loader, error) {
}
r.am = am

// We ignore the cluster name here, because we are sure that
// there's no operations on cluster scope directories in this pkg.
mm, err := metadata.New("", "")
mm, err := metadata.New("")
if err != nil {
return nil, err
}
Expand All @@ -79,7 +77,7 @@ func NewLoader(l logger.Logger, opts ...Option) (*Loader, error) {

func WithHomeDir(dir string) Option {
return func(r *Loader) {
mm, err := metadata.New(dir, "")
mm, err := metadata.New(dir)
if err != nil {
r.logger.Errorf("failed to create metadata manager: %v", err)
os.Exit(1)
Expand Down
44 changes: 14 additions & 30 deletions pkg/metadata/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ type Manager interface {
// AllocateArtifactFilePath allocates the file path of the artifact.
AllocateArtifactFilePath(src *artifacts.Source, installBinary bool) (string, error)

// AllocateClusterScopeDirs allocates the directories of one cluster.
AllocateClusterScopeDirs() error

// AllocateClusterConfigPath allocates the config path of one cluster.
AllocateClusterConfigPath(cfg *config.Config) error
// AllocateClusterScopeDirs allocates the directories and config path of one cluster.
AllocateClusterScopeDirs(clusterName string)

// SetHomeDir sets the home directory of the metadata manager.
SetHomeDir(dir string) error
Expand All @@ -47,8 +44,11 @@ type Manager interface {
// It should be ${HomeDir}/${BaseDir}.
GetWorkingDir() string

// GetClusterScopeDir returns the cluster scope directory of current cluster.
GetClusterScopeDir() *ClusterScopeDir
// CreateClusterScopeDirs creates cluster scope directories and config path that allocated by AllocateClusterScopeDirs.
CreateClusterScopeDirs(cfg *config.Config) error

// GetClusterScopeDirs returns the cluster scope directory of current cluster.
GetClusterScopeDirs() *ClusterScopeDirs

// Clean cleans up all the metadata. It will remove the working directory.
Clean() error
Expand All @@ -65,7 +65,7 @@ const (
clusterPidsDir = "pids"
)

type ClusterScopeDir struct {
type ClusterScopeDirs struct {
BaseDir string
LogsDir string
DataDir string
Expand All @@ -76,12 +76,12 @@ type ClusterScopeDir struct {
type manager struct {
workingDir string

clusterDir *ClusterScopeDir
clusterDir *ClusterScopeDirs
}

var _ Manager = &manager{}

func New(homeDir string, clusterName string) (Manager, error) {
func New(homeDir string) (Manager, error) {
m := &manager{}
if homeDir == "" {
dir, err := os.UserHomeDir()
Expand All @@ -92,16 +92,11 @@ func New(homeDir string, clusterName string) (Manager, error) {
} else {
m.workingDir = filepath.Join(homeDir, BaseDir)
}

if len(clusterName) > 0 {
m.initClusterScopeDirs(clusterName)
}

return m, nil
}

func (m *manager) initClusterScopeDirs(clusterName string) {
csd := &ClusterScopeDir{
func (m *manager) AllocateClusterScopeDirs(clusterName string) {
csd := &ClusterScopeDirs{
// ${HomeDir}/${BaseDir}${ClusterName}
BaseDir: path.Join(m.workingDir, clusterName),
}
Expand Down Expand Up @@ -137,7 +132,7 @@ func (m *manager) AllocateArtifactFilePath(src *artifacts.Source, installBinary
return filePath, nil
}

func (m *manager) AllocateClusterScopeDirs() error {
func (m *manager) CreateClusterScopeDirs(cfg *config.Config) error {
if m.clusterDir == nil {
return fmt.Errorf("unallocated cluster dir, please initialize a metadata manager with cluster name provided")
}
Expand All @@ -154,17 +149,6 @@ func (m *manager) AllocateClusterScopeDirs() error {
return err
}
}
return nil
}

func (m *manager) AllocateClusterConfigPath(cfg *config.Config) error {
if m.clusterDir == nil {
return fmt.Errorf("unallocated cluster dir, please initialize a metadata manager with cluster name provided")
}

if err := fileutils.EnsureDir(m.clusterDir.BaseDir); err != nil {
return err
}

f, err := os.Create(m.clusterDir.ConfigPath)
if err != nil {
Expand Down Expand Up @@ -203,7 +187,7 @@ func (m *manager) GetWorkingDir() string {
return m.workingDir
}

func (m *manager) GetClusterScopeDir() *ClusterScopeDir {
func (m *manager) GetClusterScopeDirs() *ClusterScopeDirs {
return m.clusterDir
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/metadata/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestMetadataManager(t *testing.T) {
}
defer os.RemoveAll(tempDir)

m, err := New(tempDir, "")
m, err := New(tempDir)
if err != nil {
t.Fatalf("failed to create metadata manager: %v", err)
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestMetadataManager(t *testing.T) {
}

func TestCreateMetadataManagerWithEmptyHomeDir(t *testing.T) {
m, err := New("", "")
m, err := New("")
if err != nil {
t.Fatalf("failed to create metadata manager: %v", err)
}
Expand All @@ -120,14 +120,15 @@ func TestCreateMetadataManagerWithEmptyHomeDir(t *testing.T) {
}

func TestMetadataManagerWithClusterConfigPath(t *testing.T) {
m, err := New("/tmp", "test")
m, err := New("/tmp")
assert.NoError(t, err)

expect := config.DefaultConfig()
err = m.AllocateClusterConfigPath(expect)
m.AllocateClusterScopeDirs("test")
err = m.CreateClusterScopeDirs(expect)
assert.NoError(t, err)

csd := m.GetClusterScopeDir()
csd := m.GetClusterScopeDirs()
assert.NotNil(t, csd)
assert.NotEmpty(t, csd.ConfigPath)

Expand Down

0 comments on commit df4655a

Please sign in to comment.