Skip to content

Commit

Permalink
Add downgrade commands
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyang.yl committed Sep 18, 2021
1 parent 58fb625 commit 85d3e7a
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
135 changes: 135 additions & 0 deletions etcdctl/ctlv3/command/downgrade_command.go
@@ -0,0 +1,135 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package command

import (
"errors"
"github.com/spf13/cobra"
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/pkg/v3/cobrautl"
)

// NewDowngradeCommand returns the cobra command for "downgrade".
func NewDowngradeCommand() *cobra.Command {
dc := &cobra.Command{
Use: "downgrade <TARGET_VERSION>",
Short: "Downgrade related commands",
}

dc.AddCommand(NewDowngradeValidateCommand())
dc.AddCommand(NewDowngradeEnableCommand())
dc.AddCommand(NewDowngradeCancelCommand())

return dc
}

// NewDowngradeValidateCommand returns the cobra command for "downgrade validate".
func NewDowngradeValidateCommand() *cobra.Command {
cc := &cobra.Command{
Use: "validate <TARGET_VERSION>",
Short: "Validate downgrade capability before starting downgrade",

Run: downgradeValidateCommandFunc,
}
return cc
}

// NewDowngradeEnableCommand returns the cobra command for "downgrade enable".
func NewDowngradeEnableCommand() *cobra.Command {
cc := &cobra.Command{
Use: "enable <TARGET_VERSION>",
Short: "Start a downgrade action to cluster",

Run: downgradeEnableCommandFunc,
}
return cc
}

// NewDowngradeCancelCommand returns the cobra command for "downgrade cancel".
func NewDowngradeCancelCommand() *cobra.Command {
cc := &cobra.Command{
Use: "cancel",
Short: "Cancel the ongoing downgrade action to cluster",

Run: downgradeCancelCommandFunc,
}
return cc
}

// downgradeValidateCommandFunc executes the "downgrade validate" command.
func downgradeValidateCommandFunc(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("TARGET_VERSION not provided"))
}
if len(args) > 1 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("too many arguments"))
}
targetVersion := args[0]

if len(targetVersion) == 0 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("member peer urls not provided"))
}

ctx, cancel := commandCtx(cmd)
cli := mustClientFromCmd(cmd)

resp, err := cli.Downgrade(ctx, int32(pb.DowngradeRequest_VALIDATE), targetVersion)
cancel()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

display.DowngradeValidate(*resp)
}

// downgradeEnableCommandFunc executes the "downgrade enable" command.
func downgradeEnableCommandFunc(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("TARGET_VERSION not provided"))
}
if len(args) > 1 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("too many arguments"))
}
targetVersion := args[0]

if len(targetVersion) == 0 {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, errors.New("member peer urls not provided"))
}

ctx, cancel := commandCtx(cmd)
cli := mustClientFromCmd(cmd)

resp, err := cli.Downgrade(ctx, int32(pb.DowngradeRequest_ENABLE), targetVersion)
cancel()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

display.DowngradeEnable(*resp)
}

// downgradeCancelCommandFunc executes the "downgrade cancel" command.
func downgradeCancelCommandFunc(cmd *cobra.Command, args []string) {
ctx, cancel := commandCtx(cmd)
cli := mustClientFromCmd(cmd)

resp, err := cli.Downgrade(ctx, int32(pb.DowngradeRequest_ENABLE), "")
cancel()
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}

display.DowngradeCancel(*resp)
}
10 changes: 10 additions & 0 deletions etcdctl/ctlv3/command/printer.go
Expand Up @@ -50,6 +50,10 @@ type printer interface {
EndpointHashKV([]epHashKV)
MoveLeader(leader, target uint64, r v3.MoveLeaderResponse)

DowngradeValidate(r v3.DowngradeResponse)
DowngradeEnable(r v3.DowngradeResponse)
DowngradeCancel(r v3.DowngradeResponse)

Alarm(v3.AlarmResponse)

RoleAdd(role string, r v3.AuthRoleAddResponse)
Expand Down Expand Up @@ -115,6 +119,9 @@ func (p *printerRPC) Alarm(r v3.AlarmResponse) { p.p((*pb.AlarmRespons
func (p *printerRPC) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) {
p.p((*pb.MoveLeaderResponse)(&r))
}
func (p *printerRPC) DowngradeValidate(r v3.DowngradeResponse) { p.p((*pb.DowngradeResponse)(&r)) }
func (p *printerRPC) DowngradeEnable(r v3.DowngradeResponse) { p.p((*pb.DowngradeResponse)(&r)) }
func (p *printerRPC) DowngradeCancel(r v3.DowngradeResponse) { p.p((*pb.DowngradeResponse)(&r)) }
func (p *printerRPC) RoleAdd(_ string, r v3.AuthRoleAddResponse) { p.p((*pb.AuthRoleAddResponse)(&r)) }
func (p *printerRPC) RoleGet(_ string, r v3.AuthRoleGetResponse) { p.p((*pb.AuthRoleGetResponse)(&r)) }
func (p *printerRPC) RoleDelete(_ string, r v3.AuthRoleDeleteResponse) {
Expand Down Expand Up @@ -160,6 +167,9 @@ func (p *printerUnsupported) EndpointStatus([]epStatus) { p.p(nil) }
func (p *printerUnsupported) EndpointHashKV([]epHashKV) { p.p(nil) }

func (p *printerUnsupported) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) { p.p(nil) }
func (p *printerUnsupported) DowngradeValidate(r v3.DowngradeResponse) { p.p(nil) }
func (p *printerUnsupported) DowngradeEnable(r v3.DowngradeResponse) { p.p(nil) }
func (p *printerUnsupported) DowngradeCancel(r v3.DowngradeResponse) { p.p(nil) }

func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string) {
hdr = []string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs", "Is Learner"}
Expand Down
10 changes: 10 additions & 0 deletions etcdctl/ctlv3/command/printer_simple.go
Expand Up @@ -176,6 +176,16 @@ func (s *simplePrinter) MoveLeader(leader, target uint64, r v3.MoveLeaderRespons
fmt.Printf("Leadership transferred from %s to %s\n", types.ID(leader), types.ID(target))
}

func (s *simplePrinter) DowngradeValidate(r v3.DowngradeResponse) {
fmt.Printf("Downgrade validate success, cluster version %s", r.Version)
}
func (s *simplePrinter) DowngradeEnable(r v3.DowngradeResponse) {
fmt.Printf("Downgrade enable success, cluster version %s", r.Version)
}
func (s *simplePrinter) DowngradeCancel(r v3.DowngradeResponse) {
fmt.Printf("Downgrade cancel success, cluster version %s", r.Version)
}

func (s *simplePrinter) RoleAdd(role string, r v3.AuthRoleAddResponse) {
fmt.Printf("Role %s created\n", role)
}
Expand Down
1 change: 1 addition & 0 deletions etcdctl/ctlv3/ctl.go
Expand Up @@ -97,6 +97,7 @@ func init() {
command.NewRoleCommand(),
command.NewCheckCommand(),
command.NewCompletionCommand(),
command.NewDowngradeCommand(),
)
}

Expand Down

0 comments on commit 85d3e7a

Please sign in to comment.