-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commands to manage/query etcd cluster
Used already existing protobufs for that. Commands: `talosctl etcd members -n <node>` `talosctl etcd leave -n <node>` `talosctl etcd forfeit-leadership -n <node>` Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
- Loading branch information
Showing
7 changed files
with
556 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package talos | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/talos-systems/talos/pkg/machinery/api/machine" | ||
"github.com/talos-systems/talos/pkg/machinery/client" | ||
) | ||
|
||
// etcdCmd represents the etcd command. | ||
var etcdCmd = &cobra.Command{ | ||
Use: "etcd", | ||
Short: "Manage etcd", | ||
Long: ``, | ||
} | ||
|
||
var etcdLeaveCmd = &cobra.Command{ | ||
Use: "leave", | ||
Short: "Tell nodes to leave etcd cluster", | ||
Long: ``, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
_, err := c.MachineClient.EtcdLeaveCluster(ctx, &machine.EtcdLeaveClusterRequest{}) | ||
|
||
return err | ||
}) | ||
}, | ||
} | ||
|
||
var etcdForfeitLeadershipCmd = &cobra.Command{ | ||
Use: "forfeit-leadership", | ||
Short: "Tell node to forfeit etcd cluster leadership", | ||
Long: ``, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
_, err := c.MachineClient.EtcdForfeitLeadership(ctx, &machine.EtcdForfeitLeadershipRequest{}) | ||
|
||
return err | ||
}) | ||
}, | ||
} | ||
|
||
var etcdMemberListCmd = &cobra.Command{ | ||
Use: "members", | ||
Short: "Get the list of etcd cluster members", | ||
Long: ``, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return WithClient(func(ctx context.Context, c *client.Client) error { | ||
response, err := c.MachineClient.EtcdMemberList(ctx, &machine.EtcdMemberListRequest{ | ||
QueryLocal: true, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error getting members: %w", err) | ||
} | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) | ||
node := "" | ||
pattern := "%s\t" | ||
|
||
for i, message := range response.Messages { | ||
if message.Metadata != nil && message.Metadata.Hostname != "" { | ||
node = message.Metadata.Hostname | ||
} | ||
|
||
if len(message.Members) == 0 { | ||
continue | ||
} | ||
|
||
if i == 0 { | ||
if node != "" { | ||
fmt.Fprintln(w, "NODE\tMEMBERS") | ||
pattern = "%s\t%s\n" | ||
} else { | ||
fmt.Fprintln(w, "MEMBERS") | ||
} | ||
} | ||
|
||
args := []interface{}{strings.Join(message.Members, ",")} | ||
if node != "" { | ||
args = append([]interface{}{node}, args...) | ||
} | ||
|
||
fmt.Fprintf(w, pattern, args...) | ||
|
||
} | ||
|
||
return w.Flush() | ||
}) | ||
}, | ||
} | ||
|
||
func init() { | ||
etcdCmd.AddCommand(etcdLeaveCmd, etcdForfeitLeadershipCmd, etcdMemberListCmd) | ||
addCommand(etcdCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
// +build integration_cli | ||
|
||
package cli | ||
|
||
import ( | ||
"github.com/talos-systems/talos/internal/integration/base" | ||
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine" | ||
) | ||
|
||
// EtcdSuite verifies etcd command. | ||
type EtcdSuite struct { | ||
base.CLISuite | ||
} | ||
|
||
// SuiteName ... | ||
func (suite *EtcdSuite) SuiteName() string { | ||
return "cli.EtcdSuite" | ||
} | ||
|
||
// TestMembers etcd members should have some output. | ||
func (suite *EtcdSuite) TestMembers() { | ||
suite.RunCLI([]string{"etcd", "members", "--nodes", suite.RandomDiscoveredNode(machine.TypeControlPlane)}) // default checks for stdout not empty | ||
} | ||
|
||
// TestForfeitLeadership etcd forfeit-leadership check. | ||
func (suite *EtcdSuite) TestForfeitLeadership() { | ||
suite.RunCLI([]string{"etcd", "forfeit-leadership", "--nodes", suite.RandomDiscoveredNode(machine.TypeControlPlane)}, | ||
base.StdoutEmpty(), | ||
) | ||
} | ||
|
||
func init() { | ||
allSuites = append(allSuites, new(EtcdSuite)) | ||
} |
Oops, something went wrong.