Skip to content

Commit f38bd66

Browse files
committed
feat: 集群项目数据实时订阅机制
1 parent bb36353 commit f38bd66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+21748
-5698
lines changed

bcs-services/bcs-storage/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11

22
.PHONY: init
33
init:
4-
go install github.com/golang/protobuf/protoc-gen-go@latest
5-
go install go install go-micro.dev/v4/cmd/[email protected]
4+
go install google.golang.org/protobuf/cmd/[email protected]
5+
go install google.golang.org/grpc/cmd/[email protected]
6+
go install github.com/go-micro/generator/cmd/protoc-gen-micro@latest
67
go install github.com/micro/micro/v3/cmd/protoc-gen-openapi@latest
78
go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
89
go install github.com/grpc-ecosystem/grpc-gateway/v2/[email protected]
10+
go install github.com/envoyproxy/[email protected]
911

1012
.PHONY: api
1113
api:
12-
protoc -I../../bcs-common/pkg/third_party/ --proto_path=. --openapi_out=./pkg/proto/ pkg/proto/bcs-storage.proto
14+
protoc -I./third_party/ --proto_path=. --openapi_out=./pkg/proto/ pkg/proto/bcs-storage.proto
1315

1416
.PHONY: proto
1517
proto:
16-
protoc -I../../bcs-common/pkg/third_party/ --proto_path=. --validate_out=paths=source_relative,lang=go:. --grpc-gateway_out=logtostderr=true,register_func_suffix=Gw:./pkg/proto/ --swagger_out=logtostderr=true:. --go-grpc_out=./pkg/proto/ --micro_out=./pkg/proto/ --go_out=:./pkg/proto/ ./pkg/proto/bcs-storage.proto
18+
protoc -I./third_party/ --proto_path=. --validate_out=paths=source_relative,lang=go:. --grpc-gateway_out=logtostderr=true,register_func_suffix=Gw:./pkg/proto/ --swagger_out=logtostderr=true:. --go-grpc_out=./pkg/proto/ --micro_out=./pkg/proto/ --go_out=:./pkg/proto/ ./pkg/proto/bcs-storage.proto

