Skip to content

Commit bc9145f

Browse files
committed
Added ability to manage cluster auto-failover settings.
1 parent a65460a commit bc9145f

9 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
)
7+
8+
var clusterSettingsDisableAutoFailoverCmd = &cobra.Command{
9+
Use: "disable-autofailover <cluster-id>",
10+
Short: "Disables auto-failover for a cluster",
11+
Args: cobra.ExactArgs(1),
12+
Run: func(cmd *cobra.Command, args []string) {
13+
helper := CmdHelper{}
14+
logger := helper.GetLogger()
15+
ctx := helper.GetContext()
16+
17+
clusterId := args[0]
18+
19+
_, deployer, cluster := helper.IdentifyCluster(ctx, clusterId)
20+
21+
err := deployer.SetAutoFailover(ctx, cluster.GetID(), false, 0)
22+
if err != nil {
23+
logger.Fatal("failed to disable auto-failover", zap.Error(err))
24+
}
25+
},
26+
}
27+
28+
func init() {
29+
clusterSettingsCmd.AddCommand(clusterSettingsDisableAutoFailoverCmd)
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"go.uber.org/zap"
6+
)
7+
8+
var clusterSettingsEnableAutoFailoverCmd = &cobra.Command{
9+
Use: "enable-autofailover <cluster-id>",
10+
Short: "Enables auto-failover for a cluster",
11+
Args: cobra.ExactArgs(1),
12+
Run: func(cmd *cobra.Command, args []string) {
13+
helper := CmdHelper{}
14+
logger := helper.GetLogger()
15+
ctx := helper.GetContext()
16+
17+
clusterId := args[0]
18+
timeout, _ := cmd.Flags().GetInt("timeout")
19+
20+
_, deployer, cluster := helper.IdentifyCluster(ctx, clusterId)
21+
22+
err := deployer.SetAutoFailover(ctx, cluster.GetID(), true, timeout)
23+
if err != nil {
24+
logger.Fatal("failed to enable auto-failover", zap.Error(err))
25+
}
26+
},
27+
}
28+
29+
func init() {
30+
clusterSettingsCmd.AddCommand(clusterSettingsEnableAutoFailoverCmd)
31+
32+
clusterSettingsEnableAutoFailoverCmd.Flags().Int("timeout", 120, "Auto-failover timeout in seconds")
33+
}

cmd/cluster-settings.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var clusterSettingsCmd = &cobra.Command{
8+
Use: "cluster-settings",
9+
Short: "Provides cluster settings management tools",
10+
Run: nil,
11+
}
12+
13+
func init() {
14+
rootCmd.AddCommand(clusterSettingsCmd)
15+
}

deployment/caodeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,7 @@ func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodes
788788
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodes []string) error {
789789
return errors.New("caodeploy does not support killing couchbase process")
790790
}
791+
792+
func (d *Deployer) SetAutoFailover(ctx context.Context, clusterID string, enabled bool, timeout int) error {
793+
return errors.New("caodeploy does not support setting auto-failover")
794+
}

deployment/clouddeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,3 +2503,7 @@ func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodes
25032503
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodeIDs []string) error {
25042504
return errors.New("clouddeploy does not support killing couchbase process")
25052505
}
2506+
2507+
func (d *Deployer) SetAutoFailover(ctx context.Context, clusterID string, enabled bool, timeout int) error {
2508+
return errors.New("clouddeploy does not support setting auto-failover")
2509+
}

deployment/deployer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,5 @@ type Deployer interface {
152152
DropLink(ctx context.Context, columnarID, linkName string) error
153153
EnableDataApi(ctx context.Context, clusterID string) error
154154
KillCouchbase(ctx context.Context, clusterID string, nodes []string) error
155+
SetAutoFailover(ctx context.Context, clusterID string, enabled bool, timeout int) error
155156
}

deployment/dockerdeploy/deployer.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,18 @@ func (d *Deployer) RedeployCluster(ctx context.Context, clusterID string) error
13991399
return errors.New("docker deploy does not support redeploy cluster")
14001400
}
14011401

1402+
func (d *Deployer) SetAutoFailover(ctx context.Context, clusterID string, enabled bool, timeout int) error {
1403+
controller, err := d.getController(ctx, clusterID)
1404+
if err != nil {
1405+
return errors.Wrap(err, "failed to get controller for cluster")
1406+
}
1407+
1408+
return controller.Controller().SetAutoFailover(ctx, &clustercontrol.SetAutoFailoverOptions{
1409+
Enabled: enabled,
1410+
Timeout: timeout,
1411+
})
1412+
}
1413+
14021414
func (d *Deployer) CreateCapellaLink(ctx context.Context, columnarID, linkName, clusterId, directID string) error {
14031415
return errors.New("docker deploy does not support create capella link")
14041416
}

deployment/localdeploy/deployer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,7 @@ func (d *Deployer) RebalanceCluster(ctx context.Context, clusterID string, nodes
279279
func (d *Deployer) KillCouchbase(ctx context.Context, clusterID string, nodes []string) error {
280280
return errors.New("localdeploy does not support killing couchbase process")
281281
}
282+
283+
func (d *Deployer) SetAutoFailover(ctx context.Context, clusterID string, enabled bool, timeout int) error {
284+
return errors.New("localdeploy does not support setting auto-failover")
285+
}

utils/clustercontrol/controller.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,20 @@ func (c *Controller) SetAppTelemetry(ctx context.Context, opts *AppTelemetryOpti
963963
return c.doFormPost(ctx, "/settings/appTelemetry", form, true, nil)
964964
}
965965

966+
type SetAutoFailoverOptions struct {
967+
Enabled bool
968+
Timeout int
969+
}
970+
971+
func (c *Controller) SetAutoFailover(ctx context.Context, opts *SetAutoFailoverOptions) error {
972+
form := make(url.Values)
973+
form.Add("enabled", strconv.FormatBool(opts.Enabled))
974+
if opts.Timeout > 0 {
975+
form.Add("timeout", strconv.Itoa(opts.Timeout))
976+
}
977+
return c.doFormPost(ctx, "/settings/autoFailover", form, true, nil)
978+
}
979+
966980
func (c *Controller) GetMetrics(ctx context.Context) (string, error) {
967981
var resp []byte
968982
err := c.doGet(ctx, "/metrics", &resp)

0 commit comments

Comments
 (0)