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:配置模板的导入 #2711

Merged
merged 1 commit into from
Nov 7, 2023

Conversation

Ambition9186
Copy link
Contributor

  • 配置模板支持zip、gz、tar压缩包导入
  • 批量新增和修改配置模板

@AlkaidChan AlkaidChan added the bcs-bscp feature or bugfix for bscp label Oct 26, 2023

const (
// MaxFileSize 最大文件
MaxFileSize = 5 * 1024 * 1024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是上传的文本类型最大文件大小,改为 MaxUploadTextFileSize 更加见名知意

// MaxFileSize 最大文件
MaxFileSize = 5 * 1024 * 1024
// ContentLength 文件大小
ContentLength = 100 * 1024 * 1024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理,这里也改成 MaxUploadContentLength

// ContentLength 文件大小
ContentLength = 100 * 1024 * 1024
// FileMode 文件模式
FileMode = "unix"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个常量已经定义过了:
string(table.Unix)

m := dao.genQ.TemplateRevision
// 根据templateIDs获取TemplateRevision中最大ID
// 通过最大ID查询
var templateId []struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以直接 var templateRevisionIDs []uint32 吧,我理解如果最后查询出来只有一列值,是可以 Scan 到一个数组的吧

func (c *configImport) uploadFile(kt *kit.Kit, filePath, rootDir string) (*types.FileInfo, error) {
var (
content bytes.Buffer
fileType = "text"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"text" 已被定义为常量
string(table.Text)


message BatchUpsertTemplatesReq {
message Item {
pbtemplate.Template templates = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 templates 和 template_revisions 都不是 repeated 类型,命名不需要复数形式

@@ -239,6 +241,21 @@ func getLatestTmplRevisions(tmplRevisions []*table.TemplateRevision) map[uint32]

func generateRevisionName() string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里生成版本名称,到秒级就够了吧。这里索引应该是 templateRevisionName + templateID,一次导入操作,分别是对不同的 template 生成新的版本,基本上是不会重复的

return nil
}

func (s *Service) validationData(grpcKit *kit.Kit, updateId []uint32,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里名称改为 validateBatchUpsertTamplates

})
}

err := s.doTemplateRevision(kt, tx, toCreateTr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改名称为: s.doCreateTemplateRevisions

@@ -309,6 +315,12 @@ service Config {
get : "/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/list_not_bound"
};
}
rpc GetTemplateDetailByUniqueKey(GetTemplateDetailByUniqueKeyReq) returns (GetTemplateDetailByUniqueKeyResp) {
option (google.api.http) = {
post : "/api/v1/config/biz/{biz_id}/template_spaces/{template_space_id}/templates/detail"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

根据蓝鲸最新接口规范,按 restful 风格定义接口,这里用 get 方法

@Ambition9186 Ambition9186 force-pushed the bcs-bscp-config branch 2 times, most recently from a29a977 to 2d2a8fb Compare October 30, 2023 09:33

message ListTemplateByTupleResp{
message Item {
pbtemplate.Template templates = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也是哈,不是 repeated 类型不用复数形式命名

message ListTemplateByTupleResp{
message Item {
pbtemplate.Template templates = 1;
pbtr.TemplateRevision template_revisions = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -239,6 +240,20 @@ func getLatestTmplRevisions(tmplRevisions []*table.TemplateRevision) map[uint32]

func generateRevisionName() string {
// Format the current time as YYYYMMDDHHMMSS
time.Sleep(time.Second)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不用 sleep,不然上传一个压缩包有100个配置得 sleep 100 秒

}

message ListConfigItemByTupleResp {
repeated pbci.ConfigItem config_item = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里命名需要用复数形式

fileType = string(table.Binary)
} else {
// 读取文件内容的前512个字节
buffer := make([]byte, 512)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只有5M以下就全部读进内存,只读 512 可能判断不准确,并且可能截断 utf8 的编码导致将文本文件误识别为二进制文件

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之后还需要验证一下如果并发读大文件缓存过快增长会不会导致 OOM,如果会导致OOM,可以考虑限制并发处理文件的数量,以及用 sync.Pool 创建一个可复用的 buffer 池

@Ambition9186 Ambition9186 force-pushed the bcs-bscp-config branch 4 times, most recently from d36d1b1 to 669c668 Compare November 6, 2023 07:13
"bscp.io/pkg/types"
)

var bufferPool sync.Pool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bufferPool 的 Get 方法一般是在全局变量定义的时候就声明了,不确定延迟声明会不会导致不同请求中使用的是不同的 bufferPool 对象,这个改为
var bufferPool = sync.Pool {
New: func() interface{} {
return new(bytes.Buffer)
},
}

@AlkaidChan AlkaidChan merged commit 5bff946 into TencentBlueKing:master Nov 7, 2023
wenxinlee2015 added a commit that referenced this pull request Nov 8, 2023
* github-bk-bcs/master:
  upd: 访问服务管理列表页时清空本地上次访问服务记录 (#2732)
  fix:修复部分空间样式和交互问题 (#2730)
  配置模板压缩包导入以及批量新增配置模板 (#2711)
  optimize: adjust bscp variable validation rule (#2729)
  fix: 加固集群重启kubelet的方式。 (#2728)
AlkaidChan pushed a commit that referenced this pull request Nov 14, 2023
* kv 相关接口

* kv 相关接口

* 配置模板压缩包导入以及批量新增配置模板 (#2711)

* kv 相关接口

* golint

* golint

* kv string 不允许换行

---------

Co-authored-by: ambition <[email protected]>
@Ambition9186 Ambition9186 deleted the bcs-bscp-config branch January 5, 2024 02:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bcs-bscp feature or bugfix for bscp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants