Skip to content

Commit

Permalink
Add ability configure register from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jskswamy committed Oct 23, 2018
1 parent 449af2a commit 9a94327
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
17 changes: 10 additions & 7 deletions cmd/stolonctl/cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ var log = slog.S()
func init() {
Register.PersistentFlags().StringVar(&rCfg.Backend, "register-backend", "consul", "register backend type (consul)")
Register.PersistentFlags().StringVar(&rCfg.Endpoints, "register-endpoints", "http://127.0.0.1:8500", "a common-delimited list of register endpoints (use https scheme for tls communication) defaults: http://127.0.0.1:8500 for consul")
Register.PersistentFlags().StringVar(&rCfg.TagMasterAs, "tag-master-as", "master", "tag to be used when registering master")
Register.PersistentFlags().StringVar(&rCfg.TagSlaveAs, "tag-slave-as", "slave", "tag to be used when registering slave")
Register.PersistentFlags().BoolVar(&cfg.Debug, "debug", false, "enable debug logging")
Register.PersistentFlags().IntVar(&rCfg.SleepInterval, "sleep-interval", 10, "number of seconds to sleep before probing for change")
CmdStolonCtl.AddCommand(Register)
}

const (
DefaultSleepInterval = 10 * time.Second
)
func sleepInterval() time.Duration {
return time.Duration(rCfg.SleepInterval) * time.Second
}

