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

refactor: refine client opts #45

Merged
merged 10 commits into from
Dec 15, 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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ linters-settings:
- name: range
- name: receiver-naming
- name: time-naming
# - name: unexported-return
- name: unexported-return
- name: var-declaration
# - name: var-naming
75 changes: 37 additions & 38 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ import (
"github.com/TencentBlueKing/bscp-go/internal/util"
"github.com/TencentBlueKing/bscp-go/internal/watch"
"github.com/TencentBlueKing/bscp-go/logger"
"github.com/TencentBlueKing/bscp-go/option"
"github.com/TencentBlueKing/bscp-go/types"
)

// Client bscp client method
type Client interface {
// PullFiles pull files from remote
PullFiles(app string, opts ...option.AppOption) (*types.Release, error)
PullFiles(app string, opts ...types.AppOption) (*types.Release, error)
// Pull Key Value from remote
Get(app string, key string, opts ...option.AppOption) (string, error)
Get(app string, key string, opts ...types.AppOption) (string, error)
// AddWatcher add a watcher to client
AddWatcher(callback option.Callback, app string, opts ...option.AppOption) error
AddWatcher(callback types.Callback, app string, opts ...types.AppOption) error
// StartWatch start watch
StartWatch() error
// StopWatch stop watch
Expand All @@ -53,22 +52,22 @@ type Client interface {
// Client is the bscp client
type client struct {
pairs map[string]string
opts option.ClientOptions
opts options
fingerPrint sfs.FingerPrint
watcher *watch.Watcher
upstream upstream.Upstream
}

// New return a bscp client instance
func New(opts ...option.ClientOption) (Client, error) {
clientOpt := &option.ClientOptions{}
func New(opts ...Option) (Client, error) {
clientOpt := &options{}
fp, err := sfs.GetFingerPrint()
if err != nil {
return nil, fmt.Errorf("get instance fingerprint failed, err: %s", err.Error())
}
logger.Info("instance fingerprint", slog.String("fingerprint", fp.Encode()))
clientOpt.Fingerprint = fp.Encode()
clientOpt.UID = clientOpt.Fingerprint
clientOpt.fingerprint = fp.Encode()
clientOpt.uid = clientOpt.fingerprint
for _, opt := range opts {
if e := opt(clientOpt); e != nil {
return nil, e
Expand All @@ -80,8 +79,8 @@ func New(opts ...option.ClientOption) (Client, error) {
pairs[constant.SideUserKey] = "TODO-USER"
// add finger printer
mh := sfs.SidecarMetaHeader{
BizID: clientOpt.BizID,
Fingerprint: clientOpt.Fingerprint,
BizID: clientOpt.bizID,
Fingerprint: clientOpt.fingerprint,
}
mhBytes, err := json.Marshal(mh)
if err != nil {
Expand All @@ -90,9 +89,9 @@ func New(opts ...option.ClientOption) (Client, error) {
pairs[constant.SidecarMetaKey] = string(mhBytes)
// prepare upstream
u, err := upstream.New(
upstream.WithFeedAddrs(clientOpt.FeedAddrs),
upstream.WithDialTimeoutMS(clientOpt.DialTimeoutMS),
upstream.WithBizID(clientOpt.BizID))
upstream.WithFeedAddrs(clientOpt.feedAddrs),
upstream.WithDialTimeoutMS(clientOpt.dialTimeoutMS),
upstream.WithBizID(clientOpt.bizID))
if err != nil {
return nil, fmt.Errorf("init upstream client failed, err: %s", err.Error())
}
Expand All @@ -107,7 +106,7 @@ func New(opts ...option.ClientOption) (Client, error) {
msg := &pbfs.HandshakeMessage{
ApiVersion: sfs.CurrentAPIVersion,
Spec: &pbfs.SidecarSpec{
BizId: clientOpt.BizID,
BizId: clientOpt.bizID,
Version: c.upstream.Version(),
},
}
Expand All @@ -120,16 +119,16 @@ func New(opts ...option.ClientOption) (Client, error) {
if err != nil {
return nil, fmt.Errorf("decode handshake payload failed, err: %s, rid: %s", err.Error(), vas.Rid)
}
err = downloader.Init(vas, clientOpt.BizID, clientOpt.Token, u, pl.RuntimeOption.RepositoryTLS)
err = downloader.Init(vas, clientOpt.bizID, clientOpt.token, u, pl.RuntimeOption.RepositoryTLS)
if err != nil {
return nil, fmt.Errorf("init downloader failed, err: %s", err.Error())
}
if clientOpt.UseFileCache {
cache.Init(true, clientOpt.FileCacheDir)
if clientOpt.useFileCache {
cache.Init(true, clientOpt.fileCacheDir)
}
watcher, err := watch.New(u, option.WatchOptions{
BizID: clientOpt.BizID,
Labels: clientOpt.Labels,
watcher, err := watch.New(u, watch.Options{
BizID: clientOpt.bizID,
Labels: clientOpt.labels,
Fingerprint: fp.Encode(),
})
if err != nil {
Expand All @@ -140,7 +139,7 @@ func New(opts ...option.ClientOption) (Client, error) {
}

// AddWatcher add a watcher to client
func (c *client) AddWatcher(callback option.Callback, app string, opts ...option.AppOption) error {
func (c *client) AddWatcher(callback types.Callback, app string, opts ...types.AppOption) error {
_ = c.watcher.Subscribe(callback, app, opts...)
return nil
}
Expand All @@ -157,34 +156,34 @@ func (c *client) StopWatch() {

// ResetLabels reset bscp client labels, if key conflict, app value will overwrite client value
func (c *client) ResetLabels(labels map[string]string) {
c.opts.Labels = labels
c.opts.labels = labels
for _, subscriber := range c.watcher.Subscribers() {
subscriber.ResetLabels(labels)
}

c.watcher.NotifyReconnect(types.ReconnectSignal{Reason: "reset labels"})
c.watcher.NotifyReconnect(watch.ReconnectSignal{Reason: "reset labels"})
}

// PullFiles pull files from remote
func (c *client) PullFiles(app string, opts ...option.AppOption) (*types.Release, error) {
option := &option.AppOptions{}
func (c *client) PullFiles(app string, opts ...types.AppOption) (*types.Release, error) {
option := &types.AppOptions{}
for _, opt := range opts {
opt(option)
}
vas, _ := c.buildVas()
req := &pbfs.PullAppFileMetaReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.BizID,
BizId: c.opts.bizID,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.Labels,
Uid: c.opts.UID,
Labels: c.opts.labels,
Uid: c.opts.uid,
},
Token: c.opts.Token,
Token: c.opts.token,
Key: option.Key,
}
// merge labels, if key conflict, app value will overwrite client value
req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels)
req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
Expand Down Expand Up @@ -220,24 +219,24 @@ func (c *client) PullFiles(app string, opts ...option.AppOption) (*types.Release
}

// Get 读取 Key 的值
func (c *client) Get(app string, key string, opts ...option.AppOption) (string, error) {
option := &option.AppOptions{}
func (c *client) Get(app string, key string, opts ...types.AppOption) (string, error) {
option := &types.AppOptions{}
for _, opt := range opts {
opt(option)
}
vas, _ := c.buildVas()
req := &pbfs.GetKvValueReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.BizID,
BizId: c.opts.bizID,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.Labels,
Uid: c.opts.UID,
Labels: c.opts.labels,
Uid: c.opts.uid,
},
Token: c.opts.Token,
Token: c.opts.token,
Key: key,
}
req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels)
req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
Expand Down
88 changes: 88 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.
*/

package client

// options options for bscp sdk client
type options struct {
// FeedAddr BSCP feed_server address
feedAddrs []string
// BizID BSCP business id
bizID uint32
// Labels instance labels
labels map[string]string
// Fingerprint sdk fingerprint
fingerprint string
// UID sdk uid
uid string
// UseFileCache use file cache
useFileCache bool
// FileCacheDir file cache directory
fileCacheDir string
// DialTimeoutMS dial upstream timeout in millisecond
dialTimeoutMS int64
// Token sdk token
token string
}

// Option setter for bscp sdk options
type Option func(*options) error

// WithFeedAddrs set feed_server addresses
func WithFeedAddrs(addrs []string) Option {
// TODO: validate Address
return func(o *options) error {
o.feedAddrs = addrs
return nil
}
}

// WithFeedAddr set feed_server addresse
func WithFeedAddr(addr string) Option {
// TODO: validate Address
return func(o *options) error {
o.feedAddrs = []string{addr}
return nil
}
}

// WithBizID set bscp business id
func WithBizID(id uint32) Option {
return func(o *options) error {
o.bizID = id
return nil
}
}

// WithLabels set instance labels
func WithLabels(labels map[string]string) Option {
return func(o *options) error {
o.labels = labels
return nil
}
}

// WithUID set sdk uid
func WithUID(uid string) Option {
return func(o *options) error {
o.uid = uid
return nil
}
}

// WithToken set sdk token
func WithToken(token string) Option {
return func(o *options) error {
o.token = token
return nil
}
}
22 changes: 11 additions & 11 deletions cmd/bscp/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/TencentBlueKing/bscp-go/cmd/bscp/internal/util"
pkgutil "github.com/TencentBlueKing/bscp-go/internal/util"
"github.com/TencentBlueKing/bscp-go/logger"
"github.com/TencentBlueKing/bscp-go/option"
"github.com/TencentBlueKing/bscp-go/types"
)

var (
Expand Down Expand Up @@ -58,21 +58,21 @@ func Pull(cmd *cobra.Command, args []string) {
conf.Labels = pkgutil.MergeLabels(conf.Labels, labels)
}
bscp, err := client.New(
option.FeedAddrs(conf.FeedAddrs),
option.BizID(conf.Biz),
option.Token(conf.Token),
option.Labels(conf.Labels),
option.UID(conf.UID),
client.WithFeedAddrs(conf.FeedAddrs),
client.WithBizID(conf.Biz),
client.WithToken(conf.Token),
client.WithLabels(conf.Labels),
client.WithUID(conf.UID),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
os.Exit(1)
}
for _, app := range conf.Apps {
opts := []option.AppOption{}
opts = append(opts, option.WithKey("**"))
opts = append(opts, option.WithLabels(app.Labels))
opts = append(opts, option.WithUID(app.UID))
opts := []types.AppOption{}
opts = append(opts, types.WithAppKey("**"))
opts = append(opts, types.WithAppLabels(app.Labels))
opts = append(opts, types.WithAppUID(app.UID))
if conf.TempDir != "" {
tempDir = conf.TempDir
}
Expand All @@ -83,7 +83,7 @@ func Pull(cmd *cobra.Command, args []string) {
}
}

func pullAppFiles(bscp client.Client, tempDir string, biz uint32, app string, opts []option.AppOption) error {
func pullAppFiles(bscp client.Client, tempDir string, biz uint32, app string, opts []types.AppOption) error {
// 1. prepare app workspace dir
appDir := path.Join(tempDir, strconv.Itoa(int(biz)), app)
if e := os.MkdirAll(appDir, os.ModePerm); e != nil {
Expand Down
19 changes: 9 additions & 10 deletions cmd/bscp/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/TencentBlueKing/bscp-go/internal/metrics"
pkgutil "github.com/TencentBlueKing/bscp-go/internal/util"
"github.com/TencentBlueKing/bscp-go/logger"
"github.com/TencentBlueKing/bscp-go/option"
"github.com/TencentBlueKing/bscp-go/types"
)

Expand Down Expand Up @@ -75,11 +74,11 @@ func Watch(cmd *cobra.Command, args []string) {
}

bscp, err := client.New(
option.FeedAddrs(conf.FeedAddrs),
option.BizID(conf.Biz),
option.Token(conf.Token),
option.Labels(confLabels),
option.UID(conf.UID),
client.WithFeedAddrs(conf.FeedAddrs),
client.WithBizID(conf.Biz),
client.WithToken(conf.Token),
client.WithLabels(confLabels),
client.WithUID(conf.UID),
)
if err != nil {
logger.Error("init client", logger.ErrAttr(err))
Expand Down Expand Up @@ -236,10 +235,10 @@ func (w *WatchHandler) watchCallback(release *types.Release) error {
return nil
}

func (w *WatchHandler) getSubscribeOptions() []option.AppOption {
options := []option.AppOption{}
options = append(options, option.WithLabels(w.Labels))
options = append(options, option.WithUID(w.UID))
func (w *WatchHandler) getSubscribeOptions() []types.AppOption {
options := []types.AppOption{}
options = append(options, types.WithAppLabels(w.Labels))
options = append(options, types.WithAppUID(w.UID))
return options
}

Expand Down
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bscp sdk examples
============

## 示例代码
* [config](./config) - bscp 命令行配置文件示例
* [kv-ctl](./kv-ctl) - 拉取 kv 型配置命令行示例
* [pull-file](./pull-file) - 拉取 file 型配置
* [watch-file](./watch-file) - 拉取 file 型配置并监听配置变更
Expand All @@ -13,7 +14,7 @@ bscp sdk examples
添加环境变量
```bash
# FEED 地址
export BSCP_FEED_ADDRS="bscp-feed.example.com:9510"
export BSCP_FEED_ADDR="bscp-feed.example.com:9510"
# 服务密钥 Token, 记得需要关联配置文件
export BSCP_TOKEN="xxx"
# 当前业务
Expand Down
Loading