Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/remote/longhornctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func newCmdLonghornctl() *cobra.Command {
{
Message: "Operation Commands:",
Commands: []*cobra.Command{
subcmd.NewCmdMaintenance(globalOpts),
subcmd.NewCmdTrim(globalOpts),
subcmd.NewCmdExport(globalOpts),
subcmd.NewCmdChecksum(globalOpts),
Expand Down
133 changes: 133 additions & 0 deletions cmd/remote/subcmd/maintenance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package subcmd

import (
"context"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/longhorn/cli/pkg/consts"
"github.com/longhorn/cli/pkg/remote/maintenance"
"github.com/longhorn/cli/pkg/types"
"github.com/longhorn/cli/pkg/utils"
)

func NewCmdMaintenance(globalOpts *types.GlobalCmdOptions) *cobra.Command {
cmd := &cobra.Command{
Use: consts.SubCmdMaintenance,
Short: "Longhorn node maintenance operations",
}

utils.SetGlobalOptionsRemote(cmd, globalOpts)

cmd.AddCommand(newCmdEvictNode(globalOpts))
cmd.AddCommand(newCmdEvictDisk(globalOpts))

return cmd
}

func newCmdEvictNode(globalOpts *types.GlobalCmdOptions) *cobra.Command {
var nodeEvictor = maintenance.Evictor{}

cmd := &cobra.Command{
Use: consts.SubCmdEvictNode,
Short: "Evict all replicas from a node",
Long: `Prepare a Longhorn node for maintenance by cordoning the Kubernetes node, disabling replica scheduling on it and requesting eviction of all its replicas.

Specify the following option:
- --node-id: The name of the node to evict.

With --wait, the command blocks until no replicas remain scheduled on the node.
Once eviction is done, drain the node with the printed kubectl command before stopping or restarting it.`,
Example: `$ longhornctl maintenance evict-node --node-id="worker-1" --wait`,

PreRun: func(cmd *cobra.Command, args []string) {
nodeEvictor.Image = globalOpts.Image
nodeEvictor.ImageRegistry = globalOpts.ImageRegistry
nodeEvictor.ImagePullSecret = globalOpts.ImagePullSecret
nodeEvictor.KubeConfigPath = globalOpts.KubeConfigPath
nodeEvictor.NodeSelector = globalOpts.NodeSelector
nodeEvictor.Tolerations = globalOpts.Tolerations
nodeEvictor.Namespace = globalOpts.Namespace

utils.CheckErr(nodeEvictor.Validate())

logrus.Info("Initializing node evictor")
if err := nodeEvictor.Init(); err != nil {
utils.CheckErr(errors.Wrapf(err, "Failed to initialize node evictor for node %s", nodeEvictor.NodeID))
}
},

Run: func(cmd *cobra.Command, args []string) {
log := logrus.WithField("node", nodeEvictor.NodeID)

log.Info("Running node evictor")
if err := nodeEvictor.EvictNode(context.Background()); err != nil {
utils.CheckErr(errors.Wrapf(err, "Failed to evict node %s", nodeEvictor.NodeID))
}

log.Info("Completed node evictor")
},
}

utils.SetGlobalOptionsRemote(cmd, globalOpts)

cmd.Flags().StringVar(&nodeEvictor.NodeID, consts.CmdOptNodeId, "", "Name of the node to evict.")
cmd.Flags().BoolVar(&nodeEvictor.Wait, consts.CmdOptWait, false, "Wait until all replicas have left the node.")

return cmd
}

func newCmdEvictDisk(globalOpts *types.GlobalCmdOptions) *cobra.Command {
var diskEvictor = maintenance.Evictor{}

cmd := &cobra.Command{
Use: consts.SubCmdEvictDisk,
Short: "Evict all replicas from a disk",
Long: `Prepare a single Longhorn disk for maintenance by disabling replica scheduling on it and requesting eviction of all its replicas.

Specify the following options:
- --node-id: The name of the node the disk belongs to.
- --disk-uuid: The UUID of the disk to evict.

With --wait, the command blocks until no replicas remain scheduled on the disk.`,
Example: `$ longhornctl maintenance evict-disk --node-id="worker-1" --disk-uuid="a1b2c3d4-e5f6-7890-abcd-ef1234567890" --wait`,

PreRun: func(cmd *cobra.Command, args []string) {
diskEvictor.Image = globalOpts.Image
diskEvictor.ImageRegistry = globalOpts.ImageRegistry
diskEvictor.ImagePullSecret = globalOpts.ImagePullSecret
diskEvictor.KubeConfigPath = globalOpts.KubeConfigPath
diskEvictor.NodeSelector = globalOpts.NodeSelector
diskEvictor.Tolerations = globalOpts.Tolerations
diskEvictor.Namespace = globalOpts.Namespace

utils.CheckErr(diskEvictor.ValidateDiskEviction())

logrus.Info("Initializing disk evictor")
if err := diskEvictor.Init(); err != nil {
utils.CheckErr(errors.Wrapf(err, "Failed to initialize disk evictor for disk %s", diskEvictor.DiskUUID))
}
},

Run: func(cmd *cobra.Command, args []string) {
log := logrus.WithFields(logrus.Fields{"node": diskEvictor.NodeID, "disk": diskEvictor.DiskUUID})

log.Info("Running disk evictor")
if err := diskEvictor.EvictDisk(context.Background()); err != nil {
utils.CheckErr(errors.Wrapf(err, "Failed to evict disk %s", diskEvictor.DiskUUID))
}

log.Info("Completed disk evictor")
},
}

utils.SetGlobalOptionsRemote(cmd, globalOpts)

cmd.Flags().StringVar(&diskEvictor.NodeID, consts.CmdOptNodeId, "", "Name of the node the disk belongs to.")
cmd.Flags().StringVar(&diskEvictor.DiskUUID, consts.CmdOptDiskUUID, "", "UUID of the disk to evict.")
cmd.Flags().BoolVar(&diskEvictor.Wait, consts.CmdOptWait, false, "Wait until all replicas have left the disk.")

return cmd
}
21 changes: 14 additions & 7 deletions pkg/consts/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ const (

const (
// The first layer of subcommands (verb)
SubCmdCheck = "check"
SubCmdDiagnose = "diagnose"
SubCmdExport = "export"
SubCmdGet = "get"
SubCmdInstall = "install"
SubCmdTrim = "trim"
SubCmdCheck = "check"
SubCmdDiagnose = "diagnose"
SubCmdExport = "export"
SubCmdGet = "get"
SubCmdInstall = "install"
SubCmdMaintenance = "maintenance"
SubCmdTrim = "trim"

// The second layer of subcommands (noun)
SubCmdPreflight = "preflight"
SubCmdReplica = "replica"
SubCmdVolume = "volume"

// The third layer of subcommands (action to the previous layers)
SubCmdStop = "stop"
SubCmdStop = "stop"
SubCmdEvictNode = "evict-node"
SubCmdEvictDisk = "evict-disk"

// Other subcommands
SubCmdVersion = "version"
Expand Down Expand Up @@ -67,6 +70,10 @@ const (
CmdOptReplicaDryRun = "dry-run"
EnvReplicaDirectory = "REPLICA_DIR"
SubCmdRecover = "recover"

// Maintenance options
CmdOptDiskUUID = "disk-uuid"
CmdOptWait = "wait"
)

const (
Expand Down
Loading
Loading