command

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ResourceNamespace = "namespace"
	ResourceCluster   = "cluster"
	ResourceShard     = "shard"
	ResourceNode      = "node"
)

Variables

View Source
var CreateCommand = &cobra.Command{
	Use:   "create",
	Short: "Create a resource",
	Example: `
# Create a namespace
kvctl create namespace <namespace>

# Create a cluster in the namespace
kvctl create cluster <cluster> -n <namespace> --replica 1 --nodes 127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381

# Create a shard in the cluster
kvctl create shard -n <namespace> -c <cluster> --nodes 127.0.0.1:6379,127.0.0.1:6380

# Create nodes in the cluster
kvctl create node 127.0.0.1:6379 -n <namespace> -c <cluster> --shard <shard>
`,
	PreRunE: createPreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		switch strings.ToLower(args[0]) {
		case ResourceNamespace:
			if len(args) < 2 {
				return errors.New("missing namespace name")
			}
			return createNamespace(client, args[1])
		case ResourceCluster:
			if len(args) < 2 {
				return errors.New("missing cluster name")
			}
			createOptions.cluster = args[1]
			return createCluster(client, &createOptions)
		case ResourceShard:
			return createShard(client, &createOptions)
		case ResourceNode:
			if len(args) < 2 {
				return errors.New("missing node address")
			}
			createOptions.nodes = []string{args[1]}
			return createNodes(client, &createOptions)
		default:
			return errors.New("unsupported resource type, please specify one of [namespace, cluster, shard, nodes]")
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var DeleteCommand = &cobra.Command{
	Use:   "delete",
	Short: "Delete a resource",
	Example: `
# Delete a namespace
kvctl delete namespace <namespace>

# Delete a cluster in the namespace
kvctl delete cluster <cluster> -n <namespace>

# Delete a shard in the cluster
kvctl delete shard <shard> -n <namespace> -c <cluster>

# Delete a node in the cluster
kvctl delete node <node_id> -n <namespace> -c <cluster> --shard <shard>
`,
	PreRunE: deletePreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		resource := strings.ToLower(args[0])
		if len(args) < 2 {
			return fmt.Errorf("missing resource name")
		}
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		switch resource {
		case ResourceNamespace:
			namespace := args[1]
			return deleteNamespace(client, namespace)
		case ResourceCluster:
			deleteOptions.cluster = args[1]
			return deleteCluster(client, &deleteOptions)
		case ResourceShard:
			shard, err := strconv.Atoi(args[1])
			if err != nil {
				return err
			}
			deleteOptions.shard = shard
			return deleteShard(client, &deleteOptions)
		case ResourceNode:
			nodeID := args[1]
			return deleteNode(client, &deleteOptions, nodeID)
		default:
			return fmt.Errorf("unsupported resource type %s", resource)
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var FailoverCommand = &cobra.Command{
	Use:   "failover",
	Short: "Failover the master of a shard",
	Example: `
# Failover the master of a shard
kvctl failover shard <shard_index> -n <namespace> -c <cluster>

# Failover the master of a shard with preferred slave
kvctl failover shard <shard_index> --preferred <node_id> -n <namespace> -c <cluster>
`,
	PreRunE: failoverPreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return fmt.Errorf("missing shard index, plese specify the shard index")
		}
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		resource := args[0]
		switch resource {
		case "shard":
			shardIndex, err := strconv.Atoi(args[1])
			if err != nil {
				return fmt.Errorf("invalid shard index: %s", args[1])
			}
			return failoverShard(client, &failoverOptions, shardIndex)
		default:
			return fmt.Errorf("unsupported resource type: %s", resource)
		}
	},
}
View Source
var GetCommand = &cobra.Command{
	Use:   "get",
	Short: "Get a resource",
	Example: `
# Get a cluster 
kvctl get cluster <cluster> -n <namespace>
`,
	PreRunE: getPreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		if len(args) < 2 {
			return fmt.Errorf("missing resource name, must be one of [cluster, shard]")
		}
		resource := strings.ToLower(args[0])
		switch resource {
		case "cluster":
			getOptions.cluster = args[1]
			return getCluster(client, &getOptions)
		default:
			return fmt.Errorf("unsupported resource type %s", resource)
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var ImportCommand = &cobra.Command{
	Use:   "import",
	Short: "Import data from a cluster",
	Example: `
# Import a cluster from nodes
kvctl import cluster <cluster> --nodes 127.0.0.1:6379,127.0.0.1:6380
`,
	PreRunE: importPreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		resource := strings.ToLower(args[0])
		switch resource {
		case ResourceCluster:
			importOptions.cluster = args[1]
			return importCluster(client, &importOptions)
		default:
			return fmt.Errorf("unsupported resource type: %s", resource)
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var ListCommand = &cobra.Command{
	Use:   "list",
	Short: "Display all resources",
	Example: `
# Display all namespaces 
kvctl list namespaces

# Display all clusters in the namespace
kvctl list clusters -n <namespace>

# Display all nodes in the cluster
kvctl list nodes -n <namespace> -c <cluster>
`,
	ValidArgs: []string{"namespaces", "clusters", "shards", "nodes"},
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		switch strings.ToLower(args[0]) {
		case "namespaces":
			return listNamespace(client)
		case "clusters":
			return listClusters(client)
		default:
			return fmt.Errorf("unsupported resource type %s", args[0])
		}
	},
	PreRunE:       listPreRun,
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var MigrateCommand = &cobra.Command{
	Use:   "migrate",
	Short: "Migrate slot to another node",
	Example: `
# Migrate slot between cluster shards 
kvctl migrate slot <slot> --target <target_shard_index> -n <namespace> -c <cluster>
`,
	PreRunE: migrationPreRun,
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		resource := strings.ToLower(args[0])
		switch resource {
		case "slot":
			return migrateSlot(client, &migrateOptions)
		default:
			return fmt.Errorf("unsupported resource type: %s", resource)
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}
View Source
var RaftCommand = &cobra.Command{
	Use:   "raft",
	Short: "Raft operations",
	Example: `
# Display all memberships in the cluster
kvctl raft list peers

# Add a node to the cluster
kvctl raft add peer <node_id> <node_address>

# Remove a node from the cluster
kvctl raft remove peer <node_id>
`,
	ValidArgs: []string{raftCommandList, raftCommandAdd, raftCommandRemove},
	RunE: func(cmd *cobra.Command, args []string) error {
		host, _ := cmd.Flags().GetString("host")
		client := newClient(host)
		switch strings.ToLower(args[0]) {
		case raftCommandList:
			if len(args) < 2 || args[1] != "peers" {
				return fmt.Errorf("unsupported openeration: '%s' in raft command", args[1])
			}
			return listRaftPeers(client)
		case raftCommandAdd, raftCommandRemove:
			if len(args) < 2 {
				return errors.New("missing 'peer' in raft command")
			}
			if args[1] != "peer" {
				return fmt.Errorf("unsupported openeration: '%s' in raft command", args[1])
			}
			if len(args) < 3 {
				return fmt.Errorf("missing node_id and node_address")
			}
			id, err := strconv.ParseUint(args[2], 10, 64)
			if err != nil {
				return fmt.Errorf("invalid node_id: %s", args[1])
			}
			if args[0] == raftCommandAdd {
				if len(args) < 4 {
					return fmt.Errorf("missing node_address")
				}
				address := args[3]
				if _, err := url.Parse(address); err != nil {
					return fmt.Errorf("invalid node_address: %s", address)
				}
				return addRaftPeer(client, id, address)
			} else {
				return removeRaftPeer(client, id)
			}
		default:
			return fmt.Errorf("unsupported openeration: '%s' in raft command", args[0])
		}
	},
	SilenceUsage:  true,
	SilenceErrors: true,
}

Functions

This section is empty.

Types

type CreateOptions

type CreateOptions struct {
	// contains filtered or unexported fields
}

type DeleteOptions

type DeleteOptions struct {
	// contains filtered or unexported fields
}

type ErrorMessage

type ErrorMessage struct {
	Message string `json:"message"`
}

type FailoverOptions

type FailoverOptions struct {
	// contains filtered or unexported fields
}

type GetOptions

type GetOptions struct {
	// contains filtered or unexported fields
}

type ImportOptions

type ImportOptions struct {
	// contains filtered or unexported fields
}

type MigrationOptions

type MigrationOptions struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL