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

feat: Go SDK支持拉取指定配置文件与配置项 --story=120303184 #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

"github.com/TencentBlueKing/bscp-go/internal/cache"
"github.com/TencentBlueKing/bscp-go/internal/downloader"
"github.com/TencentBlueKing/bscp-go/internal/upstream"

Check failure on line 37 in client/client.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/TencentBlueKing/bscp-go/internal/upstream (-: # github.com/TencentBlueKing/bscp-go/internal/upstream

Check failure on line 37 in client/client.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/TencentBlueKing/bscp-go/internal/upstream (-: # github.com/TencentBlueKing/bscp-go/internal/upstream
"github.com/TencentBlueKing/bscp-go/internal/util"
"github.com/TencentBlueKing/bscp-go/pkg/logger"
)
Expand All @@ -57,6 +57,8 @@
StopWatch()
// ResetLabels reset bscp client labels, if key conflict, app value will overwrite client value
ResetLabels(labels map[string]string)
// GetFile get files from remote
GetFile(app string, filePath string, opts ...AppOption) (*FileStreamReader, error)
}

// ErrNotFoundKvMD5 is err not found kv md5
Expand Down Expand Up @@ -218,6 +220,45 @@
c.watcher.NotifyReconnect(reconnectSignal{Reason: "reset labels"})
}

// GetFile implements Client.
func (c *client) GetFile(app string, filePath string, opts ...AppOption) (*FileStreamReader, error) {
option := &AppOptions{}
for _, opt := range opts {
opt(option)
}

vas, _ := c.buildVas()

req := &pbfs.GetSingleFileContentReq{
ApiVersion: sfs.CurrentAPIVersion,
BizId: c.opts.bizID,
AppMeta: &pbfs.AppMeta{
App: app,
Labels: c.opts.labels,
Uid: c.opts.uid,
},
Token: c.opts.token,
FilePath: filePath,
}

// merge labels, if key conflict, app value will overwrite client value
req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels)
// reset uid
if option.UID != "" {
req.AppMeta.Uid = option.UID
}
if req.AppMeta.Uid == "" {
req.AppMeta.Uid = c.opts.fingerprint
}

stream, err := c.upstream.GetSingleFileContent(vas, req)
if err != nil {
return nil, fmt.Errorf("get app file failed, err: %v", err)
}

return &FileStreamReader{stream: stream}, nil
}

// PullFiles pull files from remote
func (c *client) PullFiles(app string, opts ...AppOption) (*Release, error) { // nolint
option := &AppOptions{}
Expand Down
31 changes: 31 additions & 0 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit"
pbci "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/config-item"
pbhook "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/hook"
pbfs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/feed-server"
sfs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/sf-share"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/version"
Expand Down Expand Up @@ -662,3 +663,33 @@ func (r *Release) recordChangeEvent() error {
}
return nil
}

// FileStreamReader 定义Reader结构体
type FileStreamReader struct {
stream pbfs.Upstream_GetSingleFileContentClient
buffer []byte
}

// Read 从 stream 获取数据
func (r *FileStreamReader) Read(p []byte) (int, error) {
if len(r.buffer) == 0 {
// 从 stream 拉取数据
resp, err := r.stream.Recv()
if err != nil {
return 0, err
}
r.buffer = resp.GetContent()
}

// 读取 buffer 数据到 p 中
n := copy(p, r.buffer)
// 更新 buffer,移除已读取的部分
r.buffer = r.buffer[n:]

return n, nil
}

// Close 关闭流
func (r *FileStreamReader) Close() error {
return r.stream.CloseSend()
}
102 changes: 102 additions & 0 deletions examples/get-file/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.
*/

// download file bench test
package main

import (
"fmt"
"io"
"os"
"path/filepath"
"time"

"golang.org/x/exp/slog"

"github.com/TencentBlueKing/bscp-go/client"
"github.com/TencentBlueKing/bscp-go/pkg/logger"
)

func main() {

// 设置日志自定义 Handler
// logger.SetHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{}))

// 在线服务, 可设置 metrics
// metrics.RegisterMetrics()
// http.Handle("/metrics", promhttp.Handler())

var feedAddr string = ""
var biz uint32 = 0
var token string = ""
var app string = ""
var targetFileName string = ""
labels := map[string]string{}

start := time.Now()

err := func() error {
clientOpts := []client.Option{
client.WithFeedAddr(feedAddr),
client.WithBizID(biz),
client.WithToken(token),
client.WithLabels(labels),
}
// 初始化客户端
bscp, err := client.New(clientOpts...)
if err != nil {
return fmt.Errorf("init client failed, err: %v", err)
}
opts := []client.AppOption{}
// 拉取单文件
reader, err := bscp.GetFile(app, targetFileName, opts...)
if err != nil {
return fmt.Errorf("get app file failed, err: %v", err)
}
defer func() {
_ = reader.Close()
}()

// 设置文件保存路径
savePath := ""
fullPath := filepath.Join(savePath, targetFileName)

// 创建输出文件
outFile, err := os.Create(fullPath)
if err != nil {
return fmt.Errorf("create file failed, err: %v", err)
}
defer func() {
_ = outFile.Close()
}()

// 将数据从 reader 复制到 outFile
_, err = io.Copy(outFile, reader)
if err != nil {
return fmt.Errorf("copy data failed, err: %v", err)
}

logger.Debug("get file content by downloading from repo success",
slog.String("file", fullPath))

return nil
}()

costTime := time.Since(start).Seconds()
// 判断是否出错,决定是否退出
if err != nil {
logger.Error("download app files failed", logger.ErrAttr(err))
os.Exit(1)
}

logger.Info("download app files finished", slog.Float64("cost_time_seconds", costTime))
}
60 changes: 31 additions & 29 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ go 1.20
require (
github.com/TencentBlueKing/bscp-go v1.0.5
github.com/spf13/cobra v1.7.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20240425034551-e53e35b46a7b // indirect
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20240517115418-9396857e3664 // indirect
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20240619073002-754d2df7a1e7 // indirect
github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp v0.0.0-20241014100623-5cbfebdf0c7e // indirect
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/bluele/gcache v0.0.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.8 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand All @@ -31,13 +31,14 @@ require (
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.12.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand All @@ -46,22 +47,22 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/miekg/dns v1.1.59 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
Expand All @@ -70,39 +71,40 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/urfave/cli/v2 v2.27.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go-micro.dev/v4 v4.10.2 // indirect
go.etcd.io/etcd/api/v3 v3.5.13 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.13 // indirect
go.etcd.io/etcd/client/v3 v3.5.13 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
go-micro.dev/v4 v4.11.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.14 // indirect
go.etcd.io/etcd/client/v3 v3.5.14 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
gorm.io/gorm v1.25.4 // indirect
k8s.io/klog/v2 v2.130.0 // indirect
)

replace github.com/TencentBlueKing/bscp-go => ../
Loading
Loading