Skip to content

Commit

Permalink
--story=115483357 Feat: add kv-ctl example
Browse files Browse the repository at this point in the history
  • Loading branch information
ifooth committed Dec 11, 2023
1 parent e64dc16 commit 9f28636
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 9 deletions.
15 changes: 13 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
type Client interface {
// PullFiles pull files from remote
PullFiles(app string, opts ...option.AppOption) (*types.Release, error)
// Pull Key Value from remote
Get(app string, key string, opts ...option.AppOption) (string, error)
// AddWatcher add a watcher to client
AddWatcher(callback option.Callback, app string, opts ...option.AppOption) error
Expand Down Expand Up @@ -227,8 +228,18 @@ func (c *client) Get(app string, key string, opts ...option.AppOption) (string,
req := &pbfs.GetKvValueReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.BizID,
Token: c.opts.Token,
Key: key,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.Labels,
Uid: c.opts.UID,
},
Token: c.opts.Token,
Key: key,
}
req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
}
resp, err := c.upstream.GetKvValue(vas, req)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bscp sdk examples
============

## 示例代码
* [kv-ctl](./kv-ctl) - 拉取 kv 型配置命令行示例
* [pull-file](./pull-file) - 拉取 file 型配置
* [watch-file](./watch-file) - 拉取 file 型配置并监听配置变更
* [pull-kv](./pull-kv) - 拉取 kv 型配置
Expand All @@ -19,6 +20,8 @@ export BSCP_TOKEN="xxx"
export BSCP_BIZ="2"
# 当前服务名称
export BSCP_APP="app-test"
# 当前实例标签, 灰度发布时使用
export BSCP_LABELS='{"region": "sz"}'
```

运行示例
Expand Down
176 changes: 176 additions & 0 deletions examples/kv-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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.
*/

// watch file example for bscp sdk
package main

import (
"context"
"encoding/json"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"bscp.io/pkg/logs"
"github.com/spf13/cobra"

"github.com/TencentBlueKing/bscp-go/cli/config"
"github.com/TencentBlueKing/bscp-go/client"
"github.com/TencentBlueKing/bscp-go/option"
"github.com/TencentBlueKing/bscp-go/types"
)

var rootCmd = &cobra.Command{
Use: "kv-ctl",
Short: "bscp kv ctl",
Run: func(cmd *cobra.Command, args []string) {
execute()
},
}

var (
watchMode bool
keys string
logEnabled bool
)

func init() {
rootCmd.PersistentFlags().BoolVarP(&watchMode, "watch", "w", false, "use watch mode")
rootCmd.PersistentFlags().BoolVarP(&logEnabled, "log.enabled", "", false, "enable log")
rootCmd.PersistentFlags().StringVarP(&keys, "keys", "k", "", "use commas to separate, like key1,key2. (watch mode empty key will get all values)")
}

func main() {
rootCmd.Execute()
}

func execute() {
if logEnabled {
logs.InitLogger(logs.LogConfig{ToStdErr: true, LogLineMaxSize: 1000})
}

// 初始化配置信息, 按需修改
bizStr := os.Getenv("BSCP_BIZ")
biz, err := strconv.ParseInt(bizStr, 10, 64)
if err != nil {
logs.Errorf(err.Error())
os.Exit(1)
}

labelsStr := os.Getenv("BSCP_LABELS")
labels := map[string]string{}
if labelsStr != "" {
json.Unmarshal([]byte(labelsStr), &labels) // nolint
}

conf := &config.ClientConfig{
FeedAddrs: strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ","),
Biz: uint32(biz),
Token: os.Getenv("BSCP_TOKEN"),
Labels: labels,
}

bscp, err := client.New(
option.FeedAddrs(conf.FeedAddrs),
option.BizID(conf.Biz),
option.Token(conf.Token),
option.Labels(conf.Labels),
)
if err != nil {
logs.Errorf(err.Error())
os.Exit(1)
}

appName := os.Getenv("BSCP_APP")
opts := []option.AppOption{}
keySlice := strings.Split(keys, ",")
if watchMode {
if err = watchAppKV(bscp, appName, keySlice, opts); err != nil {
logs.Errorf(err.Error())
os.Exit(1)
}
} else {
result := map[string]string{}

for _, key := range keySlice {
value, err := bscp.Get(appName, key, opts...)
if err != nil {
continue
}
result[key] = value
}

json.NewEncoder(os.Stdout).Encode(result) // nolint
}
}

type watcher struct {
bscp client.Client
app string
keyMap map[string]struct{}
}

// callback watch 回调函数
func (w *watcher) callback(release *types.Release) error {
result := map[string]string{}

// kv 列表, 可以读取值
for _, item := range release.KvItems {
value, err := w.bscp.Get(w.app, item.Key)
if err != nil {
logs.Errorf("get value failed: %d, %v, err: %s", release.ReleaseID, item.Key, err)
continue
}
logs.Infof("get value success: %d, %v, %s", release.ReleaseID, item.Key, value)

// key匹配或者为空时,输出
if _, ok := w.keyMap[item.Key]; ok || len(keys) == 0 {
result[item.Key] = value
}
}

json.NewEncoder(os.Stdout).Encode(result) // nolint

return nil
}

// watchAppKV watch 服务版本
func watchAppKV(bscp client.Client, app string, keys []string, opts []option.AppOption) error {
keyMap := map[string]struct{}{}
for _, v := range keys {
keyMap[v] = struct{}{}
}

w := watcher{
bscp: bscp,
app: app,
keyMap: keyMap,
}
err := bscp.AddWatcher(w.callback, app, opts...)
if err != nil {
return err
}

if err := bscp.StartWatch(); err != nil {
return err
}

ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()

<-ctx.Done()
bscp.StopWatch()

return nil
}
9 changes: 9 additions & 0 deletions examples/pull-kv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"encoding/json"
"os"
"strconv"
"strings"
Expand All @@ -36,16 +37,24 @@ func main() {
os.Exit(1)
}

labelsStr := os.Getenv("BSCP_LABELS")
labels := map[string]string{}
if labelsStr != "" {
json.Unmarshal([]byte(labelsStr), &labels) // nolint
}

conf := &config.ClientConfig{
FeedAddrs: strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ","),
Biz: uint32(biz),
Token: os.Getenv("BSCP_TOKEN"),
Labels: labels,
}

bscp, err := client.New(
option.FeedAddrs(conf.FeedAddrs),
option.BizID(conf.Biz),
option.Token(conf.Token),
option.Labels(conf.Labels),
)
if err != nil {
logs.Errorf(err.Error())
Expand Down
29 changes: 26 additions & 3 deletions examples/watch-kv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package main

import (
"context"
"encoding/json"
"os"
"os/signal"
"strconv"
Expand All @@ -40,16 +41,24 @@ func main() {
os.Exit(1)
}

labelsStr := os.Getenv("BSCP_LABELS")
labels := map[string]string{}
if labelsStr != "" {
json.Unmarshal([]byte(labelsStr), &labels) // nolint
}

conf := &config.ClientConfig{
FeedAddrs: strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ","),
Biz: uint32(biz),
Token: os.Getenv("BSCP_TOKEN"),
Labels: labels,
}

bscp, err := client.New(
option.FeedAddrs(conf.FeedAddrs),
option.BizID(conf.Biz),
option.Token(conf.Token),
option.Labels(conf.Labels),
)
if err != nil {
logs.Errorf(err.Error())
Expand All @@ -64,20 +73,34 @@ func main() {
}
}

type watcher struct {
bscp client.Client
app string
}

// callback watch 回调函数
func callback(release *types.Release) error {
func (w *watcher) callback(release *types.Release) error {

// kv 列表, 可以读取值
for _, item := range release.KvItems {
logs.Infof("get event: %d, %v", release.ReleaseID, item.Key)
value, err := w.bscp.Get(w.app, item.Key)
if err != nil {
logs.Errorf("get value failed: %d, %v, err: %s", release.ReleaseID, item.Key, err)
continue
}
logs.Infof("get value success: %d, %v, %s", release.ReleaseID, item.Key, value)
}

return nil
}

// watchAppKV watch 服务版本
func watchAppKV(bscp client.Client, app string, opts []option.AppOption) error {
err := bscp.AddWatcher(callback, app, opts...)
w := watcher{
bscp: bscp,
app: app,
}
err := bscp.AddWatcher(w.callback, app, opts...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ require (
k8s.io/klog/v2 v2.100.1 // indirect
)

replace bscp.io => github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231209035018-dc424ab7702d
replace bscp.io => github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231211134924-1689a2caa08a
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20230912015319-acb7495967f5 h1:8B8di9Hxgh9aIIMLVkIhQnsJmAwKd1VxgjfHlvJ+864=
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20230912015319-acb7495967f5/go.mod h1:/9h4LrWU1cVp46TF0DW5sUrqLtUyyMW0A9mnqhCTavA=
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231209035018-dc424ab7702d h1:sLU64fWDcyHDthTboqOZG6RWogxqdFWlX6T4nvdBorU=
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231209035018-dc424ab7702d/go.mod h1:OVd7yUfbD8gsXOJagK16TECr/hAGrQHogQpRjqLYdNE=
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231211134924-1689a2caa08a h1:InKLUNbaTzv585f5vASF6+5DRyCzJ8bESv8658MgW94=
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20231211134924-1689a2caa08a/go.mod h1:OVd7yUfbD8gsXOJagK16TECr/hAGrQHogQpRjqLYdNE=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down
2 changes: 1 addition & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (rs ReconnectSignal) String() string {
// Release bscp 服务版本
type Release struct {
ReleaseID uint32 `json:"release_id"`
FileItems []*ConfigItemFile `json:"items"`
FileItems []*ConfigItemFile `json:"files"`
KvItems []*sfs.KvMetaV1 `json:"kvs"`
PreHook *pbhook.HookSpec `json:"pre_hook"`
PostHook *pbhook.HookSpec `json:"post_hook"`
Expand Down

0 comments on commit 9f28636

Please sign in to comment.