Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add show/set etcd-confg commands #143

Merged
merged 1 commit into from
Apr 25, 2023
Merged
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
17 changes: 17 additions & 0 deletions states/etcd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/milvus-io/birdwatcher/states/etcd/remove"
"github.com/milvus-io/birdwatcher/states/etcd/repair"
"github.com/milvus-io/birdwatcher/states/etcd/set"
"github.com/milvus-io/birdwatcher/states/etcd/show"
)

Expand Down Expand Up @@ -46,6 +47,8 @@ func ShowCommand(cli clientv3.KV, basePath string) *cobra.Command {
// show collection-loaded
show.CollectionLoadedCommand(cli, basePath),

show.ConfigEtcdCommand(cli, basePath),

// v2.1 legacy commands
// show querycoord-tasks
show.QueryCoordTasks(cli, basePath),
Expand Down Expand Up @@ -77,6 +80,20 @@ func RepairCommand(cli clientv3.KV, basePath string) *cobra.Command {
return repairCmd
}

// SetCommand, returns etcd set commands.
func SetCommand(cli clientv3.KV, instanceName string, metaPath string) *cobra.Command {
setCmd := &cobra.Command{
Use: "set",
}

setCmd.AddCommand(
// by-dev/config not by-dev/meta/config
set.EtcdConfigCommand(cli, instanceName),
)

return setCmd
}

// RemoveCommand returns etcd remove commands.
// WARNING this command shall be used with EXTRA CARE!
func RemoveCommand(cli clientv3.KV, basePath string) *cobra.Command {
Expand Down
36 changes: 36 additions & 0 deletions states/etcd/common/config_etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package common

import (
"context"
"fmt"
"path"

clientv3 "go.etcd.io/etcd/client/v3"
)

func ListEtcdConfigs(ctx context.Context, cli clientv3.KV, basePath string) (keys, values []string, err error) {
prefix := path.Join(basePath, "config")
return listEtcdConfigsByPrefix(ctx, cli, prefix)
}

func listEtcdConfigsByPrefix(ctx context.Context, cli clientv3.KV, prefix string) (keys, values []string, err error) {
resp, err := cli.Get(ctx, prefix, clientv3.WithPrefix())
if err != nil {
return nil, nil, err
}

keys = make([]string, 0, len(resp.Kvs))
values = make([]string, 0, len(resp.Kvs))
for _, kv := range resp.Kvs {
keys = append(keys, string(kv.Key))
values = append(values, string(kv.Value))
}
return keys, values, nil
}

func SetEtcdConfig(ctx context.Context, cli clientv3.KV, basePath string, key, value string) error {
prefix := path.Join(basePath, "config")
fmt.Println(path.Join(prefix, key), value)
_, err := cli.Put(ctx, path.Join(prefix, key), value)
return err
}
50 changes: 50 additions & 0 deletions states/etcd/set/etcd_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package set

import (
"context"
"fmt"

"github.com/milvus-io/birdwatcher/states/etcd/common"
"github.com/spf13/cobra"

clientv3 "go.etcd.io/etcd/client/v3"
)

// EtcdConfigCommand returns set etcd-config command.
func EtcdConfigCommand(cli clientv3.KV, basePath string) *cobra.Command {
cmd := &cobra.Command{
Use: "config-etcd",
Short: "list configuations set by etcd source",
Run: func(cmd *cobra.Command, args []string) {
key, err := cmd.Flags().GetString("key")
if err != nil {
fmt.Println(err.Error())
return
}
value, err := cmd.Flags().GetString("value")
if err != nil {
fmt.Println(err.Error())
return
}
if key == "" || value == "" {
fmt.Println("key & value cannot be empty")
return
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = common.SetEtcdConfig(ctx, cli, basePath, key, value)
if err != nil {
fmt.Println("failed to set etcd config item,", err.Error())
return
}

fmt.Println("etcd config set.")
},
}

cmd.Flags().String("key", "", "etcd config key")
cmd.Flags().String("value", "", "etcd config value")
return cmd
}
34 changes: 34 additions & 0 deletions states/etcd/show/config_etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package show

import (
"context"
"fmt"

"github.com/milvus-io/birdwatcher/states/etcd/common"
"github.com/spf13/cobra"
clientv3 "go.etcd.io/etcd/client/v3"
)

// ConfigEtcdCommand return show config-etcd command.
func ConfigEtcdCommand(cli clientv3.KV, basePath string) *cobra.Command {
cmd := &cobra.Command{
Use: "config-etcd",
Short: "list configuations set by etcd source",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

keys, values, err := common.ListEtcdConfigs(ctx, cli, basePath)
if err != nil {
fmt.Println("failed to list configurations from etcd", err.Error())
return
}

for i, key := range keys {
fmt.Printf("Key: %s, Value: %s\n", key, values[i])
}
},
}

return cmd
}
2 changes: 2 additions & 0 deletions states/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (s *instanceState) SetupCommands() {
etcd.RepairCommand(cli, basePath),
// remove [subcommand] options...
etcd.RemoveCommand(cli, basePath),
// set [subcommand] options...
etcd.SetCommand(cli, instanceName, metaPath),

// backup [component]
getBackupEtcdCmd(cli, basePath),
Expand Down