-
Notifications
You must be signed in to change notification settings - Fork 250
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:bscp 添加dal/vault #2719
Merged
AlkaidChan
merged 11 commits into
TencentBlueKing:master
from
linfang-canway:bscp-add-vault-dal
Nov 2, 2023
Merged
feat:bscp 添加dal/vault #2719
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f285c95
添加dal/vault
linfang-canway 3bc8a06
添加dal/vault
linfang-canway f5824d9
Merge remote-tracking branch 'TencentBlueKing/master' into bscp-add-v…
linfang-canway 5da8fc0
添加dal/vault
linfang-canway 20a5ed4
添加dal/vault
linfang-canway 9dcd3de
golint
linfang-canway 99838a5
golint
linfang-canway 01a2de1
golint
linfang-canway 0e7f15d
创建前校验
linfang-canway ae73449
golint
linfang-canway ff2861f
golint
linfang-canway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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" | ||
) | ||
|
||
const ( | ||
// MountPath mount path | ||
MountPath = "bk_bscp" | ||
// kvPath kv path | ||
kvPath = "biz/%d/apps/%d/kv/%s" | ||
) | ||
|
||
// UpsertKv 创建|更新kv | ||
func (s *set) UpsertKv(kit kit.Kit, bizID, appID uint32, key, content string) (int, error) { | ||
|
||
data := map[string]interface{}{ | ||
"data": content, | ||
} | ||
secret, err := s.cli.KVv2(MountPath).Put(kit.Ctx, fmt.Sprintf(kvPath, bizID, appID, key), data) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return secret.VersionMetadata.Version, nil | ||
|
||
} | ||
|
||
// GetLastKv 获取最新的kv | ||
func (s *set) GetLastKv(kit kit.Kit, bizID, appID uint32, key string) (string, error) { | ||
|
||
kv, err := s.cli.KVv2(MountPath).Get(kit.Ctx, fmt.Sprintf(kvPath, bizID, appID, 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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
import ( | ||
"log" | ||
|
||
vault "github.com/hashicorp/vault/api" | ||
|
||
"bscp.io/pkg/cc" | ||
"bscp.io/pkg/kit" | ||
) | ||
|
||
// Set ... | ||
type Set interface { | ||
// IsMountPathExists 挂载目录是否存在 | ||
IsMountPathExists(path string) (bool, error) | ||
//CreateMountPath 创建挂载目录 | ||
CreateMountPath(path string, config *vault.MountInput) error | ||
// UpsertKv 创建|更新kv | ||
UpsertKv(kit kit.Kit, bizID, appID uint32, key, content string) (int, error) | ||
// GetLastKv 获取最新的kv | ||
GetLastKv(kit kit.Kit, bizID, appID uint32, key string) (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 { | ||
log.Fatalf("unable to initialize Vault client: %v", err) | ||
} | ||
|
||
client.SetToken(opt.Token) | ||
|
||
s := &set{ | ||
cli: client, | ||
} | ||
|
||
return s, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其他设计
"biz/%d/apps/%d/kv/{version}