From 9a9432703587a1d66e66431495942247a41fd360 Mon Sep 17 00:00:00 2001 From: Krishnaswamy Subramanian Date: Wed, 24 Oct 2018 01:38:30 +0530 Subject: [PATCH] Add ability configure register from cli --- cmd/stolonctl/cmd/register.go | 17 ++++++++++------- cmd/stolonctl/cmd/register/cluster.go | 14 ++++++++------ cmd/stolonctl/cmd/register/cluster_test.go | 13 ++++++++----- cmd/stolonctl/cmd/register/config.go | 7 +++++-- cmd/stolonctl/cmd/register/serviceinfo.go | 2 +- cmd/stolonctl/cmd/register/serviceinfo_test.go | 4 ++-- 6 files changed, 34 insertions(+), 23 deletions(-) diff --git a/cmd/stolonctl/cmd/register.go b/cmd/stolonctl/cmd/register.go index f92ff6a6e..f47505f2c 100644 --- a/cmd/stolonctl/cmd/register.go +++ b/cmd/stolonctl/cmd/register.go @@ -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 { @@ -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 } } } @@ -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 @@ -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) } } diff --git a/cmd/stolonctl/cmd/register/cluster.go b/cmd/stolonctl/cmd/register/cluster.go index 73cfc921b..ed5bdda44 100644 --- a/cmd/stolonctl/cmd/register/cluster.go +++ b/cmd/stolonctl/cmd/register/cluster.go @@ -25,12 +25,14 @@ 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 { @@ -38,7 +40,7 @@ func NewCluster(name string, store store.Store) (*Cluster, error) { } 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 @@ -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 { diff --git a/cmd/stolonctl/cmd/register/cluster_test.go b/cmd/stolonctl/cmd/register/cluster_test.go index 5d4b80151..3d596e1da 100644 --- a/cmd/stolonctl/cmd/register/cluster_test.go +++ b/cmd/stolonctl/cmd/register/cluster_test.go @@ -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) { @@ -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") @@ -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") @@ -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) @@ -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() diff --git a/cmd/stolonctl/cmd/register/config.go b/cmd/stolonctl/cmd/register/config.go index a216a4d21..c01bbb203 100644 --- a/cmd/stolonctl/cmd/register/config.go +++ b/cmd/stolonctl/cmd/register/config.go @@ -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 diff --git a/cmd/stolonctl/cmd/register/serviceinfo.go b/cmd/stolonctl/cmd/register/serviceinfo.go index a17501bd7..d8f9012cf 100644 --- a/cmd/stolonctl/cmd/register/serviceinfo.go +++ b/cmd/stolonctl/cmd/register/serviceinfo.go @@ -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, diff --git a/cmd/stolonctl/cmd/register/serviceinfo_test.go b/cmd/stolonctl/cmd/register/serviceinfo_test.go index 08b7ee3ca..8c5531dc1 100644 --- a/cmd/stolonctl/cmd/register/serviceinfo_test.go +++ b/cmd/stolonctl/cmd/register/serviceinfo_test.go @@ -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()) } }) }