Skip to content

Commit

Permalink
rpk: make move-cancel available and hide movement-cancel
Browse files Browse the repository at this point in the history
Originally 'movement-cancel' is used to cancel ongoing partition
movements. This commit adds 'move-cancel' to align with the wording
while it provide the same functionality and hides 'movement-cancel'.
  • Loading branch information
daisukebe committed Sep 9, 2023
1 parent e422991 commit fb631f1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 44 deletions.
98 changes: 54 additions & 44 deletions src/go/rpk/pkg/cli/cluster/partitions/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,80 @@ import (
"github.com/spf13/cobra"
)

type movementCancelHandler struct {
fs afero.Fs
p *config.Params
node int
noConfirm bool
}

func newMovementCancelCommand(fs afero.Fs, p *config.Params) *cobra.Command {
var (
node int
noConfirm bool
)
m := &movementCancelHandler{
fs: fs,
p: p,
}
cmd := &cobra.Command{
Use: "movement-cancel",
Short: "Cancel ongoing partition movements",
Use: "move-cancel",
Aliases: []string{"alter-cancel"},
Short: "Cancel ongoing partition movements",
Long: `Cancel ongoing partition movements.
By default, this command cancels all the partition movements in the cluster.
To ensure that you do not accidentally cancel all partition movements, this
command prompts users for confirmation before issuing the cancellation request.
You can use "--no-confirm" to disable the confirmation prompt:
rpk cluster partitions movement-cancel --no-confirm
rpk cluster partitions move-cancel --no-confirm
If "--node" is set, this command will only stop the partition movements
occurring in the specified node:
rpk cluster partitions movement-cancel --node 1
rpk cluster partitions move-cancel --node 1
`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
p, err := p.LoadVirtualProfile(fs)
out.MaybeDie(err, "unable to load config: %v", err)
out.CheckExitCloudAdmin(p)
Run: m.runMovementCancel,
}
cmd.Flags().IntVar(&m.node, "node", -1, "ID of a specific node on which to cancel ongoing partition movements")
cmd.Flags().BoolVar(&m.noConfirm, "no-confirm", false, "Disable confirmation prompt")
return cmd
}

cl, err := adminapi.NewClient(fs, p)
out.MaybeDie(err, "unable to initialize admin client: %v", err)
func (m *movementCancelHandler) runMovementCancel(cmd *cobra.Command, _ []string) {
p, err := m.p.LoadVirtualProfile(m.fs)
out.MaybeDie(err, "unable to load config: %v", err)
out.CheckExitCloudAdmin(p)

var movements []adminapi.PartitionsMovementResult
if node >= 0 {
if !noConfirm {
confirmed, err := out.Confirm("Confirm cancellation of partition movements in node %v?", node)
out.MaybeDie(err, "unable to confirm partition movements cancel: %v", err)
if !confirmed {
out.Exit("Command execution canceled.")
}
}
movements, err = cl.CancelNodePartitionsMovement(cmd.Context(), node)
out.MaybeDie(err, "unable to cancel partition movements in node %v: %v", node, err)
} else {
if !noConfirm {
confirmed, err := out.Confirm("Confirm cancellation of all partition movements in the cluster?")
out.MaybeDie(err, "unable to confirm partition movements cancel: %v", err)
if !confirmed {
out.Exit("Command execution canceled.")
}
}
movements, err = cl.CancelAllPartitionsMovement(cmd.Context())
out.MaybeDie(err, "unable to cancel partition movements: %v", err)
}
cl, err := adminapi.NewClient(m.fs, p)
out.MaybeDie(err, "unable to initialize admin client: %v", err)

if len(movements) == 0 {
fmt.Println("There are no ongoing partition movements to cancel")
return
var movements []adminapi.PartitionsMovementResult
if m.node >= 0 {
if !m.noConfirm {
confirmed, err := out.Confirm("Confirm cancellation of partition movements in node %v?", m.node)
out.MaybeDie(err, "unable to confirm partition movements cancel: %v", err)
if !confirmed {
out.Exit("Command execution canceled.")
}
}
movements, err = cl.CancelNodePartitionsMovement(cmd.Context(), m.node)
out.MaybeDie(err, "unable to cancel partition movements in node %v: %v", m.node, err)
} else {
if !m.noConfirm {
confirmed, err := out.Confirm("Confirm cancellation of all partition movements in the cluster?")
out.MaybeDie(err, "unable to confirm partition movements cancel: %v", err)
if !confirmed {
out.Exit("Command execution canceled.")
}
printMovementsResult(movements)
},
}
movements, err = cl.CancelAllPartitionsMovement(cmd.Context())
out.MaybeDie(err, "unable to cancel partition movements: %v", err)
}
cmd.Flags().IntVar(&node, "node", -1, "ID of a specific node on which to cancel ongoing partition movements")
cmd.Flags().BoolVar(&noConfirm, "no-confirm", false, "Disable confirmation prompt")
return cmd

if len(movements) == 0 {
fmt.Println("There are no ongoing partition movements to cancel")
return
}
printMovementsResult(movements)
}

func printMovementsResult(movements []adminapi.PartitionsMovementResult) {
Expand Down
38 changes: 38 additions & 0 deletions src/go/rpk/pkg/cli/cluster/partitions/cancel_hidden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package partitions

import (
"github.com/redpanda-data/redpanda/src/go/rpk/pkg/config"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)

func newMovementCancelCommandHidden(fs afero.Fs, p *config.Params) *cobra.Command {
m := &movementCancelHandler{
fs: fs,
p: p,
}
cmd := &cobra.Command{
Use: "movement-cancel",
Short: "Cancel ongoing partition movements",
Hidden: true,
Long: `Cancel ongoing partition movements.
By default, this command cancels all the partition movements in the cluster.
To ensure that you do not accidentally cancel all partition movements, this
command prompts users for confirmation before issuing the cancellation request.
You can use "--no-confirm" to disable the confirmation prompt:
rpk cluster partitions movement-cancel --no-confirm
If "--node" is set, this command will only stop the partition movements
occurring in the specified node:
rpk cluster partitions movement-cancel --node 1
`,
Args: cobra.ExactArgs(0),
Run: m.runMovementCancel,
}
cmd.Flags().IntVar(&m.node, "node", -1, "ID of a specific node on which to cancel ongoing partition movements")
cmd.Flags().BoolVar(&m.noConfirm, "no-confirm", false, "Disable confirmation prompt")
return cmd
}
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cluster/partitions/partitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewPartitionsCommand(fs afero.Fs, p *config.Params) *cobra.Command {
cmd.AddCommand(
newBalancerStatusCommand(fs, p),
newMovementCancelCommand(fs, p),
newMovementCancelCommandHidden(fs, p),
newListPartitionMovementsCommand(fs, p),
)
return cmd
Expand Down

0 comments on commit fb631f1

Please sign in to comment.