Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-bk-bcs/master'
Browse files Browse the repository at this point in the history
* github-bk-bcs/master:
  bcs-webconsole解决遗留问题 (#2710)
  解决trace报错的问题 (#2709)
  feat:bscp 添加dal/vault (#2719)
  fix:修复部分空间样式交互问题 (#2726)
  • Loading branch information
wenxinlee2015 committed Nov 3, 2023
2 parents d950387 + a452830 commit a3e9e34
Show file tree
Hide file tree
Showing 27 changed files with 468 additions and 34 deletions.
7 changes: 7 additions & 0 deletions bcs-common/pkg/otel/trace/gin/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -116,13 +117,17 @@ func Middleware(server string, opts ...Option) gin.HandlerFunc { // nolint
if len(query) > 1024 {
query = fmt.Sprintf("%s...(Total %s)", query[:1024], humanize.Bytes(uint64(len(query))))
}
// 以utf-8方式合法截取字符串
query = strings.ToValidUTF8(query, "")
span.SetAttributes(attribute.Key("query").String(query))

// 记录body
body := string(getRequestBody(c.Request))
if len(body) > 1024 {
body = fmt.Sprintf("%s...(Total %s)", body[:1024], humanize.Bytes(uint64(len(body))))
}
// 以utf-8方式合法截取字符串
body = strings.ToValidUTF8(body, "")
span.SetAttributes(attribute.Key("body").String(body))

// pass the span through the request context
Expand All @@ -139,6 +144,8 @@ func Middleware(server string, opts ...Option) gin.HandlerFunc { // nolint
if len(respBody) > 1024 {
respBody = fmt.Sprintf("%s...(Total %s)", writer.b.String()[:1024], humanize.Bytes(uint64(len(writer.b.String()))))
}
// 以utf-8方式合法截取字符串
respBody = strings.ToValidUTF8(respBody, "")
span.SetAttributes(attribute.Key("rsp").String(respBody))
elapsedTime := time.Since(startTime)
span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String()))
Expand Down
4 changes: 4 additions & 0 deletions bcs-common/pkg/otel/trace/micro/v2/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -71,6 +72,9 @@ func NewTracingWrapper(fn server.HandlerFunc) server.HandlerFunc {
respBody = fmt.Sprintf("%s...(Total %s)", respBody[:1024], humanize.Bytes(uint64(len(respBody))))
}

// 以utf-8方式合法截取字符串
reqBody = strings.ToValidUTF8(reqBody, "")
respBody = strings.ToValidUTF8(respBody, "")
// 设置额外标签
span.SetAttributes(attribute.Key("req").String(reqBody))
span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String()))
Expand Down
4 changes: 4 additions & 0 deletions bcs-common/pkg/otel/trace/micro/v4/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -87,6 +88,9 @@ func NewTracingWrapper() server.HandlerWrapper {
respBody = fmt.Sprintf("%s...(Total %s)", respBody[:1024], humanize.Bytes(uint64(len(respBody))))
}

// 以utf-8方式合法截取字符串
reqBody = strings.ToValidUTF8(reqBody, "")
respBody = strings.ToValidUTF8(respBody, "")
// 设置额外标签
span.SetAttributes(attribute.Key("req").String(reqBody))
span.SetAttributes(attribute.Key("elapsed_ime").String(elapsedTime.String()))
Expand Down
26 changes: 25 additions & 1 deletion bcs-services/bcs-bscp/cmd/data-service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/hashicorp/vault/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

Expand All @@ -31,6 +32,7 @@ import (
"bscp.io/pkg/cc"
"bscp.io/pkg/criteria/uuid"
"bscp.io/pkg/dal/dao"
"bscp.io/pkg/dal/vault"
"bscp.io/pkg/logs"
"bscp.io/pkg/metrics"
pbds "bscp.io/pkg/protocol/data-service"
Expand Down Expand Up @@ -81,6 +83,7 @@ type dataService struct {
service *service.Service
sd serviced.Service
daoSet dao.Set
vault vault.Set
}

// prepare do prepare jobs before run data service.
Expand Down Expand Up @@ -130,6 +133,27 @@ func (ds *dataService) prepare(opt *options.Option) error {

ds.daoSet = set

// initial Vault set
vaultSet, err := vault.NewSet(cc.DataService().Vault)
if err != nil {
return fmt.Errorf("initial vault set failed, err: %v", err)
}
// 挂载目录
exists, err := vaultSet.IsMountPathExists(vault.MountPath)
if err != nil {
return fmt.Errorf("error checking mount path: %v", err)
}
if !exists {
mountConfig := &api.MountInput{
Type: "kv-v2",
}
if err = vaultSet.CreateMountPath(vault.MountPath, mountConfig); err != nil {
return fmt.Errorf("initial vault mount path failed, err: %v", err)
}
}

ds.vault = vaultSet

return nil
}

Expand Down Expand Up @@ -167,7 +191,7 @@ func (ds *dataService) listenAndServe() error {
}

serve := grpc.NewServer(opts...)
svc, err := service.NewService(ds.sd, ds.daoSet)
svc, err := service.NewService(ds.sd, ds.daoSet, ds.vault)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion bcs-services/bcs-bscp/cmd/data-service/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"bscp.io/pkg/cc"
"bscp.io/pkg/dal/dao"
"bscp.io/pkg/dal/repository"
"bscp.io/pkg/dal/vault"
"bscp.io/pkg/metrics"
pbds "bscp.io/pkg/protocol/data-service"
"bscp.io/pkg/serviced"
Expand All @@ -32,6 +33,7 @@ import (
// Service do all the data service's work
type Service struct {
dao dao.Set
vault vault.Set
gateway *gateway
// esb esb api client.
esb client.Client
Expand All @@ -40,7 +42,7 @@ type Service struct {
}

// NewService create a service instance.
func NewService(sd serviced.Service, daoSet dao.Set) (*Service, error) {
func NewService(sd serviced.Service, daoSet dao.Set, vaultSet vault.Set) (*Service, error) {
state, ok := sd.(serviced.State)
if !ok {
return nil, errors.New("discover convert state failed")
Expand All @@ -65,6 +67,7 @@ func NewService(sd serviced.Service, daoSet dao.Set) (*Service, error) {

svc := &Service{
dao: daoSet,
vault: vaultSet,
gateway: gateway,
esb: esbCli,
repo: repo,
Expand Down
5 changes: 5 additions & 0 deletions bcs-services/bcs-bscp/etc/bcs-bscp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ repository:
user: xx
downstream:
bounceIntervalHour: 48

# vault
vault:
address: http://127.0.0.1:8200
token: root
5 changes: 5 additions & 0 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ type DataServiceSetting struct {
Sharding Sharding `yaml:"sharding"`
Esb Esb `yaml:"esb"`
Repo Repository `yaml:"repository"`
Vault Vault `yaml:"vault"`
}

// trySetFlagBindIP try set flag bind ip.
Expand Down Expand Up @@ -319,6 +320,10 @@ func (s DataServiceSetting) Validate() error {
return err
}

if err := s.Vault.validate(); err != nil {
return err
}

return nil
}

Expand Down
22 changes: 22 additions & 0 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,25 @@ func (c Credential) validate() error {

return nil
}

// Vault Used to store the configuration information required for connecting to the Vault server.
type Vault struct {
// Address is used to store the address of the Vault server
Address string `yaml:"address"`
// Token is used for accessing the Vault server
Token string `yaml:"token"`
}

// validate Vault options
func (v Vault) validate() error {

if v.Address == "" {
return errors.New("vault address is not set")
}

if v.Token == "" {
return errors.New("vault token is not set")
}

return nil
}
81 changes: 81 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/vault/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 vault

import (
"fmt"

"bscp.io/pkg/kit"
"bscp.io/pkg/types"
)

const (
// MountPath mount path
MountPath = "bk_bscp"
// kvPath kv path
kvPath = "biz/%d/apps/%d/kv/key/%s"
)

// UpsertKv 创建|更新kv
func (s *set) UpsertKv(kit *kit.Kit, opt *types.UpsertKvOption) (int, error) {

if err := opt.Validate(); err != nil {
return 0, err
}

data := map[string]interface{}{
"type": opt.KvType,
"value": opt.Value,
}
secret, err := s.cli.KVv2(MountPath).Put(kit.Ctx, fmt.Sprintf(kvPath, opt.BizID, opt.AppID, opt.Key), data)
if err != nil {
return 0, err
}

return secret.VersionMetadata.Version, nil

}

// GetLastKv 获取最新的kv
func (s *set) GetLastKv(kit *kit.Kit, opt *types.GetLastKvOpt) (string, error) {

kv, err := s.cli.KVv2(MountPath).Get(kit.Ctx, fmt.Sprintf(kvPath, opt.BizID, opt.AppID, opt.Key))
if err != nil {
return "", err
}

value, ok := kv.Data["data"].(string)
if !ok {
return "", fmt.Errorf("value type assertion failed: err : %v", err)
}

return value, nil

}

// GetKvByVersion 根据版本获取kv
func (s *set) GetKvByVersion(kit *kit.Kit, bizID, appID uint32, key string, version int) (string, error) {

kv, err := s.cli.KVv2(MountPath).GetVersion(kit.Ctx, fmt.Sprintf(kvPath, bizID, appID, key), version)
if err != nil {
return "", err
}

value, ok := kv.Data["data"].(string)
if !ok {
return "", fmt.Errorf("value type assertion failed: err : %v", err)
}

return value, nil

}
37 changes: 37 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/vault/mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 vault

import (
"fmt"

"github.com/hashicorp/vault/api"
)

// CreateMountPath 创建挂载目录
func (s *set) CreateMountPath(path string, config *api.MountInput) error {
return s.cli.Sys().Mount(path, config)
}

// IsMountPathExists 挂载目录是否存在
func (s *set) IsMountPathExists(path string) (bool, error) {
// 列出所有的挂载路径
mounts, err := s.cli.Sys().ListMounts()
if err != nil {
return false, err
}

// 检查要创建的挂载路径是否已存在
_, exists := mounts[fmt.Sprintf("%s/", path)]
return exists, nil
}
60 changes: 60 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/vault/vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 vault NOTES
package vault

import (
vault "github.com/hashicorp/vault/api"

"bscp.io/pkg/cc"
"bscp.io/pkg/kit"
"bscp.io/pkg/types"
)

// Set ...
type Set interface {
// IsMountPathExists 挂载目录是否存在
IsMountPathExists(path string) (bool, error)
// CreateMountPath 创建挂载目录
CreateMountPath(path string, config *vault.MountInput) error
// UpsertKv 创建|更新kv
UpsertKv(kit *kit.Kit, opt *types.UpsertKvOption) (int, error)
// GetLastKv 获取最新的kv
GetLastKv(kit *kit.Kit, opt *types.GetLastKvOpt) (string, error)
// GetKvByVersion 根据版本获取kv
GetKvByVersion(kit *kit.Kit, bizID, appID uint32, key string, version int) (string, error)
}

type set struct {
cli *vault.Client
}

// NewSet ...
func NewSet(opt cc.Vault) (Set, error) {

config := vault.DefaultConfig()
config.Address = opt.Address

client, err := vault.NewClient(config)
if err != nil {
return nil, err
}

client.SetToken(opt.Token)

s := &set{
cli: client,
}

return s, nil
}
Loading

0 comments on commit a3e9e34

Please sign in to comment.