bcs-services/bcs-storage/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/envoyproxy/protoc-gen-validate v1.0.4
4141
github.com/go-micro/plugins/v4/client/grpc v1.1.0
4242
github.com/go-micro/plugins/v4/server/grpc v1.1.1
43+
github.com/golang/glog v1.2.0
4344
github.com/golang/protobuf v1.5.4
4445
github.com/grpc-ecosystem/grpc-gateway v1.16.0
4546
github.com/pkg/errors v0.9.1
@@ -64,6 +65,7 @@ require (
6465
github.com/evanphx/json-patch/v5 v5.5.0 // indirect
6566
github.com/felixge/httpsnoop v1.0.4 // indirect
6667
github.com/fsnotify/fsnotify v1.5.4 // indirect
68+
github.com/ghodss/yaml v1.0.0 // indirect
6769
github.com/go-acme/lego/v4 v4.4.0 // indirect
6870
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
6971
github.com/go-git/go-billy/v5 v5.6.0 // indirect

bcs-services/bcs-storage/pkg/constants/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const (
3434
TypeTag = "type"
3535
// ClusterIDTag clusterId
3636
ClusterIDTag = "clusterId"
37+
// ClusterIDUpperTag clusterID
38+
ClusterIDUpperTag = "clusterID"
39+
// ProjectIDTag projectID
40+
ProjectIDTag = "projectID"
3741
// NamespaceTag namespace
3842
NamespaceTag = "namespace"
3943
// MessageTag message

bcs-services/bcs-storage/pkg/handler/bcs-storage.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/constants"
2626
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/alarm"
27+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/cluster"
2728
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/clusterconfig"
2829
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/dynamic"
2930
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/dynamicquery"
@@ -32,11 +33,14 @@ import (
3233
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/hostconfig"
3334
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/metric"
3435
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/metricwatch"
36+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/project"
3537
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/handler/internal/watchk8smesos"
3638
storage "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/proto"
3739
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/util"
3840
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/lib"
41+
v1httpclu "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/v1http/cluster"
3942
dynamic2 "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/v1http/dynamic"
43+
v1httppro "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/v1http/project"
4044
)
4145

4246
// Storage storage struct, which include operations of alarm,
@@ -126,6 +130,110 @@ func (s *Storage) ListAlarm(ctx context.Context, req *storage.ListAlarmRequest,
126130
return nil
127131
}
128132

133+
// PutProjectInfo 订阅项目数据
134+
func (s *Storage) PutProjectInfo(ctx context.Context, req *storage.PutProjectInfoRequest,
135+
rsp *storage.PutProjectInfoResponse) error {
136+
// 如果请求合法,则执行函数请求
137+
if err := req.Validate(); err != nil {
138+
rsp.Result = false
139+
rsp.Message = err.Error()
140+
rsp.Code = common.AdditionErrorCode + 500
141+
return nil
142+
}
143+
// 打印请求体
144+
blog.Infof("PutProjectInfo req: %v", util.PrettyStruct(req))
145+
data, err := project.HandlerCreateProjectInfoReq(ctx, req)
146+
if err != nil {
147+
// 如果处理过程中出错,将错误信息直接返回
148+
rsp.Result = false
149+
rsp.Code = common.BcsErrStoragePutResourceFail
150+
rsp.Message = common.BcsErrStoragePutResourceFailStr
151+
blog.Errorf("PutProjectInfo | put project info failed.err: %v", err)
152+
return nil
153+
}
154+
155+
v1httppro.PushCreateProjectInfoToQueue(data)
156+
157+
// 将查询结果添加至响应体
158+
rsp.Result = true
159+
rsp.Code = common.BcsSuccess
160+
rsp.Message = common.BcsSuccessStr
161+
162+
// 打印响应体
163+
blog.Infof("PutProject rsp: %v", util.PrettyStruct(rsp))
164+
return nil
165+
}
166+
167+
// PutClusterInfo 订阅集群数据
168+
func (s *Storage) PutClusterInfo(ctx context.Context, req *storage.PutClusterInfoRequest,
169+
rsp *storage.PutClusterInfoResponse) error {
170+
// 如果请求合法,则执行函数请求
171+
if err := req.Validate(); err != nil {
172+
rsp.Result = false
173+
rsp.Message = err.Error()
174+
rsp.Code = common.AdditionErrorCode + 500
175+
return nil
176+
}
177+
// 打印请求体
178+
blog.Infof("PutClusterInfo req: %v", util.PrettyStruct(req))
179+
data, err := cluster.HandlerCreateClusterInfoReq(ctx, req)
180+
if err != nil {
181+
// 如果处理过程中出错,将错误信息直接返回
182+
rsp.Result = false
183+
rsp.Code = common.BcsErrStoragePutResourceFail
184+
rsp.Message = common.BcsErrStoragePutResourceFailStr
185+
blog.Errorf("PutClusterInfo | put cluster info failed.err: %v", err)
186+
return nil
187+
}
188+
189+
v1httpclu.PushCreateClusterInfoToQueue(data)
190+
191+
// 将查询结果添加至响应体
192+
rsp.Result = true
193+
rsp.Code = common.BcsSuccess
194+
rsp.Message = common.BcsSuccessStr
195+
196+
// 打印响应体
197+
blog.Infof("PutProject rsp: %v", util.PrettyStruct(rsp))
198+
return nil
199+
}
200+
201+
// DeleteClusterInfo 删除集群数据
202+
func (s *Storage) DeleteClusterInfo(ctx context.Context, req *storage.DeleteClusterInfoRequest,
203+
rsp *storage.PutClusterInfoResponse) error {
204+
// 如果请求合法,则执行函数请求
205+
if err := req.Validate(); err != nil {
206+
rsp.Result = false
207+
rsp.Message = err.Error()
208+
rsp.Code = common.AdditionErrorCode + 500
209+
return nil
210+
}
211+
// 打印请求体
212+
blog.Infof("DeleteClusterInfo req: %v", util.PrettyStruct(req))
213+
data, err := cluster.HandlerDeleteClusterInfoReq(ctx, req)
214+
if err != nil {
215+
// 如果处理过程中出错,将错误信息直接返回
216+
rsp.Result = false
217+
rsp.Code = common.BcsErrStorageDeleteResourceFail
218+
rsp.Message = common.BcsErrStorageDeleteResourceFailStr
219+
blog.Errorf("DeleteClusterInfo | delete cluster info failed.err: %v", err)
220+
return nil
221+
}
222+
223+
if len(data) == 1 {
224+
v1httpclu.PushDeleteClusterInfoToQueue(data[0])
225+
}
226+
227+
// 将查询结果添加至响应体
228+
rsp.Result = true
229+
rsp.Code = common.BcsSuccess
230+
rsp.Message = common.BcsSuccessStr
231+
232+
// 打印响应体
233+
blog.Infof("DeleteClusterInfo rsp: %v", util.PrettyStruct(rsp))
234+
return nil
235+
}
236+
129237
// **** Cluster Config(集群配置) ****
130238

131239
// GetClusterConfig 获取集群配置 get config of cluster
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Blueking Container Service available.
3+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
* http://opensource.org/licenses/MIT
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9+
* either express or implied. See the License for the specific language governing permissions and
10+
* limitations under the License.
11+
*/
12+
13+
// Package cluster xxx
14+
package cluster
15+
16+
import (
17+
"context"
18+
19+
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
20+
21+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/constants"
22+
storage "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/proto"
23+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/lib"
24+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/v1http/cluster"
25+
)
26+
27+
var (
28+
clusterFeatTags = []string{constants.ClusterIDUpperTag}
29+
)
30+
31+
type general struct {
32+
ctx context.Context
33+
clusterID string
34+
resourceFeatList []string
35+
data map[string]interface{}
36+
}
37+
38+
func (g *general) getCluCondition() *operator.Condition {
39+
features := operator.M{constants.ClusterIDUpperTag: g.clusterID}
40+
return operator.NewLeafCondition(operator.Eq, features)
41+
}
42+
43+
func (g *general) getFeatures() operator.M {
44+
features := make(operator.M)
45+
features[constants.ClusterIDUpperTag] = g.clusterID
46+
return features
47+
}
48+
49+
func (g *general) putResources() error {
50+
return cluster.PutData(g.ctx, g.data, g.getFeatures(), g.resourceFeatList)
51+
}
52+
53+
func (g *general) deleteResources() ([]operator.M, error) {
54+
condition := g.getCluCondition()
55+
getOption := &lib.StoreGetOption{
56+
Cond: condition,
57+
IsAllDocuments: true,
58+
}
59+
rmOption := &lib.StoreRemoveOption{
60+
Cond: condition,
61+
// when resource to be deleted not found, do not return error
62+
IgnoreNotFound: true,
63+
}
64+
return cluster.DeleteBatchData(g.ctx, getOption, rmOption)
65+
}
66+
67+
// HandlerCreateClusterInfoReq CreateClusterInfoReq业务方法
68+
func HandlerCreateClusterInfoReq(ctx context.Context, req *storage.PutClusterInfoRequest) (operator.M, error) {
69+
70+
data := map[string]interface{}{
71+
"clusterID": req.ClusterID,
72+
"data": req.Data.AsMap(),
73+
}
74+
r := &general{
75+
ctx: ctx,
76+
clusterID: req.ClusterID,
77+
resourceFeatList: clusterFeatTags,
78+
data: data,
79+
}
80+
81+
return data, r.putResources()
82+
}
83+
84+
// HandlerDeleteClusterInfoReq DeleteClusterInfoReq业务方法
85+
func HandlerDeleteClusterInfoReq(ctx context.Context, req *storage.DeleteClusterInfoRequest) ([]operator.M, error) {
86+
87+
r := &general{
88+
ctx: ctx,
89+
clusterID: req.ClusterID,
90+
resourceFeatList: clusterFeatTags,
91+
}
92+
93+
return r.deleteResources()
94+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Blueking Container Service available.
3+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
4+
* Licensed under the MIT License (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
* http://opensource.org/licenses/MIT
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9+
* either express or implied. See the License for the specific language governing permissions and
10+
* limitations under the License.
11+
*/
12+
13+
// Package project xxx
14+
package project
15+
16+
import (
17+
"context"
18+
19+
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
20+
21+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/constants"
22+
storage "github.com/Tencent/bk-bcs/bcs-services/bcs-storage/pkg/proto"
23+
"github.com/Tencent/bk-bcs/bcs-services/bcs-storage/storage/actions/v1http/project"
24+
)
25+
26+
var (
27+
projectFeatTags = []string{constants.ProjectIDTag}
28+
)
29+
30+
type general struct {
31+
ctx context.Context
32+
projectID string
33+
resourceFeatList []string
34+
data map[string]interface{}
35+
}
36+
37+
func (g *general) getFeatures() operator.M {
38+
features := make(operator.M)
39+
features[constants.ProjectIDTag] = g.projectID
40+
return features
41+
}
42+
43+
func (g *general) putResources() error {
44+
return project.PutData(g.ctx, g.data, g.getFeatures(), g.resourceFeatList)
45+
}
46+
47+
// HandlerCreateProjectInfoReq CreateProjectInfoReq业务方法
48+
func HandlerCreateProjectInfoReq(ctx context.Context, req *storage.PutProjectInfoRequest) (operator.M, error) {
49+
50+
data := map[string]interface{}{
51+
"projectID": req.ProjectID,
52+
"data": req.Data.AsMap(),
53+
}
54+
r := &general{
55+
ctx: ctx,
56+
projectID: req.ProjectID,
57+
resourceFeatList: projectFeatTags,
58+
data: data,
59+
}
60+
61+
return data, r.putResources()
62+
}

0 commit comments

Comments
 (0)