func checkConfig(cfg *config, rCfg *register.Config) error {
if err := cmd.CheckCommonConfig(&cfg.CommonConfig); err != nil {
Expand Down Expand Up @@ -113,7 +116,7 @@ func registerCluster(sigs chan os.Signal, cfg *config, rCfg *register.Config) er
endCh <- struct{}{}
}()
case <-endCh:
timerCh = time.NewTimer(DefaultSleepInterval).C
timerCh = time.NewTimer(sleepInterval()).C
}
}
}
Expand All @@ -124,7 +127,7 @@ func checkAndRegisterMasterAndSlaves(store store.Store, service register.Service
log.Errorf("unable to get info about existing services: %v", err)
return
}
cluster, err := register.NewCluster(cfg.ClusterName, store)
cluster, err := register.NewCluster(cfg.ClusterName, rCfg, store)
if err != nil {
log.Errorf("cannot get cluster data: %v", err)
return
Expand Down Expand Up @@ -162,8 +165,8 @@ func deRegisterService(service register.ServiceDiscovery, serviceInfo *register.
return
}
if err := service.DeRegister(serviceInfo); err != nil {
log.Errorf("unable to de-register %s with uid %s as %v, reason: %s", serviceInfo.Name, serviceInfo.ID, serviceInfo.Tags, err.Error())
log.Errorf("unable to deregister %s with uid %s as %v, reason: %s", serviceInfo.Name, serviceInfo.ID, serviceInfo.Tags, err.Error())
} else {
log.Infof("successfully de-registered %s with uid %s as %v", serviceInfo.Name, serviceInfo.ID, serviceInfo.Tags)
log.Infof("successfully deregistered %s with uid %s as %v", serviceInfo.Name, serviceInfo.ID, serviceInfo.Tags)
}
}
14 changes: 8 additions & 6 deletions cmd/stolonctl/cmd/register/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ import (
// Cluster type exposes necessary methods to find master and slave
// from underlying store
type Cluster struct {
name string
cd *cluster.ClusterData
name string
cd *cluster.ClusterData
tagMasterAs string
tagSlaveAs string
}

// NewCluster returns an new instance of Cluster
func NewCluster(name string, store store.Store) (*Cluster, error) {
func NewCluster(name string, rCfg Config, store store.Store) (*Cluster, error) {
cd, _, err := store.GetClusterData(context.TODO())

if err != nil {
return nil, err
} else if cd == nil {
return nil, errors.New("no cluster data available")
}
return &Cluster{name: name, cd: cd}, nil
return &Cluster{name: name, cd: cd, tagMasterAs: rCfg.TagMasterAs, tagSlaveAs: rCfg.TagSlaveAs}, nil
}

// ServiceInfos returns all the service information from the cluster data in underlying store
Expand All @@ -51,9 +53,9 @@ func (c *Cluster) ServiceInfos() (ServiceInfos, error) {
master := c.cd.Cluster.Status.Master
for uid, db := range c.cd.DBs {
if db.Status.Healthy {
tags := Tags{"slave"}
tags := Tags{c.tagSlaveAs}
if uid == master {
tags = Tags{"master"}
tags = Tags{c.tagMasterAs}
}
info, err := NewServiceInfo(c.name, db, tags)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions cmd/stolonctl/cmd/register/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package register

import (
"errors"
"github.com/sorintlab/stolon/internal/mock/store"
"testing"

"github.com/golang/mock/gomock"
"github.com/sorintlab/stolon/internal/cluster"
"github.com/sorintlab/stolon/internal/mock/store"
)

func TestNewCluster(t *testing.T) {
Expand All @@ -31,7 +31,7 @@ func TestNewCluster(t *testing.T) {
mockStore := mock_store.NewMockStore(ctrl)
mockStore.EXPECT().GetClusterData(gomock.Any()).Return(nil, nil, errors.New("unable to fetch cluster data"))

_, err := NewCluster("test", mockStore)
_, err := NewCluster("test", Config{}, mockStore)

if err == nil || err.Error() != "unable to fetch cluster data" {
t.Errorf("expected unable to fetch cluster data error")
Expand All @@ -45,7 +45,7 @@ func TestNewCluster(t *testing.T) {
mockStore := mock_store.NewMockStore(ctrl)
mockStore.EXPECT().GetClusterData(gomock.Any()).Return(nil, nil, nil)

_, err := NewCluster("test", mockStore)
_, err := NewCluster("test", Config{}, mockStore)

if err == nil || err.Error() != "no cluster data available" {
t.Errorf("expected no cluster data available error")
Expand All @@ -62,7 +62,7 @@ func TestNewCluster(t *testing.T) {

expected := Cluster{name: "test", cd: cd}

actual, err := NewCluster("test", mockStore)
actual, err := NewCluster("test", Config{}, mockStore)

if expected.name != actual.name {
t.Errorf("expected name to be %s but got %s", expected.name, actual.name)
Expand Down Expand Up @@ -98,7 +98,10 @@ func TestServiceInfos(t *testing.T) {
Status: cluster.ClusterStatus{Master: "master"},
},
},
name: "test"}
name: "test",
tagMasterAs: "master",
tagSlaveAs: "slave",
}

infos, err := cl.ServiceInfos()

Expand Down
7 changes: 5 additions & 2 deletions cmd/stolonctl/cmd/register/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import (
// Config represents necessary configurations which can passed
// for registering master and slave info for service discovery
type Config struct {
Backend string
Endpoints string
Backend string
Endpoints string
SleepInterval int
TagMasterAs string
TagSlaveAs string
}

// Validate returns nil if the config is valid, else returns error with
Expand Down
2 changes: 1 addition & 1 deletion cmd/stolonctl/cmd/register/serviceinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (info *ServiceInfo) Compare(target ServiceInfo) bool {
func NewServiceInfo(name string, db *cluster.DB, tags []string) (*ServiceInfo, error) {
port, err := strconv.Atoi(db.Status.Port)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("invalid database port %s", db.Status.Port))
return nil, fmt.Errorf(fmt.Sprintf("invalid database port %s for %s with uid %s", db.Status.Port, name, db.UID))
}
return &ServiceInfo{
Name: name,
Expand Down
4 changes: 2 additions & 2 deletions cmd/stolonctl/cmd/register/serviceinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestNewServiceInfo(t *testing.T) {

if actual != nil {
t.Errorf("expected service info to be nil")
} else if err == nil || err.Error() != "invalid database port cat" {
t.Errorf("expected invalid database port error")
} else if err == nil || err.Error() != "invalid database port cat for test with uid unique" {
t.Errorf("expected invalid database port error but was %s", err.Error())
}
})
}
Expand Down

0 comments on commit 9a94327

Please sign in to comment.